数据结构 c代码3:双向单链表

下面是用c语言实现的一些双向单链表的基本操作:

  1 #include<iostream>
  2 using namespace std;
  3 typedef int Status;
  4 typedef int ElemType;
  5 #define OVERFLOW -1
  6 #define ERROR 0
  7 #define OK 1
  8 
  9 /*双向链表的存储结构*/
 10 typedef struct DuLNode
 11 {
 12     ElemType elem;
 13     struct DuLNode *prior, *next;
 14 }DuLNode, *DuLinkList;
 15 
 16 
 17 
 18 /*头插创建一个双向链表*/
 19 void CreatList_Head_Dul(DuLinkList &L, int n)
 20 {
 21     L = (DuLNode *) malloc(sizeof(DuLNode));
 22     L->next = NULL;
 23     L->prior = NULL;
 24 
 25     
 26     int elem;
 27     for(int i=1; i<=n; i++)
 28     {
 29         cin>>elem;
 30         DuLNode * temp = (DuLNode *)malloc(sizeof(DuLNode));
 31         temp->elem = elem;
 32         
 33         if(i==1)
 34         {
 35             temp->next = L->next;
 36             temp->prior = L;
 37             L->next = temp;
 38         }
 39         else
 40         {
 41             temp->next = L->next;
 42             L->next->prior = temp;
 43             temp->prior = L;
 44             L->next = temp;    
 45         }
 46         
 47     } 
 48 }
 49 
 50 /*尾插法建立一个双向链表*/
 51 void CreatList_Tail_Dul(DuLinkList &L, int n)
 52 {
 53     L = (DuLNode *)malloc(sizeof(DuLNode));
 54     L->next = NULL;
 55     L->prior = NULL;
 56 
 57     int elem;
 58     DuLinkList p = L;
 59     for(int i=1;i<=n;i++)
 60     {
 61         cin>>elem;
 62         DuLNode *temp = (DuLNode *)malloc(sizeof(DuLNode));
 63         temp->elem = elem;
 64         temp->next = p->next;
 65         temp->prior = p;
 66         p->next = temp; 
 67         p = temp;
 68     }
 69     
 70 }
 71 
 72 DuLNode *GetElem_Dul(DuLinkList L, int pos)
 73 {
 74     DuLNode *p = L;
 75     p = p->next;
 76     int j = 1;
 77     while(p && (j < pos))
 78     {
 79         p = p->next;
 80         j++;
 81     }
 82     
 83     if(!p || (j>pos))
 84         return NULL;
 85     else
 86         return p;
 87 }
 88 
 89 /*在单链表的第i个节点前插入一个节点值为e的正整数*/
 90 Status ListInsert_Dul(DuLinkList &L,int pos, ElemType e)
 91 {
 92     DuLinkList p;
 93     if(!(p=GetElem_Dul(L,pos)))
 94         return ERROR;
 95     DuLinkList temp = (DuLNode *)malloc(sizeof(DuLNode));
 96     temp->elem = e;
 97     temp->prior = p->prior;
 98     p->prior->next = temp;
 99     temp->next = p;
100     p->prior = temp;
101     return OK;
102 } 
103 
104 Status ListDelete_Dul(DuLinkList &L, int pos)
105 {
106     DuLinkList p;
107     if(!(p = GetElem_Dul(L,pos)))
108         return ERROR;
109     
110     p->next->prior = p->prior;
111     p->prior->next = p->next;
112     free(p);
113     return OK;
114 }
115 
116 /*顺序输出双向链表的数据*/
117 void ShowList_Dul(DuLinkList L)
118 {
119     DuLinkList temp;
120     temp = L;
121     while(temp->next!=NULL)
122     {
123         temp = temp->next;
124         cout<<temp->elem<<"->";
125     }
126     cout<<"NULL"<<"\n";
127 }
128 
129 int main()
130 {
131 
132         DuLinkList L;
133         cout<<"------------------------双向链表菜单----------------------"<<'\n';
134         cout<<"操作0:退出程序"<<'\n';
135         cout<<"操作1:创建一个带头结点的双向链表,且遍历此链表"<<'\n';
136         cout<<"操作2:在双向链表中的第i个结点前插入一个结点值为e的正整数"<<'\n';
137         cout<<"操作3:删除双向链表链表中的第j个结点"<<'\n';
138         int n, a, flag = 1;
139         while(flag!=0)
140         {
141             cout<<"\n"<<"请选择要执行的操作:";
142             while(cin>>a)
143             {
144                 if(a<0||a>3)
145                     cout<<"请选择正确的编号:";
146                 else
147                     break;
148             }
149             
150             switch (a) 
151             {
152                 case 0:
153                 {
154                     cout<<"感谢使用本系统!!"<<"\n";
155                     flag = 0;
156                     break;
157                 }
158                 case 1:
159                 {    
160                     cout<<"----菜单-----"<<'\n';
161                     cout<<"操作1:前插法"<<'\n';
162                     cout<<"操作2:后插法"<<'\n';
163                     cout<<"-------------"<<'\n';
164                     cout<<"请选择单链表的创建方式:";
165                     int b;
166                     while(cin>>b)
167                     {
168                         if(b!=1&&b!=2)
169                             cout<<"请输入正确操作编号:";
170                         else
171                             break;
172                     }
173                     cout<<"请输入要建立双向单链表的元素个数:";
174                     cin>>n;
175                     if(b == 1)
176                     {
177                         cout<<"请按照逆位序输入"<<n<<"个元素:";
178                         CreatList_Head_Dul(L, n);         //头插建立双向单链表 
179                     } 
180                     else
181                     {
182                         cout<<"请按照正位序输入"<<n<<"个元素:";  //尾插法建立双向单链表 
183                         CreatList_Tail_Dul(L,n);
184                     } 
185                     
186                     cout<<"双向链表创建完毕!\n";
187                     ShowList_Dul(L);    //输出双链表的数据 
188                     break;
189                 }
190                 case 2:
191                 {
192                     int i;
193                     ElemType e;
194                     cout<<"在单链表的第i个节点前插入一个节点值为e的正整数,请依次输入i和e的值:";
195                     cin>>i>>e;
196                     if(ListInsert_Dul(L, i, e))
197                     {
198                         cout<<"单链表插入完成!\n"; 
199                         ShowList_Dul(L);
200                     }
201                     else
202                     {
203                         cout<<"i值的输入不合法!";
204                     }
205                     break;
206                 }
207                     
208                 case 3:
209                 {
210                     int j;
211                     cout<<"删除单链表中的第j个节点,请输入j的值:";
212                     cin>>j;
213                     if(ListDelete_Dul(L,j))
214                     {
215                         cout<<"单链表删除完毕!"<<"\n";
216                         ShowList_Dul(L);
217                     }
218                     else
219                         cout<<"j值不合法!"<<"\n";
220                     break;
221                 }
222                 
223             }
224         }
225         
226         system("pause");
227         return 0;
228 }

有不懂的可以在下方留言,如果对您有所帮助,请帮忙点个赞!!!!

posted @ 2021-12-06 14:02  快乐的余开心  阅读(46)  评论(0)    收藏  举报
!--点击火花特效-->