Data structure and algorithm - 1.11 delete the even element of the single linked list

From , 3 Years ago, written in C++, viewed 242 times.
URL https://pastebin.vip/view/d8330f85
  1. //***********************************
  2. //功能:删除单链表的第偶数个元素
  3. //日期:2017年10月10日
  4. //作者:Ryan2019
  5. //***********************************
  6. #include <iostream>
  7. using namespace std;
  8. typedef int LElemType;
  9. typedef struct LNode
  10. {
  11.     LElemType data;
  12.         LNode *next;
  13. }* LList;
  14. void ListCreate(LList &L,int n,LElemType a[]);//创建链表
  15. void Listshow(LList &L);//显示链表
  16. void DeleteEven(LList &L);//删除单链表的第偶数个元素
  17.  
  18. int main()
  19. {
  20.         LList A,B,C; int m=5,n=8;
  21.         LElemType a[5]={1,2,3,4,5};
  22.         LElemType b[8]={1,2,3,4,5,6,7,8};
  23.         ListCreate(A,m,a);     
  24.         ListCreate(B,n,b);
  25.         ListCreate(C,0,a);
  26.         cout<<"原链表A为:";Listshow(A);
  27.         DeleteEven(A);
  28.         cout<<"删除第偶数个元素链表为:";   Listshow(A);   
  29.  
  30.         cout<<"原链表B为:";       Listshow(B);
  31.         DeleteEven(B);
  32.         cout<<"删除第偶数个元素链表为:";   Listshow(B);
  33.  
  34.         cout<<"原链表C为:";       Listshow(C);
  35.         DeleteEven(C);
  36.         cout<<"删除第偶数个元素链表为:";   Listshow(C);
  37.  
  38.         return 0;
  39. }
  40.  
  41. void ListCreate(LList &L,int n,LElemType a[])
  42. {
  43.         LList p;        int i;
  44.         L=new LNode; L->next=NULL;
  45.         for(i=n-1;i>=0;i--)
  46.         {
  47.                 p=new LNode;
  48.                 p->data=a[i];
  49.                 p->next=L->next;
  50.                 L->next=p;
  51.         }
  52. }
  53. void Listshow(LList &L)
  54. {
  55.         LList p;
  56.         for(p=L->next;p;p=p->next)
  57.         {
  58.                 cout<<p->data<<" ";
  59.         }
  60.         cout<<endl;
  61. }
  62. void DeleteEven(LList &L)
  63. {
  64.         if(!L || !L->next || !L->next->next)
  65.         {
  66.                 return;
  67.         }
  68.         LList p,q,r;
  69.         p=L->next;
  70.         q=p->next;
  71.         while(p->next && q->next)
  72.         {
  73.                 p->next=q->next;
  74.                 p=p->next;
  75.                 r=q;
  76.                 q=p->next;
  77.                 delete r;
  78.         }
  79.         if(p->next && !q->next)
  80.         {
  81.                 p->next=NULL;
  82.         }
  83. }

Reply to "Data structure and algorithm - 1.11 delete the even element of the single linked list"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website