C++实现静态链表

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. const size_t MAXSIZE =100;  
  5. typedef struct SListNode {  
  6.     int data;  
  7.     size_t cur;  
  8.       
  9. }StaticList[MAXSIZE];  
  10.   
  11.   
  12.  //初始化链表函数  
  13.  void InitSList(StaticList & space )  
  14.  {  
  15.      for(int i=0;i< MAXSIZE -1 ;i++)  
  16.   {  
  17.       space[i].cur  = i+1;  
  18.   }  
  19.   
  20.      space[MAXSIZE -1].cur = 0;//构成循环  
  21.  }  
  22.   
  23.  //分配节点(只需要返回被分配节点在数组中的下标即可)  
  24.  int Malloc_SL(StaticList & space )  
  25.  {  
  26.      int i = space[0].cur;  
  27.      if(i)  
  28.      {  
  29.          space[0].cur = space[i].cur;//把i节点从空闲链表中删除  
  30.      }  
  31.      return i;  
  32.  }  
  33.   
  34.  //回收节点到空闲链表  
  35.  void Free_SLNode(StaticList & space,int t)  
  36.  {  
  37.      space[t].cur = space[0].cur;  
  38.      space[0].cur = t;  
  39.  }  
  40.  //插入  
  41.  bool insert(StaticList & space,int h,int m,int t)//在第t个元素之前插入一个下标值为m的新节点  
  42.  {  
  43.   if(t<=0)  
  44.   {  
  45.     return false;  
  46.   }  
  47.   int i = h;  
  48.   int n = 0;//计数器  
  49.   while(i)  
  50.   {  
  51.       if(n == t-1 && space[i].cur)//找到插入节点  
  52.     {  
  53.         space[m].cur = space[i].cur;  
  54.         space[i].cur = m;  
  55.         return true;  
  56.     }  
  57.     i = space[i].cur;  
  58.     n++;  
  59.   }  
  60.   return false;  
  61.  }  
  62.   
  63.  //删除第n个节点  
  64.  bool dele(StaticList & space,int h, int n)  
  65.  {  
  66.      if(n<0) return false;  
  67.      int i=h;  
  68.      int m =0;  
  69.      while(i)  
  70.      {  
  71.       if(m == n-1)  
  72.       {   int t = space[i].cur;  
  73.           space[i].cur = space[t].cur;  
  74.           Free_SLNode(space,t);//回收被删除的节点  
  75.           return true;  
  76.       }  
  77.       i = space[i].cur;  
  78.       m++;  
  79.      }  
  80.      return false;  
  81.  }  
  82.   
  83.  //在表尾增加一个新节点  
  84.  bool add(StaticList & space,int h,int m)//m代表新增加节点的下标值,h表示数据链表的头结点  
  85.  {  
  86.      int i = h;  
  87.      while(space[i].cur)  
  88.      {  
  89.        i = space[i].cur;  
  90.      }  
  91.      space[i].cur = m;  
  92.      space[m].cur = 0;  
  93.      return true;  
  94.  }  
  95.   
  96. int main()  
  97. {  
  98.     //定义一个静态链表  
  99.     StaticList MyList;  
  100.     InitSList(MyList);  
  101.     //分配一个新节点做为数据节点   空闲节点的默认头节点为0  
  102.     int h = Malloc_SL(MyList);  
  103.     MyList[h].cur = 0;  
  104.     //添加新节点  
  105.     int t = Malloc_SL(MyList);  
  106.     MyList[t].data = 1;  
  107.     add(MyList,h,t);  
  108.     t = Malloc_SL(MyList);  
  109.     MyList[t].data = 2;  
  110.     add(MyList,h,t);  
  111.     t = Malloc_SL(MyList);  
  112.     MyList[t].data = 3;  
  113.     add(MyList,h,t);  
  114.     //遍历并输出该链表上的所有数据  
  115.     cout<<"遍历并输出该链表上的所有数据:"<<endl;  
  116.     int i = MyList[h].cur;  
  117.     while (i)  
  118.     {  
  119.         cout<<MyList[i].data<<"  ";  
  120.         i = MyList[i].cur;  
  121.     }  
  122.     cout<<endl;  
  123.     //在第2个元素之前插入一个新节点  
  124.     cout<<"在第2个元素之前插入一个值为5的新节点:"<<endl;  
  125.     t = Malloc_SL(MyList);  
  126.     MyList[t].data = 5;  
  127.     insert(MyList,h,t,2);  
  128.      i = MyList[h].cur;  
  129.     while (i)  
  130.     {  
  131.         cout<<MyList[i].data<<"  ";  
  132.         i = MyList[i].cur;  
  133.     }  
  134.     cout<<endl;  
  135.   
  136.     //删除第2个节点  
  137.     cout<<"删除第2节点:"<<endl;  
  138.     dele(MyList,h,2);  
  139.     i = MyList[h].cur;  
  140.     while (i)  
  141.     {  
  142.         cout<<MyList[i].data<<"  ";  
  143.         i = MyList[i].cur;  
  144.     }  
  145.     cout<<endl;  
  146.   
  147.   
  148. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值