双向链表的相关操作C++实现

双向链表的相关操作C++实现

对于循环双向链表

判断一个链表是否为空的条件为:head->next==head (头指针)

判断*p为最后一个节点的条件为:p->next=head

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<stdlib.h>
  3. using namespace std;  
  4.   
  5. /*双链表结构*/  
  6. typedef struct node  
  7. {  
  8.     int data;  
  9.     struct node *prior;  
  10.     struct node *next;  
  11.   
  12. }DNode;   
  13. /*创建一个带头节点的双链表*/  
  14. void CreateDLink(DNode *&head)  
  15. {  
  16.     int x;  
  17.     DNode *p,*s;  
  18.     s=(DNode *)malloc(sizeof(DNode));  
  19.     if(s==NULL)  
  20.     {  
  21.         cout<<"Fail to malloc the head node!"<<endl;  
  22.           
  23.     }  
  24.        s->data=0;  
  25.         s->prior=NULL;  
  26.         s->next=NULL; //建立一个头结点为head的双链表  
  27.     head=s;   //若是循环双向链表则为:s->next=s->prior=s;  
  28.     cout<<"please input the data of the node"<<endl;  
  29.   cin>>x;  
  30.     while(x!=0)  
  31.     {  
  32.       p=(DNode *)malloc(sizeof(DNode));  
  33.      if(p==NULL)  
  34.      {  
  35.         cout<<"Fail to malloc a new  node!"<<endl;  
  36.           
  37.      }  
  38.        
  39.      p->data=x;  
  40.      s->next=p;  
  41.      p->prior=s;  
  42.      p->next=NULL;  
  43.      s=p;  
  44.   
  45.        
  46.     cin>>x;  
  47.     }  
  48.   
  49. }  
  50.   
  51. void PrintLink(DNode *head)  
  52. {  
  53.     if(head->next==NULL)  
  54.     {  
  55.         cout<<"The list is NULL"<<endl;  
  56.         return ;  
  57.     }  
  58.     DNode *p;  
  59.     p=head->next;  
  60.     while(p!=NULL)  
  61.     {  
  62.         cout<<p->data<<" ";  
  63.         p=p->next;  
  64.           
  65.     }  
  66.    cout<<endl;  
  67. }  
  68. int Length(DNode *head) //求双链表长度  
  69. {  
  70.     int len=0;  
  71.     DNode * p=head->next;  
  72.     while(p!=NULL)  
  73.     {  
  74.         len++;  
  75.         p=p->next;  
  76.     }  
  77.     return len;  
  78.   
  79. }  
  80. DNode * Get(DNode *head,int i) //获取链表第i个节点地址  
  81. {  
  82.     int j=0;  
  83.     DNode *p=head->next;  
  84.     while(j<i&&p!=NULL)  
  85.     {  
  86.         p=p->next;  
  87.         j++;  
  88.     }  
  89.     if(p!=NULL)  
  90.         return p;  
  91.     else   
  92.         return NULL;  
  93. }  
  94. int InsertNode(DNode *head,int x,int i)//在第i个位置插入一个节点  
  95. {  
  96.     DNode *s,*p;  
  97.     s=(DNode *)malloc(sizeof(DNode));  
  98.     s->data=x;  
  99.     if(i==0)  
  100.     {  
  101.         s->next=head->next;  
  102.         if(head->next!=NULL)  
  103.             head->next->prior=s;  
  104.         s->prior=head;  
  105.         head->next=s;  
  106.           
  107.     }  
  108.     else  
  109.     {  
  110.         p=Get(head,i-1); //查找待插入节点前一个位置  
  111.         if(p==NULL)  
  112.             return 0;  
  113.         else  
  114.         {  
  115.             s->next=p->next;  
  116.             if(p->next!=NULL)  
  117.                 p->next->prior=s;  
  118.             s->prior=p;  
  119.             p->next=s;  
  120.         }  
  121.     }  
  122.   
  123.     return 1;  
  124. }  
  125.   
  126. void DeleteNode(DNode *head,int index) //删除第index个节点  
  127. {  
  128.     if(head->next==NULL)  
  129.     {  
  130.         cout<<"The list is NULL"<<endl;  
  131.         return;  
  132.     }  
  133.     DNode *p,*s/*保存待删除的节点*/;  
  134.     if(index==0)  
  135.     {  
  136.         s=head->next;  
  137.         head->next=s->next;  
  138.         if(s->next!=NULL)  
  139.             s->next->prior=head;  
  140.          free(s);  
  141.     }  
  142.     else  
  143.     {  
  144.         p=Get(head,index-1);  
  145.         if(p==NULL)  
  146.             cout<<"The Node "<<index<<"is not exist"<<endl;  
  147.         else  
  148.         {  
  149.             s=p->next;  
  150.             p->next=s->next;  
  151.             if(s->next!=NULL)  
  152.                 s->next->prior=p;  
  153.             free(s);  
  154.         }  
  155.     }  
  156.   
  157. }  
  158. int main(int argc,char *argv[])  
  159. {  
  160.   DNode *head;  
  161.   CreateDLink(head);   
  162.   cout<<"链表长度为:"<<Length(head)<<endl;  
  163.    PrintLink(head);  
  164.    cout<<"第3个节点的值为:"<<Get(head,3)->data<<endl;  
  165.    
  166.    cout<<"输入插入节点的位置及值:"<<endl;  
  167.     int value, index;  
  168.     cin>>index>>value;  
  169.    InsertNode(head,value,index);  
  170.    cout<<"插入后的链表为:"<<endl;  
  171.        PrintLink(head);  
  172.   
  173.        cout<<"请输入要删除的节点位置:"<<endl;  
  174.        cin>>index;  
  175.        DeleteNode(head,index);  
  176.    PrintLink(head);  
  177.     return 0;  
  178. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值