| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 323 人关注过本帖
标题:顺序栈出错
只看楼主 加入收藏
wolf赵帅
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-11-8
结帖率:50%
收藏
 问题点数:0 回复次数:0 
顺序栈出错
# include<stdio.h>
# include<stdlib.h>
# define StackInitSize 100
# define Increment 10
typedef struct
{
    int * base;
    int * top;
    int StackSize;
}SqStack;
SqStack InitStack(SqStack *S)
{
    (*S).base=(int *)malloc(StackInitSize*sizeof(int));
    if(!(*S).base) exit(0);
    else
    {
        (*S).top=(*S).base;
        (*S).StackSize=StackInitSize;
        return *S ;
    }
}
void Push(SqStack *S,int e)
{//将元素e入栈
    if((*S).top-(*S).base>=(*S).StackSize)
    {//栈满,追加存储空间
        (*S).base=(int *)realloc((*S).base,((*S).StackSize+Increment)*sizeof(int));
        if(!(*S).base) exit(0);
        (*S).top=(*S).base+(*S).StackSize;
        (*S).StackSize +=Increment;
    }
    *S->top++=e;
}
int Pop(SqStack *S,int e)
{//若栈不空,则用e返回栈顶元素;否则打印错误
    if((*S).top==(*S).base) printf("Error\n");
    e=*--S->top;
    return e;
}   

void main()
{//十进制转化成八进制
    int a,b,c;
    SqStack S;
    S=InitStack(&S);
    scanf("%d",&a);
    while(a!=0)
    {
        b=a%8;
        Push(&S,b);
        a=a/8;
    }
    while(!(S.top==S.base))
    {
        Pop(&S,c);
        printf("%-2d",c);
    }
    printf("\n");
}

为什么输入100后 运行结果不是1 4 4
搜索更多相关主题的帖子: return 空间 元素 
2011-04-28 01:59
快速回复:顺序栈出错
数据加载中...
 
   



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

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