迴圈雙連結串列的簡單操作

Diy_os發表於2015-10-16
下面是迴圈雙連結串列的簡單操作,其中很多細節沒有考慮到,比如沒有判斷空連結串列,目的提供思想,如有出現錯誤,請大家指正!

點選(此處)摺疊或開啟

  1. #include<iostream>
  2. //#include<stdlib.h>
  3. using namespace std;
  4. struct List {

  5.     int data;
  6.     struct List *last, *next;
  7. };

  8. List *CreateList() {

  9.     List *first;
  10.     first = new List; //first = (List *)malloc(sizeof(List));
  11.     first->next = first;
  12.     first->last = first;
  13.     return first;
  14. }

  15. List *InitialList(List *first) {

  16.     List *p, *head;
  17.     head = first;
  18.     for (int i = 0; i < 5; i++) {

  19.         p = new List;
  20.         p->data = i;
  21.         p->last = head;
  22.      head->next = p;
  23.         
  24.         head = p;
  25.     }

  26.     first->last = head;
  27.     head->next = first;
  28.     return first;
  29. }

  30.     void PrintList(List *first){
  31.         List *q = first;
  32.         first = first->next;
  33.         cout << "正序列印:";
  34.         for (; first != q; first = first->next) {

  35.             cout << first->data << " ";
  36.         }
  37.         
  38.         cout << endl;

  39.         List *p = first;
  40.         first = first->last;
  41.         cout << "逆序列印:";
  42.         for (; first != p; first = first->last) {

  43.             cout << first->data << " ";
  44.         }
  45.     }

  46.     void InsertList(List *first,int i,int x) {

  47.         List *head, *p;
  48.         head = first;

  49.         for (int j = 0; j < i - 1; j++) {

  50.             head = head->next;
  51.         }
  52.         
  53.         p = new List;
  54.         p->data = x;
  55.         p->last = head;
  56.         p->next = head->next; //P
  57.         head->next->last = p;  
  58.         head->next = p;  //該過程操作有順序,該步一定要在P之後,否則會覆蓋head ->next

  59.     }

  60.     void DeleteList(List *first, int i) {

  61.         List *head, *p;
  62.         head = first;

  63.         for (int j = 0; j < i; j++) {

  64.             head = head->next;
  65.         }

  66.         p = head;
  67.         head->last->next = head->next;   //由於是迴圈雙連結串列,可以直接以刪除點為中心操作
  68.         head->next->last = head->last;
  69.         delete p;
  70.         p = NULL;

  71.         first = first->next;

  72.         for (; !first; first = first->next) {

  73.             cout << first->data << " ";
  74.         }

  75.     }
  76.         void DestoryList(List *first){

  77.             List *p, *head;
  78.             head = first;
  79.             while(head !=first){

  80.                 p = head;
  81.                 head = head ->next;
  82.                 delete p;
  83.                 
  84.             }
  85.             delete first; //一定要銷燬頭指標
  86.             first = NULL; //為了防止出現迷途指標,將頭結點置空
  87.             cout << "連結串列已經銷燬" << endl;
  88.             exit(0);
  89.         }
  90.     
  91.     int main() {

  92.         List *s,*t;
  93.         s = CreateList();
  94.         t = InitialList(s);
  95.         DestoryList(t);
  96.         //InsertList(t, 3, 11);
  97.         //DeleteList(t,4);
  98.         PrintList(t);
  99.     }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29876893/viewspace-1814415/,如需轉載,請註明出處,否則將追究法律責任。

相關文章