c語言實現--雙向循環鏈表操作


1,雙向鏈表相當於兩個單向循環鏈表。

2,雙向鏈表的結點定義。

1 struct DULNode
2 {
3     int data;
4     struct DULNode * prior;
5     struct DULNode * next;
6 };
7 
8 typedef struct DULNode * linklist;

3,單循環鏈表的操作都適用於雙循環鏈表。
4,雙循環鏈表的操作集合仍在頭文件defs.h中。

5,InitList操作。雙循環鏈表初始化操作示意圖

 1 #include"defs.h"
 2 
 3 void InitList(linklist *L) //改變頭指針
 4 {
 5     *L = (linklist)malloc(sizeof(struct DULNode));
 6     if (*L == NULL)
 7         exit(0);
 8      (*L)->next = *L;
 9      (*L)->prior = *L; 
10 }

6,ClearList操作.

 1 #include"defs.h"
 2 
 3 void ClearList(linklist L)
 4 {
 5     linklist p = L->next;  //p指向鏈表第一個元素
 6    
 7     while (p != L)
 8      {
 9           p = p->next;  //p指向第二個元素
10         free(p->prior);
11      }
12      (*L)->next = *L;
13      (*L)->prior = *L;
14 }
ClearList.c

7,DestroyList操作

1 #include"defs.h"
2 
3 void DestroyList(linklist *L)
4 {
5     ClearList(*L);
6     free(*L);
7     *L = NULL;
8 }
DestroyList.c

8,ListEmpty操作

1 #include"defs.h"
2 
3 int ListEmpty(linklist L)
4 {
5     if (L->next == L && L->prior == L) //兩個條件均要有
6         return 0;
7     else
8         return 1;
9 }
ListEmpty.c

9,ListLength操作

 1 #include"defs.h"
 2 
 3 int ListLength(linklist L)
 4 {
 5     linklist p = L->next;
 6     int j = 0;
 7     
 8     while (p != L)
 9     {
10         ++j;
11         p = p->next;
12     }
13     return j;
14 }
ListLength.c

10,GetElem操作

 1 #include"defs.h"
 2 
 3 int GetElem(linklist L, int i, int *e)
 4 {
 5      linklist p = L;
 6      int j = 0;
 7      if (i<1 || i>ListLength(L))
 8          exit(0);
 9      
10      while (j<i)   //找到第i個結點
11      {
12           ++j;
13           p = p->next;
14      }
15      *e = p->data;
16 }
GetElem.c

11,LocateElem操作, 沒有找到返回-1

 1 #include"defs.h"
 2 
 3 int LocateElem(linklist L, int e)
 4 {
 5     linklist p = L->next;
 6     int j = 0;
 7 
 8     while (p != L)
 9     {
10         ++j;
11         if (p->data == e)
12              return j;
13     }
14     return -1;  
15 }
LocateElem.c

12,PriorElem操作

 1 #include"defs.h"
 2 
 3 int PriorElem(linklist L, int cur_e, int *pri_e)
 4 {
 5     linklist p = L->next->next;  //p指向第二個元素
 6  
 7    while (p != L)
 8     {
 9           if (p->data == cur_e)
10           {
11                 *pri_e = p->prior->data;
12                 return 0;
13           }
14     }
15     return 0;
16 }
PriorElem.c

13,NextElem操作

 1 #include"defs.h"
 2 
 3 int NextElem(linklist L, int cur_e, int *nex_e)
 4 {
 5     linklist p = L->next->next; 
 6     
 7      while (p != L)
 8      {
 9              if (p->prior->data == cur_e)
10              {
11                       *nex_e = p->data;
12                        return 0;
13              }
14      }
15      return 0;
16 }
NextElem.c

14,ListInsert操作

 1 #include"defs.h"
 2 
 3 int ListInsert(linklist L, int i, int e)
 4 {
 5       linklist p = L; //p指向頭結點
 6      linklist q, s;
 7       int j = 0;
 8       if (i<1 || i>ListLength(L)+1)
 9          exit(0);
10   
11        while (j < i-1) //找到第i-1個結點
12      {
13            ++j;
14            p = p->next;
15        }
16        q = p->next;  //q指向第i個結點
17      
18      s = (linklist)malloc(sizeof(struct DULNode));
19       s->data = e;
20       
21       s->next = q;  //先改變向右的指針
22       p->next = s;   
23       s->prior = p;  //改變向左的指針
24       q->prior = s;
25       return 0;
26 }
ListInsert.c

15,ListDelete操作

 1 #include"defs.h"
 2 
 3 int ListDelete(linklist L, int i, int *e)
 4 {
 5     linklist p = L;
 6     linklist q;
 7     int j = 0;
 8     if (i<1 || i>ListLength(L))  //位置是否合理
 9         exit(0);
10     
11     while (j < i-1)    //找到第i-1個元素
12     {
13          ++j;
14          p = p->next;
15     }
16     q = p->next;  //p指向第i個結點
17    *e = q->data;
18     
19      p->next = q->next;
20      q->next->prior = p;
21      free(q);
22      return 0;
23 }
ListDelete.c

16,TravelList操作

 1 #include"defs.h"
 2 
 3 void TravelList(linklist L)
 4 {
 5     linklist p = L->next;
 6     int j = 0;
 7     while (p != L)
 8     {
 9           ++j;
10           printf("第%d個元素值為:%d\n", j, p->data);
11           p = p->next;
12     }
13 }
TravelList.c

17,TravelListBack操作,逆序輸出表中元素

 1 #include"defs.h"
 2 
 3 void TravelListBack(linklist L)
 4 {
 5      linklist p = L->prior;
 6      while (p != L)
 7      {
 8           printf("%d ", p->data);
 9           p = p->prior;
10      }
11      printf("\n");
12 }
TravelListBack.c

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM