ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 循环双链表的简单操作

循环双链表的简单操作

原创 Linux操作系统 作者:Diy_os 时间:2015-10-16 01:06:29 0 删除 编辑
下面是循环双链表的简单操作,其中很多细节没有考虑到,比如没有判断空链表,目的提供思想,如有出现错误,请大家指正!

点击(此处)折叠或打开

  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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-12-11

  • 博文量
    159
  • 访问量
    988946