| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 417 人关注过本帖
标题:压栈问题,求助
只看楼主 加入收藏
luoyang2404
Rank: 2
等 级:论坛游民
帖 子:19
专家分:27
注 册:2010-11-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
压栈问题,求助
请高手帮忙讲解一下下面这个压栈问题

#define  STACK_SIZE  100    /*  栈初始向量大小  */
#define STACKINCREMENT 10   /*  存储空间分配增量  */
#typedef  int  ElemType ;
typedef struct sqstack
{   ElemType  *bottom;     /*  栈不存在时值为NULL  */
ElemType  *top;      /*  栈顶指针  */
int   stacksize ;      /*  当前已分配空间,以元素为单位  */
}SqStack ;

typedef struct sqstack
{   ElemType  *bottom;     /*  栈不存在时值为NULL  */
ElemType  *top;      /*  栈顶指针  */
int   stacksize ;      /*  当前已分配空间,以元素为单位  */
}SqStack ;

栈的初始化
Status Init_Stack(void)
{   SqStack  S ;
S.bottom=(ElemType *)malloc(STACK_SIZE *sizeof(ElemType));
if (! S.bottom) return  ERROR;
S.top=S.bottom ;    /*  栈空时栈顶和栈底指针相同  */
S. stacksize=STACK_SIZE;
return OK ;
}

Status push(SqStack S , ElemType  e)
   {  if  (S.top-S.bottom>=S. stacksize-1)
{   S.bottom=(ElemType *)realloc((S. STACKINCREMENT+STACK_SIZE) *sizeof(ElemType));   /*  栈满,追加存储空间  */
if (! S.bottom)  return  ERROR;
S.top=S.bottom+S. stacksize ;
S. stacksize+=STACKINCREMENT ;
}  
*S.top=e;  S.top++ ; /*  栈顶指针加1,e成为新的栈顶 */
return OK;
}
我的疑问是在栈这个结构体声明中根本没有蓝色加粗部分的项,怎么这里用上了?
请高手不吝赐教,谢谢
搜索更多相关主题的帖子: bottom 空间 元素 
2010-11-16 15:03
韭菜
Rank: 2
等 级:论坛游民
帖 子:51
专家分:94
注 册:2010-9-26
收藏
得分:10 
楼主 我没有按照你的思路写  我的程序是这样的  你看看行不
程序代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct stack
{
int *bottom;
int *top;
int stacksize;    //表示当前栈中有多少项
}Stack;
int Initialization(Stack *s)
{
    s->bottom=(int *)malloc(sizeof(int));
    if(!s->bottom)    return 1;
    s->top=s->bottom;
    s->stacksize=0;
    return 0;
}
int push(Stack *s)
{
    int i;
while(1)
{
    printf("Please input the number to push :");
    if(scanf("%d",&i)==EOF)    return 0;;
    if(s->stacksize==0)
    {
        *s->bottom=*s->top=i;
        s->stacksize=1;
    }
    else
    {
        *(++s->top)=i;
        s->stacksize=s->stacksize+1;
    }
}
return 0;
}
int pop(Stack *s)
{
    while(s->top!=s->bottom)    
    {
        printf("%d  ",*s->top);
        s->top--;
    }
    printf("%d \n",*s->top);
    return 0;
}

int main()
{
    Stack s;
Initialization(&s);
push(&s);
pop(&s);
return 0;
}
2010-11-16 17:32
xiaomarn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:348
专家分:2026
注 册:2009-3-18
收藏
得分:10 
你确定你的代码没错?
S.bottom=(ElemType *)realloc(S.bottom,(S.stacksize + STACKINCREMENT) *sizeof(ElemType));
应该是这样吧!
2010-11-16 18:29
快速回复:压栈问题,求助
数据加载中...
 
   



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

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