顺序栈和链栈的实现

以你之姓@ 2023-02-13 14:59 119阅读 0赞
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #define maxSize 100
  4. typedef struct{
  5. int data[maxSize];
  6. int top;
  7. }SqStack;
  8. typedef struct{
  9. int data;
  10. struct Lnode *next;
  11. }LNode;
  12. int main(){
  13. //顺序栈基本函数
  14. void initStack(SqStack* st);
  15. int isEmpty(SqStack* st);
  16. int push(SqStack* st,int x);
  17. SqStack* pop(SqStack* st,int *x);
  18. void Output(SqStack* st,int* x);
  19. //链栈基本函数
  20. void initLStack(LNode*lst);
  21. int isLEmpty(LNode* lst);
  22. void LPush(LNode* lst,int x);
  23. void LOutput(LNode* lst,int *x);
  24. LNode* lst;
  25. int* y;
  26. initLStack(lst);
  27. if(isLEmpty(lst) == 1)
  28. printf("栈为空\n");
  29. else printf("栈不为空\n");
  30. for(int i = 0;i < 5;i++){
  31. LPush(lst,i);
  32. }
  33. if(isLEmpty(lst) == 1)
  34. printf("栈为空\n");
  35. else printf("栈不为空\n");
  36. LOutput(lst,y);
  37. //顺序栈测试
  38. // SqStack* st;
  39. // int* x;
  40. // initStack(st);
  41. // if(isEmpty(st) == 1)
  42. // printf("栈为空\n");
  43. // else printf("栈不为空\n");
  44. //
  45. // for(int i = 0;i < 10;i++)
  46. // push(st,i);
  47. //
  48. // if(isEmpty(st) == 1)
  49. // printf("栈为空\n");
  50. // else printf("栈不为空\n");
  51. // Output(st,x);
  52. return 0;
  53. }
  54. //链栈
  55. //链栈的初始化
  56. void initLStack(LNode*lst){
  57. lst = (LNode*)malloc(sizeof(LNode));
  58. lst->next = NULL;
  59. }
  60. //判断栈空的代码
  61. int isLEmpty(LNode* lst){
  62. if(lst->next == NULL){
  63. return 1;
  64. } else{
  65. return 0;
  66. }
  67. }
  68. //进栈代码
  69. void LPush(LNode* lst,int x){
  70. LNode* p;
  71. p = (LNode*)malloc(sizeof(LNode));
  72. p->next = NULL;
  73. //头插法
  74. p->data = x;
  75. p->next = lst->next;
  76. lst->next = p;
  77. }
  78. //出栈代码
  79. int* LPop(LNode* lst,int *x){
  80. LNode* p;
  81. if (lst->next == NULL){
  82. return NULL;
  83. }
  84. //单链表的删除操作
  85. p = lst->next;
  86. x = p->data;
  87. lst->next = p->next;
  88. free(p);
  89. return x;
  90. }
  91. void LOutput(LNode* lst,int *x){
  92. printf("输出栈内元素:\n");
  93. while (1){
  94. printf("%d ",LPop(lst,x));
  95. if (lst->next == NULL){
  96. break;
  97. }
  98. }
  99. printf("\n");
  100. }
  101. //顺序栈
  102. //初始化栈
  103. void initStack(SqStack* st){
  104. st->top = -1;
  105. }
  106. //判断栈空代码
  107. int isEmpty(SqStack* st){
  108. if(st->top == -1){
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. //进栈代码
  114. int push(SqStack* st,int x){
  115. if(st->top == maxSize - 1){
  116. return 0;
  117. }
  118. ++(st->top);
  119. st->data[st->top] = x;
  120. return 1;
  121. }
  122. //出栈代码
  123. SqStack* pop(SqStack* st,int *x){
  124. if(st->top == -1){
  125. return NULL;
  126. }
  127. x = st->data[st->top];
  128. --(st->top);
  129. return x;
  130. }
  131. //打印栈内元素
  132. void Output(SqStack* st,int* x){
  133. printf("输出栈内元素:\n");
  134. for(int i = 0;i < maxSize;i++){
  135. printf("%d ",pop(st,x));
  136. if (st->top == -1){
  137. break;
  138. }
  139. }
  140. printf("\n");
  141. }

发表评论

表情:
评论列表 (有 0 条评论,119人围观)

还没有评论,来说两句吧...

相关阅读