注册 登录
编程论坛 数据结构与算法

关于数据结构的问题(1)

lufeng1720 发布于 2010-04-21 16:53, 547 次点击
#include<stdio.h>
#include<stdlib.h>
#define MAXLEN 10
typedef struct
{ int data[MAXLEN];
    int top;
}seqstack;
seqstack *Snull()
{seqstack *s;
    s=malloc(sizeof(seqstack));
    s->top=-1;
    return s;}
int push(seqstack *s,int x)
{if(s->top==MAXLEN-1)
    return 0;
    else
    {s->top++;
        s->data[s->top]=x;
        return 1;}
}
int sempty(seqstack *s)
{if(s->top==-1)
    return 1;
    else
    return 0;
}
int sfull(seqstack *s)
{if(s->top==MAXLEN-1)
    return 1;
    else return 0;
}
int pop(seqstack *s,int *x)
{if(sempty(s))
return 0;
else
{*x=s->data[s->top];
s->top--;
return 1;
}
}
int readtop(seqstack *s)
{if(sempty(s))
    return 0;
    else
    return(s->data[s->top]);
}


main()
{int *x=NULL;
    seqstack *s;
    s=Snull;
    push(s,5);
    printf("%d",readtop(s));
    pop(s,x);
    printf("%d",*x);
}
   
我怎么运行不了啊????????请各位帮忙看看啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!这是为什么???????????????????????????
1 回复
#2
寒风中的细雨2010-04-21 21:14

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

#define MAXLEN 10

typedef struct
{
    int data[MAXLEN];
    int top;
}seqstack;

seqstack *Snull()
{
    seqstack *s;
    s = (seqstack *) malloc (sizeof(seqstack));
    s->top=-1;
    return s;
}

int push(seqstack *s,int x)
{
    if(s->top==MAXLEN-1)
        return 0;
    else
    {
        s->top++;
        s->data[s->top]=x;
        return 1;
    }
}

int sempty(seqstack *s)//判断是否栈空
{
    if(s->top==-1)
        return 1;
    else
        return 0;
}

int sfull(seqstack *s)//判断是否栈满
{
    if(s->top==MAXLEN-1)
        return 1;
    else
        return 0;
}

int pop(seqstack *s,int *x)
{
    if(sempty(s))
    return 0;
    else
    {
        *x=s->data[s->top];
        s->top--;
        return 1;
    }
}

int readtop(seqstack *s)
{
    if(sempty(s))
    return 0;
    else
    return(s->data[s->top]);
}


int main()
{
    //int *x=NULL;
    int *x=(int *) malloc (sizeof(int));
    seqstack *s;

    s=Snull();
    push(s,5);
    printf("%d\n",readtop(s));
    pop(s,x);
    printf("%d\n",*x);

    return 0;
}
1