当前位置:   article > 正文

数据结构:链栈(含完整代码,可复制)_链栈数据结构

链栈数据结构

       链栈是采用链式存储结构实现的栈,通常用单链表来表示。链栈的优点是不存在栈满上溢的情况(只有在内存溢出时才会出现栈满,通常不考虑)。链栈的栈顶是链表的第一个结点,栈底是链表的最后一个结点,一个链栈可以由栈顶指针唯一确定。链栈的每个结点都包含两个域,数据域和指针域,与单链表的结点结构一样。链栈只能在栈顶进行入栈或出栈操作,类似于一个只能进行头插法或尾插法的单链表。

1.链栈的初始化

  1. Lsnode *InitStack(Lsnode *top)//初始化栈
  2. { top=(Lsnode *)malloc(sizeof(Lsnode));
  3. top->next=NULL;
  4. printf("Stack is NULL.\n");
  5. return top;
  6. }

2.插入一个元素

  1. void Push(Lsnode *top, ElemType x)//插入一个元素
  2. {Lsnode *p;
  3. p=(Lsnode *)malloc(sizeof(Lsnode));
  4. p->data=x;
  5. p->next=top->next;
  6. top->next=p;
  7. }

3.删除一个元素

  1. ElemType Pop(Lsnode *top) //删除一个元素
  2. {ElemType x; Lsnode *p;
  3. if(top->next!=NULL)
  4. { p=top->next; top->next=p->next;
  5. x=p->data; free(p); return x;
  6. }
  7. else
  8. { printf("Stack null! \n");
  9. exit(1);
  10. }
  11. }

4.取栈顶元素

  1. ElemType GetTop(Lsnode *top) //取栈顶元素
  2. {ElemType x; Lsnode *p;
  3. if(top->next!=NULL)
  4. { p=top->next;
  5. x=p->data; return x;
  6. }
  7. else
  8. { printf("Stack null! \n");
  9. exit(1);
  10. }
  11. }

5.输出链栈

  1. void OutStack(Lsnode *top)//输出链栈
  2. {ElemType x;Lsnode *p;
  3. p=top->next;
  4. while(p!=NULL)
  5. {
  6. x=p->data; printf(" %d ",x);
  7. p=p->next;
  8. }
  9. }

6.完整代码

  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. typedef int ElemType;
  4. typedef struct Lsnode
  5. {ElemType data;
  6. struct Lsnode *next;
  7. } Lsnode;
  8. void OutStack(Lsnode *p);
  9. Lsnode *InitStack(Lsnode *p);
  10. void Push(Lsnode *p,ElemType x);
  11. ElemType Pop(Lsnode *p);
  12. ElemType GetTop(Lsnode *p);
  13. Lsnode *q;
  14. int main()
  15. {
  16. ElemType y,cord;ElemType a;
  17. do { printf("\n");
  18. printf("\n 主菜单\n");
  19. printf("\n 1 初始化栈\n");
  20. printf("\n 2 插入一个元素\n");
  21. printf("\n 3 删除栈顶元素\n");
  22. printf("\n 4 取栈顶元素\n");
  23. printf("\n 5 结束程序运行\n");
  24. printf("\n---------------\n");
  25. printf("请输入您的选择(1.2.3.4.5)"); scanf("%d",&cord);
  26. switch(cord)
  27. {case 1:q=InitStack(q);OutStack(q);
  28. break;
  29. case 2:{printf("\n请输入要插入的数据:");scanf("%d",&a);
  30. Push(q,a);OutStack(q);
  31. }break;
  32. case 3:{a=Pop(q);printf("\n删除的是 %d\n",a);
  33. OutStack(q);
  34. }break;
  35. case 4:{y=GetTop(q);printf("\n 栈顶=%d\n",y);
  36. OutStack(q);
  37. }break;
  38. case 5:exit(1);
  39. }
  40. }while(cord<=5);
  41. }
  42. Lsnode *InitStack(Lsnode *top)//初始化栈
  43. { top=(Lsnode *)malloc(sizeof(Lsnode));
  44. top->next=NULL;
  45. printf("Stack is NULL.\n");
  46. return top;
  47. }
  48. void Push(Lsnode *top, ElemType x)//插入一个元素
  49. {Lsnode *p;
  50. p=(Lsnode *)malloc(sizeof(Lsnode));
  51. p->data=x;
  52. p->next=top->next;
  53. top->next=p;
  54. }
  55. ElemType Pop(Lsnode *top) //删除一个元素
  56. {ElemType x; Lsnode *p;
  57. if(top->next!=NULL)
  58. { p=top->next; top->next=p->next;
  59. x=p->data; free(p); return x;
  60. }
  61. else
  62. { printf("Stack null! \n");
  63. exit(1);
  64. }
  65. }
  66. ElemType GetTop(Lsnode *top) //取栈顶元素
  67. {ElemType x; Lsnode *p;
  68. if(top->next!=NULL)
  69. { p=top->next;
  70. x=p->data; return x;
  71. }
  72. else
  73. { printf("Stack null! \n");
  74. exit(1);
  75. }
  76. }
  77. void OutStack(Lsnode *top)//输出链栈
  78. {ElemType x;Lsnode *p;
  79. p=top->next;
  80. while(p!=NULL)
  81. {
  82. x=p->data; printf(" %d ",x);
  83. p=p->next;
  84. }
  85. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/905908
推荐阅读
相关标签
  

闽ICP备14008679号