小标
2018-06-25
来源 :
阅读 2325
评论 0
摘要:在C++语言中,下面是基本的双链表操作,由于双链表有两个方向,所以在删除和插入节点时,可以节省一个指针,只用一个链表上的指针和一个待操作的指针即可完成插入和删除;同时也要注意在编写双链表时对情况的判断要仔细,希望对大家学习C++语言有所帮助。
在C++语言中,下面是基本的双链表操作,由于双链表有两个方向,所以在删除和插入节点时,可以节省一个指针,只用一个链表上的指针和一个待操作的指针即可完成插入和删除;同时也要注意在编写双链表时对情况的判断要仔细,希望对大家学习C++语言有所帮助。否则很容易出错~
[cpp] view plain copy
1. #include<iostream>
2. using namespace std;
3.
4. typedef struct student
5. {
6. int data;
7. struct student *next;
8. struct student *pre;
9. }dnode;
10.
11. //建立双链表
12. dnode *create()
13. {
14. dnode *head, *p, *s;
15. int x;
16. head = (dnode *)malloc(sizeof(dnode));
17. p = head;
18. cout<<"\nInput the data : ";
19. while(1)
20. {
21. if(scanf("%d", &x) != EOF && x != 0)
22. {
23. s = (dnode *)malloc(sizeof(dnode));
24. s->data = x;
25. p->next = s;
26. s->pre = p;
27. p = s;
28. }
29. else break;
30. }
31. head = head->next;
32. head->pre = NULL;
33. p->next = NULL;
34. return head;
35. }
36.
37. //双链表删除节点
38. dnode *del(dnode *head, int num)
39. {
40. dnode *p1;
41. p1 = head;
42. while(num != p1->data && p1->next != NULL)
43. p1 = p1->next;
44. if(num == p1->data)
45. {
46. if(head == p1)
47. {
48. head = head->next;
49. head->pre = NULL;
50. }/*此处的处理与单链表不同,因为双链表有两个方向,所以在定义时仅定义一个指针,
51. 这使得当出现删除链表尾部时,由于NULL没有前驱,导致无法连接,所以要分开讨论。*/
52. else if(p1->next == NULL) p1->pre->next = NULL;
53. else
54. {//p1为最后一个节点,如没有上一个分支,则第二个语句会出错。
55. p1->pre->next = p1->next;
56. p1->next->pre = p1->pre;
57. }
58. free(p1); //释放
59. }
60. else cout<<"There is no %d "<<num;
61. return head;
62. }
63.
64. //双链表插入节点
65. dnode *insert(dnode *head, int num)
66. {
67. dnode *p0, *p1;
68. p1 = head;
69. p0 = (dnode *)malloc(sizeof(dnode));
70. p0->data = num;
71. while(num > p1->data && p1->next != NULL)
72. p1 = p1->next;
73. if(num <= p1->data)
74. {
75. if(head == p1)
76. {
77. p0->next = p1;
78. p1->pre = p0;
79. head = p0;
80. }
81. else
82. {
83. p1->pre->next = p0;
84. p0->pre = p1->pre;
85. p0->next = p1;
86. p1->pre = p0;
87. }
88. }
89. else
90. {//插入尾部~
91. p1->next = p0;
92. p0->next = NULL;
93. p0->pre = p1;
94. }
95. return head;
96. }
97.
98. //计算表长
99. int length(dnode *head)
100. {
101. int n = 0;
102. while(head != NULL)
103. {
104. head = head->next;
105. n++;
106. }
107. return n;
108. }
109. //打印双链表
110. void print(dnode *head)
111. {
112. int n = length(head);
113. cout<<"Output the DoubleList ("<<n<<" records~) : ";
114. while(head != NULL)
115. {
116. if(head->next == NULL) cout<<head->data<<endl;
117. else cout<<head->data<<"<->";
118. head = head->next;
119. }
120. }
121. //排序函数同单链表,此处省略了~,默认输入为递增哈~
122. int main()
123. {
124. dnode *head;
125. //创建
126. head = create();
127. print(head);
128. //删除
129. int numD;
130. cout<<"\nDelete : ";
131. cin>>numD;
132. print(del(head, numD));
133. //插入
134. int numS;
135. cout<<"\nInsert : ";
136. cin>>numS;
137. print(insert(head, numS));
138. return 0;
139. } 
以上就介绍了C/C+的相关知识,希望对C/C+有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C/C+频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号