鱼C论坛

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

用头插法创建单链表实现栈的操作,存在BUG!不过 调试器什么的都坏了...

[复制链接]
发表于 2014-5-6 19:40:38 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 风中醉柳 于 2014-5-6 19:43 编辑

谁能帮我看下bug存在哪里....谢谢咯

#include<stdio.h>
#include<stdlib.h>

typedef int Emptype;
struct L_stack
{
    Emptype data;
    struct L_stack * next;
}StackSize;

typedef struct L_stack *Ptr;
typedef Ptr hStack;

hStack initstack()
{
    hStack sBottom;
    sBottom=(hStack )malloc(sizeof(StackSize));
    if(!sBottom)
        return NULL;
    sBottom->next=NULL;
    return sBottom;
}

void push(hStack s,Emptype a)
{
    hStack add;
    add=(hStack )malloc(sizeof(StackSize));
    if(!add)
         printf("stack NO space\n");
    add->data=a;
    add->next=s->next;
    s->next=add;
}

Emptype pop(hStack s)
{
    if(!s)
        return 0;
    hStack r=s;
    Emptype io;
    io=s->data;
    s=s->next;
    free(r);
    return io;
}

void DestoryStack(hStack s)
{
    hStack r;
    if(!s)
     printf("stack NO start\n");
    while(s)
    {
        r=s;
        s=s->next;
        free(r);
    }
}

int main(void)
{
    hStack q=initstack(),S;
    int i;
    S=q->next;
    for(i=0;i<=5;i++)
        push(S,i);
    for(i=0;i<5;i++)   
        printf("%d ",pop(S));
    printf("\n");   
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-7 16:45:40 | 显示全部楼层
本帖最后由 elvo 于 2014-5-7 16:46 编辑

根据你的code修改了一下,修改的地方大都做了注释的
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef int Emptype;
  4. typedef struct L_stack
  5. {
  6.     Emptype data;
  7.     struct L_stack * next;
  8. }StackSize;

  9. typedef struct L_stack *Ptr;
  10. typedef Ptr hStack;

  11. /*
  12. hStack initstack()
  13. {
  14.     hStack sBottom;
  15.     sBottom=(hStack )malloc(sizeof(StackSize));
  16.     if(!sBottom)
  17.         return NULL;
  18.     sBottom->next=NULL;
  19.     return sBottom;
  20. }
  21. */

  22. void push(hStack *s,Emptype a)    //传入了指向s的指针,以便能修改s
  23. {
  24.     hStack add;
  25.     add=(hStack )malloc(sizeof(StackSize));
  26.     if(!add)
  27.          printf("stack NO space\n");
  28.     add->data=a;
  29.     if(!(*s)){                  
  30.             add->next=NULL;
  31.             (*s)=add;
  32.     }
  33.     else{
  34.             add->next=(*s);
  35.             (*s)=add;
  36.     }
  37.            

  38. }

  39. Emptype pop(hStack *s)   //同上
  40. {
  41.     if(!(*s))
  42.         return 0;
  43.     hStack r=(*s);
  44.     Emptype io;
  45.     io=(*s)->data;
  46.     (*s)=(*s)->next;
  47.     free(r);
  48.     return io;
  49. }

  50. void DestoryStack(hStack *s)
  51. {
  52.     hStack r;
  53.     if(!(*s))
  54.      printf("stack NO start\n");
  55.     while((*s))
  56.     {
  57.         r=(*s);
  58.         (*s)=(*s)->next;
  59.         free(r);
  60.     }
  61. }

  62. int main(void)
  63. {
  64.     hStack q=NULL;
  65.     int i;
  66.     //S=q->next; 为什么要这样做  S=NULL
  67.     for(i=0;i<=5;i++)
  68.         push(&q,i);
  69.   //  DestoryStack(&q);
  70.     for(i=0;i<5;i++)   
  71.         printf("%d ",pop(&q));
  72.     printf("\n");   
  73. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-11 16:40:28 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 19:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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