双链表基本操作

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。但是双链表就是很费空间,优点就是它能够正反访问数据。

if 1
#include<iostream>
using namespace std;
typedef struct Node
{
    int data;
    struct Node*next;
    struct Node*pre;

}*node;
node insertHeaddata(node pHead)
{
    node q = pHead;
    //p = pHead->next;
    for (int i = 0; i < 3; i++)
    {
        node p = (node)malloc(sizeof(struct Node));

        //链接后面 
        q->next->pre = p;  // 1
        p->next = q->next;  // 2

        //断开 q   链接前面
        q->next = p;  //3
        p->pre = q;  //4
        p->data = i;
    }

    
    return pHead;
}
void showNode(node head)//打印全部节点的信息
{
    node p = head->next;
    while (p != head)
    {
        cout << p->data << ' ';
        p = p->next;
    }
}
int main()
{
    node pHead=(node)malloc(sizeof(node));
    pHead->next = pHead;
    pHead->pre = pHead;
    insertHeaddata(pHead);
    showNode(pHead);
    system("pause");
    return 0;
}
#endif

 

头插法

#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
	struct node*pre;
}*NODE;
void insertData(NODE pHead)//头插法
{
	pHead->next = pHead;
	pHead->pre = pHead;
	int data;
	cout << "请输入数据,以0结束";
	cin >> data;
	while(data!=0)
	{
		NODE temp = (NODE)malloc(sizeof(struct node));
		temp->pre = pHead;//1
		temp->next = pHead->next;//2
		temp->pre->next = temp;//3
		temp->next->pre = temp;//4
		temp->data = data;
		cin >> data;
	}
}
void showData(NODE pHead)
{
	NODE p = pHead->next;
	while (p != pHead)
	{
		cout << p->data << " ";
		p = p->next;
	}
}
int main()
{
	NODE pHead=(NODE)malloc(sizeof(struct node));;
	insertData(pHead);
	showData(pHead);
	system("pause");
}

 

尾插法: 

#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
	struct node*pre;
}*NODE;
void insertData1(NODE pHead)//尾插法
{
	pHead->next = pHead;
	pHead->pre = pHead;
	int data;
	cout << "请输入数据,以0结束";
	cin >> data;
	while (data != 0)
	{
		NODE temp = (NODE)malloc(sizeof(struct node));
		temp->pre = pHead->pre;
		temp->next = pHead;
		temp->next->pre = temp;
		temp->pre->next = temp;
		temp->data = data;
		cin >> data;
	}
}
void showData(NODE pHead)
{
	NODE p = pHead->next;
	while (p != pHead)
	{
		cout << p->data << " ";
		p = p->next;
	}
}
int main()
{
	NODE pHead=(NODE)malloc(sizeof(struct node));;
	insertData1(pHead);
	showData(pHead);
	system("pause");
}

 

链表的删除 

#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node*next;
	struct node*pre;
}*NODE;
void insertData1(NODE pHead)//尾插法
{
	pHead->next = pHead;
	pHead->pre = pHead;
	int data;
	cout << "请输入数据,以0结束";
	cin >> data;
	while (data != 0)
	{
		NODE temp = (NODE)malloc(sizeof(struct node));
		temp->pre = pHead->pre;
		temp->next = pHead;
		temp->next->pre = temp;
		temp->pre->next = temp;
		temp->data = data;
		cin >> data;
	}
}
void deleData(NODE pHead)
{
	cout << "请输入删除的数据";
	NODE p = pHead->next;
	NODE q,ph;
	int data;
	cin >> data;
	while (p != pHead)
	{
		if (p->data == data)
		{
			q= p;
			q->pre->next = q->next;
			q->next->pre = q->pre;
			p = q->next;//为什么不能用p=p->next???
			free(q);
			continue;
		}
		p = p->next;
	}
}
void showData(NODE pHead)
{
	NODE p = pHead->next;
	while (p != pHead)
	{
		cout << p->data << " ";
		p = p->next;
	}
}
int main()
{
	NODE pHead=(NODE)malloc(sizeof(struct node));;
	insertData1(pHead);
	showData(pHead);
	deleData(pHead);
	showData(pHead);
	system("pause");
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值