静态链表

  1. #include<iostream>  
  2. #include<cstdlib>  
  3. #include<cassert>  
  4. using namespace std;  
  5.   
  6. typedef enum{FALSE,TRUE}Status;  
  7. typedef int ElemType;  
  8. #define MAXSIZE 10  
  9.   
  10. typedef struct StaticNode  
  11. {  
  12.     ElemType data;  
  13.     int cur;  
  14. }StaticNode;  
  15. typedef StaticNode StaticList[MAXSIZE];  
  16.   
  17. void Init_SL(StaticList &SL)  
  18. {  
  19.     for (int i = 0; i < MAXSIZE - 1; ++i)  
  20.     {  
  21.         SL[i].cur = i + 1;  
  22.     }  
  23.     SL[MAXSIZE - 1].cur = 0;//开始静态链表为空,没有有效节点,所以有效链表的头结点的指向为NULL(0)  
  24. }  
  25. //开辟成功:返回开辟节点的下标  
  26. //开辟失败:返回0  
  27. int Malloc_SL(StaticList &SL)  
  28. {  
  29.     int i = SL[0].cur;  
  30.     if (i == 0 || i == MAXSIZE - 1)  
  31.         return 0;  
  32.     else//存在备用链表  
  33.         SL[0].cur = SL[i].cur;//备用链表用了一个节点,把它的下一个节点用来备用  
  34.     return i;  
  35. }  
  36. //将释放的节点,头插到备用链表中  
  37. void Free_SL(StaticList &SL,int k)  
  38. {  
  39.     SL[k].cur = SL[0].cur;  
  40.     SL[0].cur = k;  
  41. }  
  42.   
  43. void Show_SL(StaticList SL)  
  44. {  
  45.     int i = SL[MAXSIZE - 1].cur;//找到第一个有效节点的下标  
  46.     while (i != 0)  
  47.     {  
  48.         cout << SL[i].data << "-->";  
  49.         i = SL[i].cur;  
  50.     }  
  51.     cout <<"Nul."<< endl;  
  52. }  
  53. Status Push_Back(StaticList &SL, ElemType x)  
  54. {  
  55.     int i = Malloc_SL(SL);  
  56.     if (i == 0)  
  57.     {  
  58.         cout << "静态链表已满"<<x<<"无法尾插" << endl;  
  59.         return FALSE;  
  60.     }  
  61.     SL[i].data = x;  
  62.     SL[i].cur = 0;  
  63.   
  64.     int j = SL[MAXSIZE - 1].cur;//寻找最后一个节点(最后一个节点的cur为0),进行尾插  
  65.     while (SL[j].cur != 0)  
  66.     {  
  67.         j = SL[j].cur;  
  68.     }  
  69.     SL[j].cur = i;  
  70.     return TRUE;  
  71. }  
  72. Status Push_Front(StaticList &SL, ElemType x)  
  73. {  
  74.     int i = Malloc_SL(SL);  
  75.     if (i == 0)  
  76.     {  
  77.         cout << "静态链表已满" << x << "无法头插" << endl;  
  78.         return FALSE;  
  79.     }  
  80.     SL[i].data = x;  
  81.     SL[i].cur = SL[MAXSIZE - 1].cur;  
  82.     SL[MAXSIZE - 1].cur = i;  
  83.     return TRUE;  
  84. }   
  85. Status Pop_Front(StaticList &SL)  
  86. {  
  87.     if (SL[MAXSIZE - 1].cur == 0)  
  88.     {  
  89.         cout << "静态链表已空,无法头删" << endl;  
  90.         return FALSE;  
  91.     }  
  92.     int i = SL[MAXSIZE - 1].cur;  
  93.     SL[MAXSIZE - 1].cur = SL[i].cur;  
  94.     Free_SL(SL, i);  
  95.     //SL[MAXSIZE - 1].cur = SL[i].cur;//从有效节点中删除  
  96.     //SL[i].cur = SL[0].cur;//将删除的节点头插入到备用链表中  
  97.     //SL[0].cur = i;  
  98.     return TRUE;  
  99. }  
  100. Status Pop_Back(StaticList &SL)  
  101. {  
  102.     if (SL[MAXSIZE - 1].cur == 0)  
  103.     {  
  104.         cout << "静态链表已空,无法头删" << endl;  
  105.         return FALSE;  
  106.     }  
  107.     int i = MAXSIZE - 1;  
  108.     while (SL[SL[i].cur].cur != 0)//寻找最后一个节点的前驱  
  109.     {  
  110.         i = SL[i].cur;  
  111.     }  
  112.     int tmp = SL[i].cur;//有效链表的最后一个节点的坐标(先保存下来)  
  113.     SL[i].cur = 0;//在有效链表中删除尾节点  
  114.     Free_SL(SL,tmp);  
  115.     //SL[tmp].cur = SL[0].cur;  
  116.     //SL[0].cur = tmp;  
  117.     return TRUE;  
  118. }  
  119.   
  120. //void Clear_SL(StaticList &SL)  
  121. //#include<iostream>  
  122. //using namespace std;  
  123. //#define MAXSIZE 8  
  124. //typedef int ElemType;  
  125. //  
  126. //typedef struct StaticListNode  
  127. //{  
  128. //  ElemType data;  
  129. //  size_t   cur;  
  130. //}StaticListNode;  
  131. //typedef StaticListNode StaticList[MAXSIZE];  
  132. //  
  133. //void Initialize(StaticList &SL)  
  134. //{  
  135. //  
  136. //}  
  137. //int Malloc_SL(StaticList &SL)  
  138. //{  
  139. //  if (SL[1].cur == -1)     //SL[1].cur存放备用链表的第一个节点的下标  
  140. //      return -1;  
  141. //  int i = SL[1].cur;  
  142. //  SL[1].cur = SL[i].cur;   //作业——--->头删除,尾删除(回收空间)  
  143. //  return i;  
  144. //}  

main.cpp

[cpp]  view plain  copy
  1. #include"StaticList.h"  
  2. int main()  
  3. {  
  4.     StaticList SL;  
  5.     Init_SL(SL);  
  6.     for (int i = 0; i < 5; ++i)  
  7.     {  
  8.         //Push_Back(SL, i);  
  9.         Push_Front(SL, i);  
  10.     }  
  11.     Show_SL(SL);  
  12.     for (int i = 0; i < 5; ++i)  
  13.     {  
  14.         //Pop_Front(SL);  
  15.         Pop_Back(SL);  
  16.         Show_SL(SL);  
  17.     }  
  18.       
  19.     system("pause");  
  20.     return 0;  
  21. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值