首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C数据结构, Status GetTop(SqStack S,SElemType &e)函数,S.top-1与S.top-为啥S.top-不行

2012-11-12 
C数据结构, Status GetTop(SqStack S,SElemType &e)函数,S.top-1与S.top--?为什么S.top--不行?是不是e*(S

C数据结构, Status GetTop(SqStack S,SElemType &e)函数,S.top-1与S.top--?为什么S.top--不行?
是不是e=*(S.top--)运算顺序问题?刚才试了一下,e=*(--S.top)就可以了



#include<malloc.h>
#include<stdio.h> 
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量

typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等

struct SqStack
{
  SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
  SElemType *top; // 栈顶指针
  int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈

Status InitStack(SqStack &S)  
{  
  S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(struct SqStack));
S.top=S.base;
S.stacksize=0;
return OK;
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
// 请补全代码

}

Status Push(SqStack &S,SElemType e)  
{
  int *newbase;
if(S.stacksize>=STACK_INIT_SIZE)
{
newbase=(int *)realloc(S.base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(struct SqStack));
S.base=newbase;

}
  S.base[S.stacksize]=e;
  S.stacksize++;
  S.top++;
  return OK;
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码

}

Status Pop(SqStack &S,SElemType &e)  
{
if(S.top==S.base)
return ERROR;
else
{
  e=*(S.top-1);///////////////////////////////////e=S.base[S.stacksize];????????????????????????????????????//
S.top--;
S.stacksize--;////////////////////////////////////free()caozuobudong

}
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码

}

Status GetTop(SqStack S,SElemType &e)  

if(S.top==S.base)
return ERROR;
else
{
e=*(S.top-1);//////////////S.top--???????????????????????????????????????????????????????

}
return OK;
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
// 请补全代码

}






int StackLength(SqStack S) 
{
return S.stacksize;
// 返回栈S的元素个数
// 请补全代码

}

Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
SElemType *p=(SElemType *)malloc(sizeof(SElemType)); 
p=S.top; //请填空
if(p==S.base)printf("The Stack is Empty!"); //请填空
else
{
printf("The Stack is: ");
p--;
while(p>=S.base) //请填空
{
printf("%d ", *p);
p--; //请填空
}
}
printf("\n");
return OK;
}

int main()
{
  int a;
  SqStack S;
SElemType x, e;
  if(InitStack(S)) // 判断顺序表是否创建成功,请填空
{
printf("A Stack Has Created.\n");
}
while(1)
{
  printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空
else printf("The Element %d is Successfully Pushed!\n", x); 
break;
case 2: if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
else printf("The Element %d is Successfully Poped!\n", e);
break;
case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
else printf("The Top Element is %d!\n",e);
break;
case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空
break;
case 5:StackTraverse(S); //请填空
break;
case 0: return 1;
}
}
}

------解决方案--------------------


探讨
是不是e=*(S.top--)运算顺序问题?刚才试了一下,e=*(--S.top)就可以了

[解决办法]
是不是e=*(S.top--)运算顺序问题?刚才试了一下,e=*(--S.top)就可以了

个人感觉这两句是一样的效果的.

看了一下,这段代码有个地方不合理,stacksize应该是保存已分配空间的大小而不是存放栈中元素个数,元素个数可以用top - base得到

热点排行