《数据结构》双向链表的基本操作

前端之家收集整理的这篇文章主要介绍了《数据结构》双向链表的基本操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

双向链表的操作

双向链表的基本操作包括:判断链表是否为空、计算链表的长度、遍历链表,这3中操作的方法和单链表一样,主要和双向链表中的指向后继的指针有关。

双向链表的插入和删除操作和单链表是有区别的:双向链表的插入和删除操作要修改指向前驱和指向后继的指针。

双向链表的删除删除指定位置 i 的元素

1.判断输入的删除的位置是否合法,1<=i<=表长

2.顺着指向后继的指针域移动指针,找到第i-1个结点

3.令指针q指向第i个结点,

4.修改指针

  1. //双向链表的删除
  2. int ListDelete(DuLinkList &L,int i,int &e){
  3. //删除链表第i个位置上的元素,并用e返回其值
  4. // 1<=i<=表长
  5. struct DuLNode *p;
  6. p=L;
  7. //struct DuLNode *q;
  8. int j=0;
  9. while(p->next&&j<i-1){//找到第i-1个结点
  10. ++j;
  11. p=p->next;
  12. }
  13. if(!(p->next)||j>i-1){
  14. return 0;
  15. }
  16. struct DuLNode *q;
  17. q=new DuLNode;
  18. q=p->next;//用q保存要删除的元素,以便释放
  19. e=q->data;
  20. p->next=q->next;//修改指针
  21. q->next->prior=p;
  22. delete q;
  23. return 1;
  24. }


双向链表的插入:

算法思想和单链表的思想相同,只是修改的指针不同。

  1. //双向链表的插入
  2. int ListInsert(DuLinkList &L,int e){
  3. //在双向链表的第i个位置之前插入元素e
  4. // 1<=i<=表长+1
  5. struct DuLNode *p;
  6. p=L;
  7. int j=0;
  8. while(p->next&&j<i-1){
  9. ++j;
  10. p=p->next;
  11. }
  12. if(!(p->next)||j>i-1){
  13. return 0;
  14. }
  15. struct DuLNode *s;//生成要插入的结点
  16. s=new DuLNode;
  17. s->data=e;
  18. p->next->prior=s;
  19. s->next=p->next;
  20. p->next=s;
  21. s->prior=p;
  22. return 1;
  23. }

具体实现:
  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. #define MAX 100
  5.  
  6. //定义双向链表
  7. typedef struct DuLNode{
  8. int data;
  9. struct DuLNode *prior;
  10. struct DuLNode *next;
  11. }DuLNode,*DuLinkList;
  12.  
  13. //初始化
  14. int InitList(DuLinkList &L){
  15. L=new DuLNode;
  16. L->next=NULL;
  17. L->prior=NULL;
  18. return 1;
  19. }
  20.  
  21. //判断链表是否为空
  22. int ListEmpty(DuLinkList L){
  23. if(L->next==NULL){
  24. return 1;//空
  25. }else{
  26. return 0;//非空
  27. }
  28. }
  29.  
  30. //计算链表的长度
  31. int ListLength(DuLinkList L){
  32. int length=0;
  33. struct DuLNode *p;
  34. p=L->next;
  35. while(p){
  36. p=p->next;
  37. ++length;
  38. }
  39. return length;
  40. }
  41.  
  42. //遍历链表
  43. void TraveList(DuLinkList L){
  44. struct DuLNode *p;
  45. p=L->next;
  46. while(p){
  47. printf("%d ",p->data);
  48. p=p->next;
  49. }
  50. printf("\n");
  51. }
  52.  
  53. //头插法创建单链表
  54. void CreateList_F(DuLinkList &L,int n){
  55. L=new DuLNode;
  56. L->next=NULL;
  57. L->prior=NULL;
  58. printf("请输入链表的元素:\n");
  59. for(int i=n;i>0;--i){
  60. printf("请输入第%d个元素的值:",i);
  61. struct DuLNode *s;
  62. s=new DuLNode;
  63. scanf("%d",&s->data);
  64. s->next=L->next;
  65. if(L->next!=NULL)
  66. L->next->prior=s;
  67. s->prior=L;
  68. L->next=s;
  69. }
  70. }
  71.  
  72. //尾差法创建单链表
  73. void CreateList_L(DuLinkList &L,int n){
  74. L=new DuLNode;
  75. L->next=NULL;
  76. L->prior=NULL;
  77. struct DuLNode *p;
  78. p=L;
  79. printf("请输入链表的元素:\n");
  80. for(int i=0;i<n;i++){
  81. printf("请输入第%d个要元素的值:",i+1);
  82. struct DuLNode *s;
  83. s=new DuLNode;
  84. scanf("%d",&s->data);
  85. p->next=s;
  86. s->prior=p;
  87. p=s;
  88. }
  89. }
  90.  
  91. //双向链表的删除
  92. int ListDelete(DuLinkList &L,并用e返回其值
  93. // 1<=i<=表长
  94. struct DuLNode *p;
  95. p=L;
  96. //struct DuLNode *q;
  97. int j=0;
  98. while(p->next&&j<i-1){//找到第i-1个结点
  99. ++j;
  100. p=p->next;
  101. }
  102. if(!(p->next)||j>i-1){
  103. return 0;
  104. }
  105. struct DuLNode *q;
  106. q=new DuLNode;
  107. q=p->next;//用q保存要删除的元素,以便释放
  108. e=q->data;
  109. p->next=q->next;//修改指针
  110. q->next->prior=p;
  111. delete q;
  112. return 1;
  113. }
  114.  
  115. //双向链表的插入
  116. int ListInsert(DuLinkList &L,int e){
  117. //在双向链表的第i个位置之前插入元素e
  118. // 1<=i<=表长+1
  119. struct DuLNode *p;
  120. p=L;
  121. int j=0;
  122. while(p->next&&j<i-1){
  123. ++j;
  124. p=p->next;
  125. }
  126. if(!(p->next)||j>i-1){
  127. return 0;
  128. }
  129. struct DuLNode *s;//生成要插入的结点
  130. s=new DuLNode;
  131. s->data=e;
  132. p->next->prior=s;
  133. s->next=p->next;
  134. p->next=s;
  135. s->prior=p;
  136. return 1;
  137. }
  138.  
  139. int main(){
  140. DuLinkList L;
  141. if(InitList(L)){
  142. printf("链表初始化成功!\n");
  143. }else{
  144. printf("链表初始化失败!\n");
  145. }
  146. if(ListEmpty(L)){
  147. printf("链表为空!\n");
  148. }else{
  149. printf("链表非空!\n");
  150. }
  151. printf("请输入链表的长度:");
  152. int n;
  153. scanf("%d",&n);
  154. //CreateList_L(L,n);
  155. CreateList_F(L,n);
  156. printf("遍历链表:\n");
  157. TraveList(L);
  158. printf("请输入要删除元素的位置:");
  159. int location1;
  160. scanf("%d",&location1);
  161. int e;
  162. if(ListDelete(L,location1,e)){
  163. printf("删除成功!\n");
  164. }else{
  165. printf("删除失败!\n");
  166. }
  167. printf("要删除的元素的值是:%d\n",e);
  168. printf("删除后链表结构:\n");
  169. TraveList(L);
  170. printf("删除后链表长度是:%d\n",ListLength(L));
  171. printf("请输入要插入的位置和元素的值:\n");
  172. int location2,value;
  173. scanf("%d%d",&location2,&value);
  174. if(ListInsert(L,location2,value)){
  175. printf("插入成功!\n");
  176. }else{
  177. printf("插入失败!\n");
  178. }
  179. printf("插入后的链表:\n");
  180. TraveList(L);
  181. printf("插入后链表的长度是:%d\n",ListLength(L));
  182. }

猜你在找的数据结构相关文章