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


3388

被折叠的 条评论
为什么被折叠?



