C语言顺序栈改错
以下是我练习的一个顺序栈,但老是出错,请大侠改一下,谢谢#include<stdio.h>#include<stdlib.h>//================栈的顺序...
以下是我练习的一个顺序栈,但老是出错,请大侠改一下,谢谢
#include <stdio.h>
#include <stdlib.h>
//================栈的顺序存储================
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 1
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//——————构造一个空栈——————
void InitStack(SqStack *S)
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//为base数组分配空间
SElemType SElem[STACK_INIT_SIZE];
S->base=SElem;
if(!S->base) printf("分配内存失败!");
S->top=S->base;//将top指针指向base
S->stacksize=STACK_INIT_SIZE;//stacksize值设为刚刚申请的长度
}
void Push(SqStack *S,SElemType e)
{
printf("开始入栈 \n");
if(S->top - S->base >= S->stacksize)//如果top-base大于等于stacksize就是内存分配不够了
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base) printf("重新分配失败!");
S->top = S->base + S->stacksize;
S->stacksize+=STACKINCREMENT;
}
//*S->top++ =e;//!!!!!!!!!!??????????????
*(S->top) = e; printf("S->top = e \n");
S->top++; printf("S->top++ \n");
printf("入栈成功 \n");
}
//————————遍历显示————————————
void Display(SqStack *S)
{
while(S->top > S->base)
{
printf("%d ",*(S->base));
S->base++;
}
printf("(成功)\n ");
}
int main()
{
SqStack S;
InitStack(&S);
Push(&S,1);
Push(&S,2);
Push(&S,3);
Display(&S);
} 展开
#include <stdio.h>
#include <stdlib.h>
//================栈的顺序存储================
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 1
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//——————构造一个空栈——————
void InitStack(SqStack *S)
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//为base数组分配空间
SElemType SElem[STACK_INIT_SIZE];
S->base=SElem;
if(!S->base) printf("分配内存失败!");
S->top=S->base;//将top指针指向base
S->stacksize=STACK_INIT_SIZE;//stacksize值设为刚刚申请的长度
}
void Push(SqStack *S,SElemType e)
{
printf("开始入栈 \n");
if(S->top - S->base >= S->stacksize)//如果top-base大于等于stacksize就是内存分配不够了
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base) printf("重新分配失败!");
S->top = S->base + S->stacksize;
S->stacksize+=STACKINCREMENT;
}
//*S->top++ =e;//!!!!!!!!!!??????????????
*(S->top) = e; printf("S->top = e \n");
S->top++; printf("S->top++ \n");
printf("入栈成功 \n");
}
//————————遍历显示————————————
void Display(SqStack *S)
{
while(S->top > S->base)
{
printf("%d ",*(S->base));
S->base++;
}
printf("(成功)\n ");
}
int main()
{
SqStack S;
InitStack(&S);
Push(&S,1);
Push(&S,2);
Push(&S,3);
Display(&S);
} 展开
1个回答
展开全部
你既然已经分配了空间,还要定义数组干吗,还有现在的电脑一般不会存在分配不成功的,再说如果分配不成功也不是你那样写的,那就是直接跳出函数了。
还有display()像我一般是通过pop函数输出,这样更能体现栈的先进后出。
给你稍微改了一下。
#include <stdio.h>
#include <stdlib.h>
//================栈的顺序存储================
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 1
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//——————构造一个空栈——————
void InitStack(SqStack *S)
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//为base数组分配空间
S->top=S->base;//将top指针指向base
S->stacksize=STACK_INIT_SIZE;//stacksize值设为刚刚申请的长度
}
void Push(SqStack *S,SElemType e)
{
printf("开始入栈 \n");
if(S->top - S->base >= S->stacksize)//如果top-base大于等于stacksize就是内存分配不够了
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
S->stacksize+=STACKINCREMENT;
}
//*S->top++ =e;//!!!!!!!!!!??????????????
*(S->top) = e; printf("S->top = %d \n",e);
S->top++; printf("S->top++ \n");
printf("入栈成功 \n");
}
//————————遍历显示————————————
void Display(SqStack *S)
{
while(S->top > S->base)
{
printf("%d ",*(S->base));
S->base++;
}
printf("(成功)\n ");
}
int main()
{
SqStack S;
InitStack(&S);
Push(&S,1);
Push(&S,2);
Push(&S,3);
Display(&S);
return 0;
}
还有display()像我一般是通过pop函数输出,这样更能体现栈的先进后出。
给你稍微改了一下。
#include <stdio.h>
#include <stdlib.h>
//================栈的顺序存储================
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 1
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//——————构造一个空栈——————
void InitStack(SqStack *S)
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//为base数组分配空间
S->top=S->base;//将top指针指向base
S->stacksize=STACK_INIT_SIZE;//stacksize值设为刚刚申请的长度
}
void Push(SqStack *S,SElemType e)
{
printf("开始入栈 \n");
if(S->top - S->base >= S->stacksize)//如果top-base大于等于stacksize就是内存分配不够了
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
S->stacksize+=STACKINCREMENT;
}
//*S->top++ =e;//!!!!!!!!!!??????????????
*(S->top) = e; printf("S->top = %d \n",e);
S->top++; printf("S->top++ \n");
printf("入栈成功 \n");
}
//————————遍历显示————————————
void Display(SqStack *S)
{
while(S->top > S->base)
{
printf("%d ",*(S->base));
S->base++;
}
printf("(成功)\n ");
}
int main()
{
SqStack S;
InitStack(&S);
Push(&S,1);
Push(&S,2);
Push(&S,3);
Display(&S);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询