| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 594 人关注过本帖
标题:顺序栈出问题了……
只看楼主 加入收藏
寻梦飞翔
Rank: 1
等 级:新手上路
帖 子:76
专家分:9
注 册:2010-3-15
结帖率:94.12%
收藏
已结贴  问题点数:5 回复次数:6 
顺序栈出问题了……
#define MAXSIZE 100
typedef struct SqStack
 {
   int elem[MAXSIZE];
   int top;
 }*S;

int SqStackpush(S s,int e)
{
 if(s->top==MAXSIZE-1)
 {
   printf("栈满溢出");
   return 0;
 }
 else
 {s->top++;
  s->elem[s->top]=e;
 }
}
int SqStackEmpty(S s)
{if(s->top==0)
 return 0;
 else
 return 1;
}
int SqStackpop(S s,int a)
{
  a=s->elem[s->top];
  s->top--;
  return a;
}
main()
{S s;
 int a;
 scanf("%d",&a);
 while(a!=0)
 {
   SqStackpush(s,a);
   scanf("%d",&a);
 }
 while(SqStackEmpty(s))
 {
   SqStackpop(s,a);
   printf("%d",a);
 }
}
搜索更多相关主题的帖子: 顺序 
2010-04-04 11:11
邶风
Rank: 5Rank: 5
等 级:职业侠客
帖 子:287
专家分:335
注 册:2009-1-20
收藏
得分:1 
头文件没有include
main中S没有malloc空间
你的pop也没有栈空时的检查

#include
2010-04-04 11:52
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
先还是认真理解模仿  然后再加入自己的算法去实现
 
2010-04-04 12:19
寻梦飞翔
Rank: 1
等 级:新手上路
帖 子:76
专家分:9
注 册:2010-3-15
收藏
得分:0 
还是不太明白,能具体说一下吗?谢谢……
2010-04-04 17:37
ggvvcc
Rank: 3Rank: 3
来 自:山东临沂
等 级:论坛游侠
帖 子:50
专家分:119
注 册:2010-4-3
收藏
得分:3 
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10

typedef struct SqStack{
   int elem[MAXSIZE];
   int top;
} SQ, *S;

int SqStackpush(S s, int e)
{
    if(s->top == MAXSIZE-1){
       printf("栈满溢出\n");
       return -1;
    }else{
        s->elem[s->top++] = e;
        return e;
    }
}

int getTop(S s){
    return s->top;
}

int SqStackEmpty(S s){
    if(getTop(s) > 0)
        return 1;
    else
        return 0;
}

int SqStackpop(S s){
    return s->elem[--s->top];

}

void init(S s){
    int i = 0;
    s->top = 0;
    while(i < MAXSIZE){
        s->elem[i++] = 0;
    }
}


int main(){
    S s = (S)malloc(sizeof(SQ));
    int a = -1;
    init(s);
    printf("please enter an integer: \n");
    scanf("%d", &a);
    while(a != 0){
        SqStackpush(s, a);
        printf("please enter an integer: \n");
        scanf("%d", &a);
    }

    while(SqStackEmpty(s))
    {
       printf("%5d", SqStackpop(s));
    }
    printf("\n");
    free(s);
    return 0;
}
出了一头汗,终于做出来了,你试试。
2010-04-04 19:21
寻梦飞翔
Rank: 1
等 级:新手上路
帖 子:76
专家分:9
注 册:2010-3-15
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10

typedef struct SqStack{
   int elem[MAXSIZE];
   int top;
} SQ, *S;

int SqStackpush(S s, int e)
{
    if(s->top == MAXSIZE-1){
       printf("栈满溢出\n");
       return -1;
    }else{
        s->elem[s->top++] = e;
        return e;
    }
}

int getTop(S s){
    return s->top;
}

int SqStackEmpty(S s){
    if(getTop(s) > 0)
        return 1;
    else
        return 0;
}

int SqStackpop(S s){
    return s->elem[--s->top];

}

void init(S s){
    int i = 0;
    s->top = 0;
    while(i < MAXSIZE){              //请问这个循环是什么意思?
        s->elem[i++] = 0;
    }
}


int main(){
    S s = (S)malloc(sizeof(SQ));
    int a = -1;
    init(s);
    printf("please enter an integer: \n");
    scanf("%d", &a);
    while(a != 0){
        SqStackpush(s, a);
        printf("please enter an integer: \n");
        scanf("%d", &a);
    }

    while(SqStackEmpty(s))
    {
       printf("%5d", SqStackpop(s));
    }
    printf("\n");
    free(s);
    return 0;
}
2010-04-04 22:01
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:1 
这种结构体的定义
不能说是栈  应该是顺序表
2010-04-05 09:09
快速回复:顺序栈出问题了……
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011709 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved