静态链表

本文介绍了一个使用静态链表实现的数据结构及其基本操作,包括初始化、输入、输出、排序、插入和删除等,并演示了如何通过两个静态链表计算差集的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1.   
  2. #include<iostream>
  3. #include<malloc.h>
  4. #include<stdlib.h>
  5. using namespace std;
  6. #define OK 1
  7. #define ERROR 0
  8. #define OVERFLOW -2
  9. #define MAXSIZE 10
  10. typedef int Status;
  11. typedef int ElemType;
  12. typedef struct
  13. {
  14.         ElemType data;
  15.         int cur;
  16.         }component,SLinkList[MAXSIZE];
  17.         
  18. int Malloc_SL(SLinkList &space)
  19. {//若备用空间链表非空,则返回分配的结点的下标,否则返回0
  20.     int i=space[0].cur;
  21.     if(i) space[0].cur=space[i].cur;
  22.     return i;}   
  23.      
  24. void InitSpace_SL(SLinkList &space)
  25. {//将一维数组S中各分量链成一个备用链表
  26.      for(int i=0;i<MAXSIZE;i++)
  27.      space[i].cur=i+1;
  28.      space[MAXSIZE-1].cur=0;
  29.    
  30.      }
  31. void Input_SL(SLinkList &space,int n)               //输入 
  32. {
  33.      if(space[0].cur!=1||n>MAXSIZE-2) exit(OVERFLOW);
  34.      int k; 
  35.      k=Malloc_SL(space); space[k].cur=2;
  36.      for(int i=0;i<n;i++)
  37.      {
  38.      k=Malloc_SL(space);
  39.      cin>>space[k].data;}
  40.      space[k].cur=0;}
  41. void Free_SL(SLinkList &space,int k)          //将下标为n的结点回收到备用链表
  42. {
  43.    space[k].cur=space[0].cur;
  44.    space[0].cur=k; }
  45.    
  46. Status Delete_SL(SLinkList &space, int i, ElemType &e)
  47. {//在静态的单链线性表L中查找第i–1个元素的位置
  48. //若找到,且存在它的下一个位置的元素,则删除;否则返回ERROR
  49.     int j=1,m=0;
  50.     for(;space[j].cur&&m<i-1;m++) j=space[j].cur;
  51.     if(!space[j].cur||m>i-1) return ERROR;
  52.     int k=space[j].cur;
  53.     space[j].cur=space[k].cur;
  54.     Free_SL(space,k);
  55.     return OK;}
  56. Status Insert_SL(SLinkList &space, int i, ElemType e)
  57. {//在静态的单链线性表中查找第i–1个元素的位置
  58. //若找到,则在它的下一个位置插入元素,否则返回ERROR
  59.      int j=1,m=0;
  60.      for(;j&&m<i-1;m++) j=space[j].cur;
  61.      if(!j||m>i-1) return ERROR;
  62.      int k=Malloc_SL(space);
  63.      if(!k) exit(OVERFLOW);
  64.      space[k].data=e;
  65.      space[k].cur=space[j].cur;
  66.      space[j].cur=k;      
  67.      return OK;                                                                                             
  68. }
  69. void Output_SL(SLinkList space)                //输出 
  70. {
  71.      for(int j=space[1].cur;j;j=space[j].cur)
  72.      cout<<space[j].data<<" ";
  73.      cout<<endl;
  74.      }
  75. /*
  76. int LocateElem_SL(SLinkList space,ElemType e)
  77. {//在静态的单链线性表中查找1个值为e的元素
  78. //若找到,则返回它在S中的下标,否则返回0
  79. int j=space[1].cur;
  80. for(;j&&space[j].data!=e;j=space[j].cur);
  81. return j;
  82. }                                        */
  83. void Sort(SLinkList &space,int n)           //排序 
  84. {
  85.      for(int j=n+1;j>2;j--)
  86.      for(int i=2;i<j;i++)
  87.      if(space[i].data>space[i+1].data)
  88.      {
  89.         ElemType t=space[i].data;
  90.         space[i].data=space[i+1].data;
  91.         space[i+1].data=t;   }
  92.      }
  93. void difference(SLinkList &Sa,SLinkList &Sb)   //(A-B)∪(B-A)
  94. {      
  95.      int j=Sb[1].cur,k,p,m;
  96.      for(;j;j=Sb[j].cur)
  97.     {
  98.          for(k=Sa[1].cur,p=1;k&&Sb[j].data!=Sa[k].data;k=Sa[k].cur)
  99.               p=k;
  100.          if(Sb[j].data==Sa[k].data) {
  101.                   Sa[p].cur=Sa[k].cur;
  102.                   Free_SL(Sa,k);        }
  103.          else 
  104.                   {
  105.                     m=Malloc_SL(Sa);
  106.                     if(!m) exit(OVERFLOW); 
  107.                     Sa[m].data=Sb[j].data;
  108.                     Sa[m].cur=Sa[p].cur;
  109.                     Sa[p].cur=m;         }
  110.          }
  111. }
  112. int main()
  113. {
  114.     SLinkList Sa,Sb;
  115.     int num_a,num_b,pos_in,pos_del;
  116.     ElemType insert_elem,delete_elem;
  117.     InitSpace_SL(Sa);
  118.     cin>>num_a;
  119.     Input_SL(Sa,num_a);
  120.     Sort(Sa,num_a);
  121.     Output_SL(Sa);
  122.     
  123.     InitSpace_SL(Sb);
  124.     cin>>num_b;
  125.     Input_SL(Sb,num_b);
  126.     Sort(Sb,num_b);
  127.     Output_SL(Sb);
  128.     
  129.     difference(Sa,Sb);
  130.     Output_SL(Sa);
  131.     
  132.     cout<<"please input the position and the element you want to insert:";
  133.     cin>>pos_in>>insert_elem;
  134.     Insert_SL(Sa,pos_in,insert_elem);
  135.     Output_SL(Sa);
  136.     
  137.     cout<<"please input the position you want to delete:";
  138.     cin>>pos_del;
  139.     Delete_SL(Sa,pos_del,delete_elem);
  140.     Output_SL(Sa);
  141.     return 0;
  142. }
  143. 4
  144. 3 5 2 6
  145. 2 3 5 6
  146. 5
  147. 1 4 2 6 5
  148. 1 2 4 5 6
  149. 3 1 4
  150. please input the position and the element you want to insert:2
  151. 5
  152. 3 5 1 4
  153. please input the position you want to delete:3
  154. 3 5 4
  155. 请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值