关于环形链表的建立,删除,插入操作演示

来源:互联网 发布:c语言字符个数统计 编辑:程序博客网 时间:2024/06/16 17:37
  1. #include<iostream>  
  2. using namespace std;  
  3. struct node{  
  4.     int  x;  
  5.     struct node *next,*pre;  
  6.     node(){  
  7.         x=0;next=NULL;pre=NULL;  
  8.     }  
  9. };  
  10. node *h,*p,*q;  
  11. int main(){  
  12.     int i,j,k,m,n,c;  
  13.     cin>>n>>m>>c;  
  14.     h=new node;  
  15.     h->x=1;  
  16.     p=h;  
  17.     for(i=2;i<=n;i++){  
  18.         q=new node;  
  19.         q->x=i;  
  20.         p->next=q;  
  21.         q->pre=p;  
  22.         p=q;                  
  23.     }  
  24.     p->next=h;h->pre=p;  
  25.     p=h;  
  26.     while(p->x!=c)p=p->next;  
  27.     q=new node;q->x=n+1;  
  28.     q->next=p->next;p->next->pre=q;  
  29.     p->next=q;q->pre=p;  
  30.       
  31.     k=1;  
  32.     p=h;  
  33.     while(p->next!=p){  
  34.         p=p->next;  
  35.         k++;  
  36.         if(k==m){  
  37.             q=p;  
  38.             cout<<p->x<<" ";  
  39.             p->next->pre=p->pre;  
  40.             p->pre->next=p->next;            
  41.             p=p->next;  
  42.             delete(q);  
  43.             k=1;  
  44.         }  
  45.     }  
  46.     cout<<endl;  
  47.     cout<<p->x;  
  48.     return 0;  
  49. }
0 0
原创粉丝点击