C语言关于栈的程序求助

来源:百度知道 编辑:UC知道 时间:2024/05/24 01:30:07
自己写了这么多,但是在主函数里的调用,我就不会了,试了很久都不行。希望高手帮忙。我不确定前面写的有没有出错,如果有,请说一下。另外,帮分析一下如何写主函数。
#include<stdio.h>
#include<stdlib.h>
#define sqstack_maxsize 100 /*顺序栈的容量*/
typedef struct sqstack
{
int data[100];
int top;
}SqStackTp;

SqStackTp InitStack(SqStackTp s) /*初始化*/
{

s.top = 0;
return s;
}

void Push(SqStackTp *s, int x) /*进*/
{
SqStackTp *l;
l = (SqStackTp *) malloc(sizeof(SqStackTp));
l->data[l->top] = x;
l->data[l->top] = s->data[s->top + 1];
s = l;
}

void Pop(SqStackTp *s, int *x)
{
if(s->top != 0)
{
*x = s->data[s->top];
s->top = s->top - 1;
}
else
printf("Error!/n");
}

int EmptyStack(SqStackTp *s)
{
if(s->top == 0)
return(1);
else
return(0);

1、将
int data[100];
修改为
int data[sqstack_maxsize];
2、将函数InitStack()修改为
void InitStack(SqStackTp *s) /*初始化*/
{
s->top = 0;
}
3、将函数Push()修改为
void Push(SqStackTp *s, int x) /*进*/
{
if (s->top == sqstack_maxsize)
{
printf("stack is full\n");
}
else
{
s->data[s->top] = x;
s->top++;
}
}
4、将函数GetTop()修改为
void GetTop(SqStackTp *s, int *x)
{
if(s->top != 0)
{
*x = s->data[s->top];
}
else
printf("Error!/n");
}
5、将函数OutStack()修改为
void OutStack(SqStackTp *s)
{
int i;
if(s->top == 0)
printf("The stack is null./n");
for(i = s->top-1; i >= 0; i--)
printf("%2d %6d\n",i,s->data[i]);
}
6、在函数main()中将
struct sqstack *s;
修改为
struct sqstack s; // 定义顺序栈
InitStack(&s