有头双向链表(C++)

关于对有头双向链表的头插,头删,尾插,尾删,以及插入,查询的方法代码如下:

整个原代码:

List.h

#pragma once
#include<iostream>
using namespace std;

class ListNode
{
public:
	ListNode* begin;
	int data;
	ListNode* end;

};

void ListInit(ListNode*& p);//链表初始化
void ListPushFront(ListNode*& phead, int x);头插
void Listprintf(ListNode*& phead);打印链表
void ListPushBack(ListNode*& phead, int x);尾插
void ListPopBack(ListNode*& phead);尾删
void ListPopFront(ListNode*& phead);头删
ListNode* SListFind(ListNode*& phead, int x);查找
void ListInsert(ListNode*& phead, ListNode* pos, int x);指定节点前插入

List.cpp

#include<iostream>
#include"List.h"
using namespace std;

void ListInit(ListNode*& p)//初始化
{
	p = new ListNode();
	p->begin = p;
	p->end = p;
	p->data = 0;
}
void ListPushFront(ListNode*& phead, int x)//头插
{
	ListNode* newnode = new ListNode();
	newnode->data = x;
	if (phead->begin==phead)
	{
		phead->end = newnode;
		phead->begin = newnode;
		newnode->end = phead;
		newnode->begin = phead;
	}
	else
	{
		newnode->end = phead;
		newnode->begin = phead->begin;
		phead->begin->end = newnode;
		phead->begin = newnode;

	}
		
		
	phead = newnode;

}
void Listprintf(ListNode*& phead)//打印
{
	ListNode* head=phead->end;
	cout << "phead->begin " << phead->begin << "  phead->data  " << phead->data << "  phead->end  " << phead->end << endl;
	while (phead!=head)
	{
		cout << "p->begin=" << head->begin << "  p->data=" << head->data << "  p->end=" << head->end << endl;
		head = head->end;
	}
}
void ListPushBack(ListNode*& phead, int x)//尾插
{
	ListNode* newnode = new ListNode();
	newnode->data = x;
	if (phead->begin == phead)
	{
		phead->end = newnode;
		phead->begin = newnode;
		newnode->end = phead;
		newnode->begin = phead;
	}
	else {
		newnode->begin = phead->begin;
		newnode->end = phead;
		newnode->begin->end = newnode;
		phead->begin = newnode;
	}
}
void ListPopBack(ListNode*& phead)
{
	phead->begin = phead->begin->begin;
	phead->begin->end = phead;
}
void ListPopFront(ListNode*& phead)
{
	phead->end->begin = phead->begin;
	phead->begin->end = phead->end;
	phead = phead->end;
}
ListNode* SListFind(ListNode*& phead, int x)
{
	ListNode* head = phead->end;
	if (phead->data == x)
	{
		return phead;
	}
	while (phead != head)
	{
		if (head->data == x)
		{
			return head;

		}
		head = head->end;
	}
	return NULL;
}
void ListInsert(ListNode*& phead, ListNode* pos, int x)
{
	
	if (phead->data == pos->data)
	{
		ListPushFront(phead, x);
	}
	else
	{
		ListNode* newnode = new ListNode();
		newnode->data = x;
		ListNode* head = phead->end;
		while (phead != head)
		{
			if (head->data == pos->data)
			{
				newnode->begin = head->begin;
				newnode->end = head;
				head->begin->end = newnode;
				head->begin = newnode;
			}
			head = head->end;
		}
	}
	
}
int main()
{
	ListNode* p;
	ListInit(p);
	ListPushBack(p, 1);
	ListPushBack(p, 2);
	ListPushBack(p, 5);
	ListPushBack(p, 6);
	Listprintf(p);
	ListPopFront(p);
	ListPopFront(p);
	ListPopFront(p);
	ListNode*SIX=SListFind(p, 6);
	ListInsert(p, SIX, 22);
	Listprintf(p);
	
}

各个方法如下:

void ListInit(ListNode*& p)//初始化
 


void ListInit(ListNode*& p)//初始化
{
	p = new ListNode();
	p->begin = p;
	p->end = p;
	p->data = 0;
}

void ListPushFront(ListNode*& phead, int x)//头插

void ListPushFront(ListNode*& phead, int x)//头插
{
	ListNode* newnode = new ListNode();
	newnode->data = x;
	if (phead->begin==phead)
	{
		phead->end = newnode;
		phead->begin = newnode;
		newnode->end = phead;
		newnode->begin = phead;
	}
	else
	{
		newnode->end = phead;
		newnode->begin = phead->begin;
		phead->begin->end = newnode;
		phead->begin = newnode;

	}
		
		
	phead = newnode;

}

void Listprintf(ListNode*& phead)//打印
 

void Listprintf(ListNode*& phead)//打印
{
	ListNode* head=phead->end;
	cout << "phead->begin " << phead->begin << "  phead->data  " << phead->data << "  phead->end  " << phead->end << endl;
	while (phead!=head)
	{
		cout << "p->begin=" << head->begin << "  p->data=" << head->data << "  p->end=" << head->end << endl;
		head = head->end;
	}
}

void ListPushBack(ListNode*& phead, int x)//尾插
 

void ListPushBack(ListNode*& phead, int x)//尾插
{
	ListNode* newnode = new ListNode();
	newnode->data = x;
	if (phead->begin == phead)
	{
		phead->end = newnode;
		phead->begin = newnode;
		newnode->end = phead;
		newnode->begin = phead;
	}
	else {
		newnode->begin = phead->begin;
		newnode->end = phead;
		newnode->begin->end = newnode;
		phead->begin = newnode;
	}
}

void ListPopBack(ListNode*& phead)
 

void ListPopBack(ListNode*& phead)
{
	phead->begin = phead->begin->begin;
	phead->begin->end = phead;
}

void ListPopFront(ListNode*& phead)
 

void ListPopFront(ListNode*& phead)
{
	phead->end->begin = phead->begin;
	phead->begin->end = phead->end;
	phead = phead->end;
}

ListNode* SListFind(ListNode*& phead, int x)

ListNode* SListFind(ListNode*& phead, int x)
{
	ListNode* head = phead->end;
	if (phead->data == x)
	{
		return phead;
	}
	while (phead != head)
	{
		if (head->data == x)
		{
			return head;

		}
		head = head->end;
	}
	return NULL;
}

void ListInsert(ListNode*& phead, ListNode* pos, int x)
 

void ListInsert(ListNode*& phead, ListNode* pos, int x)
{
	
	if (phead->data == pos->data)
	{
		ListPushFront(phead, x);
	}
	else
	{
		ListNode* newnode = new ListNode();
		newnode->data = x;
		ListNode* head = phead->end;
		while (phead != head)
		{
			if (head->data == pos->data)
			{
				newnode->begin = head->begin;
				newnode->end = head;
				head->begin->end = newnode;
				head->begin = newnode;
			}
			head = head->end;
		}
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值