用C++编写的双向链表程序示例

用C++编写的双向链表程序示例
用C++编写的双向链表程序示例

// \*********** 用C++编写一个双链表的程序***********/ //

/*--------- 本文可以帮助大家快速的学习如何建立双向链表,以及对双向链表进行的相关操作

,比如删除节点、插入节点、链表排序、计算链表长度、打印双向链表等操作--------- */

#include

using namespace std;

//定义双向链表的节点

typedef struct student

{

int data;

struct student *pre;

struct student *next;

}dnode;

//建立双向链表

dnode *creat()

{

int cycle = 1;

int x;

dnode *head, *t, *s=NULL;

head = new dnode;

t = head;

while (cycle)

{

cout << "Please input a 'int' type number: " << endl;

cin >> x;

if (x != 0)

{

s = new dnode;

s->data = x;

t->next = s;

s->pre = t;

t = s;

}

else

cycle = 0;

}

head = head->next;

head->pre = NULL;

t->next = NULL;

cout << "第一个节点的值:" << endl ;

cout << head->data << endl << endl;

return head;

}

//计算双向链表的长度

int length(dnode *head)

{

int n=0;

dnode *p;

p = head;

while (p != NULL)

{

n++;

p = p->next;

}

return n;

}

//打印双向链表并计算链表的长度

int print(dnode *head)

{

int n = 0;

dnode *p;

p = head;

while (p != NULL)

{

n++;

cout << p->data << endl;

p = p->next;

}

cout << "Length= " << n << endl;

return n;

}

//实现双向链表中节点的删除

dnode *del(dnode *head)

{

dnode *p;

int num;

p = new dnode;

p = head;

//输入要删除的节点上的值

cout << "Please input the deletating value:" << endl;

cin >> num;

while (num != p->data && p->next != NULL)

{

p = p->next;

}

if (num == p->data)

{

if (p == head)

{

head = head->next;

head->pre = NULL;

delete(p);

}

else if (p->next == NULL)

{

p->pre->next = NULL;

delete(p);

}

else

{

p->pre->next = p->next;

p->next->pre = p->pre;

}

}

else

{

cout << "Don't find the 'num' :" << endl;

}

return head;

}

//双向链表中插入节点

dnode *insert(dnode *head)

{

dnode *p0, *p;

int num;

p = head;

p0 = new dnode;

cout << "Please input the inserting value:" << endl;

cin >> num;

p0->data = num;

while (p0->data > p->data&&p->next != NULL)

{

p = p->next;

}

if (p0->data <= p->data)

{

if (p == head)

{

head = p0;

p0->pre = NULL;

p0->next = p;

p->pre = p0;

}

else

{

p->pre->next = p0;

p0->pre = p->pre;

p0->next = p;

p->pre = p0;

/*p->next = p0;

p0->pre = p;

p0->next = p->next;

p->next->pre = p0;*/

}

}

else

{

p->next = p0;

p0->pre = p;

p0->next = NULL;

}

return head;

}

//对双向链表进行排序

dnode *sort(dnode *head)

dnode *p;

int t,n;

p = head;

n = length(p);

for (int i = 0; i < n - 1; i++)

{

p = head;

for (int j = 0; j < n - 1 - i; j++)

{

if (p->data > p->next->data)

{

t = p->data;

p->data = p->next->data;

p->next->data = t;

}

p = p->next;

}

}

return head;

}

int main()

{

dnode *head;

head = creat();

cout << "创建的双向链表为:" << endl;

print(head);

cout << "head = " << head << endl << endl;

head=del(head);

cout << "删除节点之后的双向链表为:" << endl;

print(head);

head = insert(head);

cout << "插入节点之后的双向链表为:" << endl;

print(head);

head = sort(head);

cout << "排序之后的双向链表为:" << endl;

print(head);

}

/*备注:在进行链表插入时要注意,链表中的值,

、从头节点向后默认是增加的,所以插入的节点位于两个节点中间时,插入节点的位置应该是在p节点之前。如下图所示:*/

实验验证:

创建链表输入:1 ,2,3,65,12,11,70

删除:12

插入:68

最后按照冒泡排序发进行排序。

相关主题
相关文档
最新文档