读书人

栈的进栈输出出栈?解决办法

发布时间: 2012-02-26 20:19:45 作者: rapoo

栈的进栈,输出,出栈?
出栈的问题
问题见程序中的红字
[code=C/C++][/code]
#include<stdio.h>
#include <stdlib.h>
const int StackSize=100; //顺序栈的初始分配空间
typedef int ElemType; // 数据元素类型
typedef struct sq
{ ElemType data[StackSize]; // 一维数组子域
int top; // 栈顶指针子域
}SqStack; // 栈的顺序结构体类型

//函数声明
void InitStack(SqStack *&sq);
int Push(SqStack *sq,ElemType x);
void out_s(SqStack sq);
int Pop(SqStack *&sq,ElemType &x);

//函数实现
void InitStack(SqStack *&sq) //初始化栈
{
sq=(SqStack *)malloc(sizeof(SqStack));
sq->top=-1;
}
int Push(SqStack *sq,ElemType x) //进栈
{
if(sq->top==StackSize-1) return 0; //栈满
else
{
sq->top++;
sq->data[sq->top]=x;
return 1;
}
}
void out_s(SqStack *sq) //输出sq
{
int i;
printf("\n");
if(sq->top<0)
{
printf("这是一个空栈 !");
printf("\n");
}
for(i=sq->top;i>=0;i--)
printf("第 %d 个数据元素是: %d\n",i,sq->data[i]);
}

int Pop(SqStack *&sq,ElemType &x)
{
SqStack *p;
if(sq==NULL)
return 0;
else
{
p=sq->top; //怎么将栈顶位置赋给p??并删除p[/color[color=#FF0000]]//错误的地方??error C2440: '=' : cannot convert from 'int' to 'struct sq *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style castx=sq->data[sq->top]; //返回栈顶元素x
free(p); //释放栈顶p
sq->top--; //sq的长度减一??有必要吗??printf("\n栈顶元素是%d",x);
return 1;
}
}


//主函数
void main()
{
SqStack *sq; //对sq的声明
int x,i;
InitStack(sq);
printf("请输入要进入栈的数字(输入-1结束)\n");
for(i=0;i<StackSize;i++)
{
printf("data=");
scanf("%d",&x); //通过键盘输入为变量x赋值
if(x==-1) break; //注意输入
else
Push(sq,x); //将x压入栈中
}
out_s(sq);
Pop(sq,x);
printf("\n删除栈顶后的栈是:");
out_s(sq);
}


[解决办法]
p->top=sq->top;
[解决办法]
#include<stdio.h>
#include <stdlib.h>
const int StackSize=100; //顺序栈的初始分配空间
typedef int ElemType; // 数据元素类型
typedef struct sq
{ ElemType data[StackSize]; // 一维数组子域
int top; // 栈顶指针子域
}SqStack; // 栈的顺序结构体类型

//函数声明
void InitStack(SqStack *&sq);
int Push(SqStack *sq,ElemType x);
void out_s(SqStack sq);
int Pop(SqStack *sq,ElemType &x);

//函数实现
void InitStack(SqStack *&sq) //初始化栈
{
sq=(SqStack *)malloc(sizeof(SqStack));
sq->top=-1;
}
int Push(SqStack *sq,ElemType x) //进栈
{
if(sq->top==StackSize-1) return 0; //栈满
else
{
sq->top++;
sq->data[sq->top]=x;
return 1;
}
}
void out_s(SqStack *sq) //输出sq
{
int i;
printf("\n");
if(sq->top<0)
{
printf("这是一个空栈 !");
printf("\n");
}
for(i=sq->top;i>=0;i--)
printf("第 %d 个数据元素是: %d\n",i,sq->data[i]);
}

int Pop(SqStack *sq,ElemType &x)
{

if(sq==NULL)
return 0;
else
{
//p=sq->top; //怎么将栈顶位置赋给p??并删除p[/color[color=#FF0000]]//错误的地方??error C2440: '=' : cannot convert from 'int' to 'struct sq *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast x=sq->data[sq->top]; //返回栈顶元素x


//free(p); //释放栈顶p
sq->data[--sq->top]; //sq的长度减一??有必要吗?? printf("\n栈顶元素是%d",x);
return 1;
}
}


//主函数
void main()
{
SqStack *sq; //对sq的声明
int x,i;
InitStack(sq);
printf("请输入要进入栈的数字(输入-1结束)\n");
for(i=0;i<StackSize;i++)
{
printf("data=");
scanf("%d",&x); //通过键盘输入为变量x赋值
if(x==-1) break; //注意输入
else
Push(sq,x); //将x压入栈中
}
out_s(sq);
Pop(sq,x);
printf("\n删除栈顶后的栈是:");
out_s(sq);
}
[解决办法]
if(sq->top>StackSize-1)
//怎么多分配,好像没效果,我输入三个数就自动跳出这部分了,应该是再分配3个内存给顺序表啊!

----------------

楼主断点调试下,就会发现你输了3个数,i=3,i不满足i<stacksize,main函数的循环跳出,根本进不了push函数realloc。

读书人网 >软件架构设计

热点推荐