資料結構(C實現)------- 雙向鏈表

來源:互聯網
上載者:User

標籤:雙向鏈表

         雙向鏈表中的每一個結點都含有兩個指標域,一個指標域存放其後繼結點的儲存地址,另一個指標域則存放其前驅結點的儲存地址。

         雙向鏈表結點的類型描述:

//雙向鏈表的類型描述typedef int ElemType;typedef struct node{ElemType data;struct node *prior,*next;}DuLNode,*DuLinkList;

        其中,prior域存放的是其前驅結點的儲存地址,next域存放的是其後繼結點的儲存地址。

        雙向鏈表有兩個特點:一是可以從兩個方向搜尋某個結點,這使得鏈表的某些操作(如插入和刪除)變得比較簡單; 二是無論利用前鏈還是後鏈都可以遍曆整個雙向鏈表。


        雙向鏈表的操作基本和單鏈表的操作相同;

        1. 頭插法建立帶頭結點的雙向鏈表Create_DLinkListF(int n)

//頭插法建立帶頭結點的雙向鏈表DuLinkList Create_DLinkListF(int n){DuLinkList L,p;int i = n - 1;ElemType x;//建立頭結點L = (DuLinkList)malloc(sizeof(DuLNode));L->prior = NULL;L->next = NULL;//添加第一個結點scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;L->next = p;p->prior = L;p->next = NULL;//加入其他結點while(i > 0){scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;p->next = L->next;L->next->prior = p;p->prior = L;L->next = p;i--;}return L;}
 

         2. 尾插法建立帶頭結點的雙向鏈表Create_DLinkListR(int n)

//尾插法建立帶頭結點的雙向鏈表DuLinkList Create_DLinkListR(int n){DuLinkList L,p,lastNode;int i = n - 1;ElemType x;//建立頭結點L = (DuLinkList)malloc(sizeof(DuLNode));L->prior = NULL;L->next = NULL;//添加第一個結點scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;L->next = p;p->prior = L;p->next = NULL;lastNode = p;//加入其他結點while(i > 0){scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;lastNode->next = p;p->prior = lastNode;p->next = NULL;lastNode = p;i--;}return L;}
       3. 在指定結點之前插入新結點Insert_DLinkListBefore(DuLinkList p,ElemType x)

//在指定結點之前插入新結點void Insert_DLinkListBefore(DuLinkList p,ElemType x){DuLinkList newNode;//判斷結點p之前的結點的合法性:if(p->prior == NULL)printf("結點不合法,不能在該結點之前插入結點\n");else{newNode = (DuLinkList)malloc(sizeof(DuLNode));newNode->data = x;newNode->next = p;p->prior->next = newNode;newNode->prior = p->prior;p->prior = newNode;}}
      4. 在指定結點之後插入新結點Insert_DLinkListAfter(DuLinkList p,ElemType x)

//在指定結點之後插入新結點void Insert_DLinkListAfter(DuLinkList p,ElemType x){DuLinkList newNode;newNode = (DuLinkList)malloc(sizeof(DuLNode));newNode->data = x;//當插入位置是最後一個結點之後時if(p->next == NULL){p->next = newNode;newNode->prior = p;newNode->next = NULL;}else{newNode->next = p->next;p->next->prior = newNode;p->next = newNode;newNode->prior = p;}}
      5. 刪除指定結點Delete_DLinkList(DuLinkList p)

//刪除指定結點void Delete_DLinkList(DuLinkList p){//如果刪除的是最後一個元素if(p->next == NULL)p->prior->next = NULL;else{p->prior->next = p->next;p->next->prior = p->prior;}free(p);}
      6. 後鏈輸出雙向鏈表Print_DLinkListN(DuLinkList L)

//後鏈輸出雙向鏈表void Print_DLinkListN(DuLinkList p){while(p != NULL){printf("%d\t",p->data);p = p->next;}printf("\n");}
      7.前鏈輸出雙向鏈表Print_DLinkListP(DuLinkList p)

//前鏈輸出雙向鏈表void Print_DLinkListP(DuLinkList p){while(p != NULL){printf("%d\t",p->data);p = p-prior;}printf("\n");}
       

       至於雙向鏈表的其他動作,如定位,和單鏈表的操作類同,不再贅述。







資料結構(C實現)------- 雙向鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.