鱼C论坛

 找回密码
 立即注册
查看: 4849|回复: 2

[已解决]堆栈的实现,不知为何运行结果很奇怪!求大佬指导

[复制链接]
发表于 2021-4-5 12:46:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX_SIZE 10

  4. typedef struct stack{
  5.         int top;
  6.         int data[MAX_SIZE];
  7. }stack;

  8. void init_stack(stack a){
  9.         a.top=-1;
  10.         a.data[]
  11. }

  12. int stack_empty(stack a){
  13.         if(a.top==-1) return 1;
  14.         else return 0;
  15. }

  16. void show(stack a){
  17.         if(stack_empty) printf("An empty stack!");
  18.         int temp = a.top;
  19.         while(temp>=0){
  20.                 printf("%d ",a.data[temp]);
  21.                 temp--;
  22.         }
  23.         printf("\n");
  24. }

  25. void push(stack a,int new_push){
  26.         if(MAX_SIZE-1==a.top) printf("Full stack!Stop insert data!");
  27.         else{
  28.                 ++a.top;
  29.                 a.data[a.top]=new_push;
  30.         }
  31. }

  32. void pop(stack a){
  33.         if(stack_empty) printf("An empty stack!");
  34.         else a.top--;
  35. }

  36. int get_top_elem(stack a){
  37.         return a.data[a.top];
  38. }

  39. int main(){
  40.         int i;
  41.         stack a;
  42.         init_stack(a);
  43.         //show(a);
  44.         for(i = 0;i<10;i++) {
  45.                 push(a,i);
  46.                 show(a);
  47.         }
  48.         pop(a);
  49.         show(a);
  50.         return 0;
  51. }
复制代码
最佳答案
2021-4-8 19:39:17
本帖最后由 李京 于 2021-4-8 19:43 编辑

你的函数都应该传入指针,不能直接传入,这是传值和传地址的区别

你的程序  21 和 39 if 判断的条件,调用函数没有加 ()  


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX_SIZE 10

  4. typedef struct stack {
  5.     int top;
  6.     int data[MAX_SIZE];
  7. }*stack, sta; // 这里的名字我随便取的   这里我修改了  stack 变成了 指针类型的了

  8. void init_stack(stack a) { // 传入指针
  9.     a->top = -1;
  10.     a->data[0] = 0;
  11. }

  12. int stack_empty(stack a) {
  13.     if (a->top == -1)
  14.     {
  15.         printf("t->top  ==  %d   ", a->top);
  16.         return 1;
  17.     }
  18.     else return 0;
  19. }

  20. void show(stack a) {
  21.     if (stack_empty(a)) printf("An empty stack!"); // 这里我改了
  22.     int temp = a->top;
  23.     while (temp >= 0) {
  24.         printf("%d ", a->data[temp]);
  25.         temp--;
  26.     }
  27.     printf("\n");
  28. }

  29. void push(stack a, int new_push) {
  30.     if (MAX_SIZE - 1 == a->top) printf("Full stack!Stop insert data!");
  31.     else {
  32.         ++a->top;
  33.         a->data[a->top] = new_push;
  34.     }
  35. }

  36. void pop(stack a) {
  37.     if (stack_empty(a)) printf("An empty stack!"); // 这里的条件我改了
  38.     else a->top--;
  39. }

  40. int get_top_elem(stack a) {
  41.     return a->data[a->top];
  42. }

  43. int main() {
  44.     int i;
  45.     sta a;
  46.     init_stack(&a); //  传入地址
  47.     //show(a);
  48.     for (i = 0; i < 10; i++) {
  49.         push(&a, i);
  50.         show(&a);
  51.     }
  52.     pop(&a);
  53.     show(&a);
  54.     return 0;
  55. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-8 19:39:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 李京 于 2021-4-8 19:43 编辑

你的函数都应该传入指针,不能直接传入,这是传值和传地址的区别

你的程序  21 和 39 if 判断的条件,调用函数没有加 ()  


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX_SIZE 10

  4. typedef struct stack {
  5.     int top;
  6.     int data[MAX_SIZE];
  7. }*stack, sta; // 这里的名字我随便取的   这里我修改了  stack 变成了 指针类型的了

  8. void init_stack(stack a) { // 传入指针
  9.     a->top = -1;
  10.     a->data[0] = 0;
  11. }

  12. int stack_empty(stack a) {
  13.     if (a->top == -1)
  14.     {
  15.         printf("t->top  ==  %d   ", a->top);
  16.         return 1;
  17.     }
  18.     else return 0;
  19. }

  20. void show(stack a) {
  21.     if (stack_empty(a)) printf("An empty stack!"); // 这里我改了
  22.     int temp = a->top;
  23.     while (temp >= 0) {
  24.         printf("%d ", a->data[temp]);
  25.         temp--;
  26.     }
  27.     printf("\n");
  28. }

  29. void push(stack a, int new_push) {
  30.     if (MAX_SIZE - 1 == a->top) printf("Full stack!Stop insert data!");
  31.     else {
  32.         ++a->top;
  33.         a->data[a->top] = new_push;
  34.     }
  35. }

  36. void pop(stack a) {
  37.     if (stack_empty(a)) printf("An empty stack!"); // 这里的条件我改了
  38.     else a->top--;
  39. }

  40. int get_top_elem(stack a) {
  41.     return a->data[a->top];
  42. }

  43. int main() {
  44.     int i;
  45.     sta a;
  46.     init_stack(&a); //  传入地址
  47.     //show(a);
  48.     for (i = 0; i < 10; i++) {
  49.         push(&a, i);
  50.         show(&a);
  51.     }
  52.     pop(&a);
  53.     show(&a);
  54.     return 0;
  55. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-4-17 23:42:50 | 显示全部楼层
李京 发表于 2021-4-8 19:39
你的函数都应该传入指针,不能直接传入,这是传值和传地址的区别

你的程序  21 和 39 if 判断的条件,调 ...

感谢!!!搞了好久了都没搞懂!!
!!!谢谢您!!
看了您改动后的代码如沐春风
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 06:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表