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

数据结构求教~~

王志鹏 发布于 2010-05-18 17:22, 748 次点击
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
typedef struct STACK{
    int *base;
    int *top;
    int stacksize;
   
}sqstack;
typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*linklist;
sqstack Build(sqstack *s){
   
    s->base=malloc(100*sizeof(int));
    s->top=s->base;
    s->stacksize=100;
    return *s;
}
int GetTop(sqstack *s ,int e){
    if(s->top==s->base)
        exit (0);
    e=*(s->top-1);
    return e;
};
void push(sqstack *q,int e){
    if (q->top-q->base==q->stacksize)
    {
        q->base=malloc((q->stacksize+10)*sizeof(int));
        if(!q->base)exit;
        q->top=q->base+q->stacksize;
        q->stacksize+=10;
        
    }
    *q->top++=e;
}
void pop(sqstack *q,int e){
    if (q->base==q->top)
    {
        exit;
    }
    e=*--q->top;
}
main()
{
    int a[256];
    int n;
    int i=0;
    int j=0;
    int e;
    sqstack *S;
    S=malloc(100*sizeof(sqstack));
    Build(S);

    printf("input n:");
    scanf("%d",&n);
   
    for(i=0;i<n;i++)
    {
        printf("input a[%d]",i);
        scanf("%d",a[i]);
        push(S,a[i]);
    }
    while (S->base!=S->top)
    {
        pop(S,e);
        printf("%d",e);
    }
}
//这段代码,编译连接没有问题,但是一运行就出错,类似的程序写了好几个了,都是类似情况,头疼,求教~
3 回复
#2
寒风中的细雨2010-05-19 22:42
s->base=malloc(100*sizeof(int));           s->base=(int *)malloc(100*sizeof(int));
q->base=malloc((q->stacksize+10)*sizeof(int));    q->base=(int *)malloc((q->stacksize+10)*sizeof(int));
    void pop(sqstack *q,int e)           void pop(sqstack *q,int *e)
 e=*--q->top;                 *e=*--q->top;
S=malloc(100*sizeof(sqstack));                  S=(sqstack *)malloc(sizeof(sqstack));
scanf("%d",a[i]);           scanf("%d",&a[i]);
#3
冥卫2010-05-20 13:19
迷惑
#4
高冬青a2010-07-08 14:56
不错~~~~~呵呵·`
1