| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 377 人关注过本帖
标题:求助:这段顺序栈C代码中出栈函数的错误
只看楼主 加入收藏
tane05
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-12-28
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:2 
求助:这段顺序栈C代码中出栈函数的错误
程序代码:
include<stdio.h>
#include<stdlib.h>

#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2

typedef int ElemType;
typedef struct stack
{
    ElemType *base;
    ElemType *top;
    int stacksize;
}STACK;
typedef struct stack *SqStack;

void InitStack(SqStack index);
void DestroyStack(SqStack index);
int StackLength(SqStack index);
int Push(SqStack index,ElemType e);
int GetTop(SqStack index);
int Pop(SqStack index,ElemType e);

int main()
{
    STACK s;
    SqStack point=&s;
    InitStack(point);
    printf("The length of Stack is %d!\n",StackLength(point));
    Push(point,5);
    printf("Now the top element of Stack is %d!\n",GetTop(point));
    printf("The length of Stack is %d!\n",StackLength(point));
    Push(point,20);
    Push(point,30);
    printf("Now the top element of Stack is %d!\n",GetTop(point));
    printf("The length of Stack is %d!\n",StackLength(point));
    ElemType e;
    Pop(point,e);
    printf("The value of e is %d!\n",e);
    DestroyStack(point);
    return 0;
}

void InitStack(SqStack index)
{
    index->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    if(index->base==NULL)
    {
        printf("Apply space is not successed!\n");
        exit(1);
    }
    index->top=index->base;
    index->stacksize=STACK_INIT_SIZE;
    printf("Apply space is successed!\n");
}

void DestroyStack(SqStack index)
{
    index->base=index->top=NULL;
    index->stacksize=0;
    printf("Stack has destroy!\n");
}

int StackLength(SqStack index)
{
    return index->top-index->base;
}

int Push(SqStack index,ElemType e)
{
    if(StackLength(index)>=index->stacksize)
    {
        ElemType *sq=(ElemType *)realloc(index->base,(StackLength(index)+STACKINCREMENT)*sizeof(ElemType));
        index->base=sq;
        index->stacksize+=STACKINCREMENT;
    }
    *(index->top)=e;
    index->top++;
    return 1;
}

int GetTop(SqStack index)
{
    if(index->top==index->base)
    {
        printf("Stack is Empty!There is no element!");
        exit(1);
    }
    ElemType *flag=index->top-1;
    ElemType e=*flag;
    return e;
}

/*int Pop(SqStack index,ElemType e)

 出栈操作,将出栈的元素保存到e中
{
    if(index->top==index->base)
    {
        printf("Stack is Empty!There is no Element!");
    }
//    printf("求妹子!\n");
    e=GetTop(index);
    index->top--;
//    printf("求妹子2!\n");
    return 1;
}*/

int Pop(SqStack index,ElemType e)
//  出栈操作,将出栈的元素保存到e中
{
    if(index->top==index->base)
    {
        printf("Stack is Empty!There is no Element!");
    }
    e=*(--index->top);
    return 1;
}
个人感觉从入栈函数,和求栈的长度函数中看程序中指针没问题,为什么出栈函数有错误?求高手解释下,顺便问问 销毁顺序栈DestroyStack()的写法,十分感谢!
搜索更多相关主题的帖子: include 
2011-06-06 23:11
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:3 
程序代码:
int Pop(SqStack index, ElemType * e)    // 少了小星星
//  出栈操作,将出栈的元素保存到e中
{
    if(index->top==index->base)
    {
        printf("Stack is Empty!There is no Element!");
    }
    *e=*(--index->top);
    return 1;
}


其他地方楼主自己改
2011-06-06 23:23
变幻小子
Rank: 6Rank: 6
来 自:广东陆丰
等 级:侠之大者
帖 子:188
专家分:473
注 册:2011-3-4
收藏
得分:3 
学习

明天的梦
2011-06-08 13:26
快速回复:求助:这段顺序栈C代码中出栈函数的错误
数据加载中...
 
   



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

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