Advertisement
MaskerQwQ

实验一 单链表

Oct 31st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct LNode{
  6.     int data;
  7.     struct LNode *next;
  8. }LNode,*LinkList;
  9.  
  10.  
  11. //初始化
  12. bool InitList(LinkList &L){
  13.     L=new LNode;
  14.     if(!L){
  15.         return false;
  16.     }
  17.     L->next=NULL;
  18.     return true;
  19. }
  20.  
  21. //头插法
  22. void ListInsert_F(LinkList &L,int fv){
  23.     LinkList s;
  24.     s=new LNode;
  25.     s->data=fv;
  26.     s->next=L->next;
  27.     L->next=s;
  28. }
  29.  
  30. //尾插法
  31. void ListInsert_B(LinkList &L,int bv){
  32.     LinkList s,r;
  33.     r=L;
  34.     while(r->next!=NULL){
  35.         r=r->next;
  36.     }
  37.     s=new LNode;
  38.     s->data=bv;
  39.     s->next=NULL;
  40.     r->next=s;
  41.     r=s;
  42. }
  43.  
  44. //位置插入
  45. bool ListInsert_P(LinkList &L,int i,int e){
  46.     int j;
  47.     LinkList p,s;
  48.     p = L;
  49.     j = 0;
  50.     while(p && j < i - 1)      
  51.     {
  52.         p = p->next;
  53.         j++;
  54.     }
  55.     if(!p || j > i - 1)  
  56.         return false ;
  57.     s = new LNode;        
  58.     s->data = e;          
  59.     s->next = p->next;
  60.     p->next = s;
  61.     return true;
  62. }
  63.  
  64. //删除元素(位置删除)
  65. bool ListDelete_P(LinkList &L,int i){
  66.     LinkList p,q;
  67.     int j = 0;
  68.     p = L;
  69.     while((p->next)&&(j<i-1))  //p的下一个结点存在才能删除
  70.     {
  71.         p=p->next;
  72.         j++;
  73.     }
  74.     if(!(p->next)||(j>i-1)){  //当i > n 或i < 1时删除位置不合理
  75.         return false;
  76.     }
  77.     q=p->next;;
  78.     p->next=q->next;
  79.     delete q;
  80.     return true;
  81. }
  82.  
  83. //删除元素(值删除)
  84. bool ListDelete_V(LinkList &L,int vd){
  85.     if(L->next==NULL){
  86.         cout<<"当前表为空表,没有值可供删除"<<endl;
  87.         return false;
  88.     }
  89.     LNode *cur,*pre;
  90.     pre=L;
  91.     cur=L->next;
  92.     while(cur!=NULL){
  93.         if(cur->data==vd){
  94.             pre->next=cur->next;
  95.             free(cur);
  96.             return true;
  97.         }
  98.         pre=cur;
  99.         cur=cur->next;
  100.     }
  101.     return false;
  102. }
  103. //输出
  104. void ListOutput(LinkList L){
  105.     LinkList p;
  106.     p=L->next;
  107.     if(p==NULL){
  108.         cout<<"当前为空表"<<endl;
  109.     }else{
  110.         cout<<"当前单链表元素为";
  111.         while(p!=NULL){
  112.             cout<<p->data<<" ";
  113.             p=p->next;
  114.         }
  115.         cout<<endl;
  116.     }
  117. }
  118.  
  119. //查找
  120. bool ListSearch(LinkList &L,int sv){
  121.     if(L->next==NULL){
  122.         cout<<"表中没有此元素"<<endl;
  123.         return false;
  124.     }
  125.     int cnt=1;
  126.     LNode *cur,*pre;
  127.     pre=L;
  128.     cur=L->next;
  129.     while(cur!=NULL){
  130.         if(cur->data==sv){
  131.             cout<<"此元素在表中第"<<cnt<<"位"<<endl;
  132.             return true;
  133.         }
  134.         pre=cur;
  135.         cur=cur->next;
  136.         cnt++;
  137.     }
  138.     return false;  
  139. }
  140.  
  141. //求前驱
  142. bool ListFr(LinkList &L,int efr){
  143.     if(L->next==NULL){
  144.         cout<<"表中没有此元素"<<endl;
  145.         return false;
  146.     }
  147.     LNode *cur,*pre;
  148.     pre=L;
  149.     cur=L->next;
  150.     while(cur!=NULL){
  151.         if(cur->data==efr){
  152.             cout<<"此元素的前驱为"<<pre->data<<endl;
  153.             return true;
  154.         }
  155.         pre=cur;
  156.         cur=cur->next;
  157.     }
  158.     return false;  
  159. }
  160. //求后继
  161. bool ListAf(LinkList &L,int eaf){
  162.     if(L->next==NULL){
  163.         cout<<"表中没有此元素"<<endl;
  164.         return false;
  165.     }
  166.     LNode *cur,*pre;
  167.     pre=L;
  168.     cur=L->next;
  169.     while(cur!=NULL){
  170.         if(cur->data==eaf){
  171.             cout<<"此元素的后继为"<<cur->next->data<<endl;
  172.             return true;
  173.         }
  174.         pre=cur;
  175.         cur=cur->next;
  176.     }
  177.     return false;
  178. }
  179. //销毁单链表
  180. bool ListDestory(LinkList &L){
  181.     LinkList p=L;
  182.     while(p){
  183.         L=L->next;
  184.         delete(p);
  185.         p=L;
  186.     }
  187.     cout<<"链表已销毁"<<endl;
  188.     return true;
  189. }
  190.  
  191. //置空表
  192. bool ListEmpty(LinkList &L){
  193.     LinkList p;
  194.     while(L->next){
  195.         p=L->next;
  196.         L->next=p->next;
  197.         delete(p);
  198.     }
  199.     return true;
  200. }
  201.  
  202. //求单链表长度
  203. bool ListLength(LinkList &L){
  204.     int len=0;
  205.     if(L==NULL){
  206.         cout<<"当前为空表,表长为:0"<<endl;
  207.     }else{
  208.         while(L->next){
  209.             len++;
  210.             L=L->next;
  211.         }
  212.         cout<<"当前表长为:"<<len<<endl;
  213.     }
  214.     return true;
  215. }
  216.  
  217. //获取特定位置结点内容
  218. void ListGetData(LinkList &L,int pos){
  219.     LinkList p=L;
  220.     while(pos--){
  221.         p=p->next;
  222.     }
  223.     cout<<"此位置的数据为:"<<p->data<<endl;
  224. }
  225.  
  226. int main(){
  227.    
  228.     LinkList List;
  229.     cout<<"1.建立链表"<<endl;
  230.     cout<<"2.插入元素(头插入)"<<endl;
  231.     cout<<"3.插入元素(尾插入)"<<endl;
  232.     cout<<"4.插入元素(位置插入)"<<endl;
  233.     cout<<"5.删除元素(位置删除)"<<endl;
  234.     cout<<"6.删除元素(值删除)(第一个)"<<endl;
  235.     cout<<"7.查找元素是否在链表中"<<endl;
  236.     cout<<"8.查找元素的前驱"<<endl;
  237.     cout<<"9.查找元素的后继"<<endl; 
  238.     cout<<"10.销毁"<<endl;
  239.     cout<<"11.置空表"<<endl;
  240.     cout<<"12.求表长"<<endl;
  241.     cout<<"13.获取特定位置结点的内容"<<endl;
  242.     cout<<"请输入将要进行的操作编号:"<<endl;
  243.     int option;
  244.     cin>>option;
  245.     while(option!=15){
  246.         switch(option){
  247.             case 1:
  248.                 InitList(List);
  249.                 ListOutput(List);
  250.                 break;
  251.             case 2:
  252.                 int f;
  253.                 cout<<"头插入的值为:";
  254.                 cin>>f;
  255.                 ListInsert_F(List,f);
  256.                 ListOutput(List);
  257.                 break;
  258.             case 3:
  259.                 int b;
  260.                 cout<<"尾插入的值为:";
  261.                 cin>>b;
  262.                 ListInsert_B(List,b);
  263.                 ListOutput(List);
  264.                 break;
  265.             case 4:
  266.                 int ips,val;
  267.                 cout<<"要插入的位置是:"<<endl;
  268.                 cin>>ips;
  269.                 cout<<"要插入的值是:"<<endl;
  270.                 cin>>val;
  271.                 ListInsert_P(List,ips,val);
  272.                 ListOutput(List);
  273.                 break;
  274.             case 5:
  275.                 int dps;
  276.                 cout<<"要删除的位置是:"<<endl;
  277.                 cin>>dps;
  278.                 ListDelete_P(List,dps);
  279.                 ListOutput(List);
  280.                 break;
  281.             case 6:
  282.                 int dv;
  283.                 cout<<"要删除的值是:";
  284.                 cin>>dv;
  285.                 ListDelete_V(List,dv);
  286.                 ListOutput(List);
  287.                 break;
  288.             case 7:
  289.                 int vs;
  290.                 cout<<"要查找的元素是:";
  291.                 cin>>vs;               
  292.                 ListOutput(List);
  293.                 ListSearch(List,vs);
  294.                 break;
  295.             case 8:
  296.                 int px;
  297.                 cout<<"要查找前驱的元素是:";
  298.                 cin>>px;
  299.                 ListOutput(List);
  300.                 ListFr(List,px);
  301.                 break;
  302.             case 9:
  303.                 int bx;
  304.                 cout<<"要查找后继的元素是:";
  305.                 cin>>bx;
  306.                 ListOutput(List);
  307.                 ListAf(List,bx);
  308.                 break;
  309.             case 10:
  310.                 ListDestory(List);
  311.                 break;
  312.             case 11:
  313.                 ListEmpty(List);
  314.                 ListOutput(List);
  315.                 break;
  316.             case 12:
  317.                 ListOutput(List);
  318.                 ListLength(List);
  319.                 break;
  320.             case 13:
  321.                 int posi;
  322.                 cout<<"结点的位置为:";
  323.                 cin>>posi;
  324.                 ListOutput(List);
  325.                 ListGetData(List,posi);
  326.                 break;
  327.         }
  328.         cin>>option;
  329.     }
  330.     return 0;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement