C语言 的各位兄弟 给帮帮忙?100分,关于堆栈

来源:百度知道 编辑:UC知道 时间:2024/06/07 14:35:10
#include "stdio.h"
#define MAXSIZE 4
struct Stack{
int top;
int stackList[MAXSIZE];
};
struct Stack *creatStcakList()/*建立一个空的堆栈*/
{struct Stack *tem;
if((tem=(struct Stack*)malloc(sizeof(struct Stack)))==NULL)
{printf("system cann't malloc space");
exit(1);
}
else
tem->top=-1;
printf("1.tem->top: %d int create\n",tem->top);/*大家帮忙看看怎么回事???*/
tem->stackList[-1]=NULL;/*这段运行之手 tem->top的值就改变了?*/
printf("2.tem->top: %d int create\n",tem->top);

return tem;
}

void push(struct Stack *stack,int item)/*压入堆栈*/
{struct Stack *ptr;
ptr=stack;
if(ptr->top==MAXSIZE-1)
{printf("stack is full cann't push anyitem");
exit(1);
}
else
{ptr->top++;
ptr->stackList[ptr->top]=item;
}
}

int pop(struct Stack *st

stackList[MAXSIZE]; 是0<=MAXSIZE<4
stackList[-1] = NULL; //这里有必要么?
而且堆栈的初始化,这样就可以了:
/* 栈的初始化 */
int init_stack(struct Stack *head)
{
head->top = -1;
return 0;
}
或者就用你的哪个:修改如下:
void creatStcakList(struct Stack *stack)
{
stack->top = -1;
}

太长了 太长了~ 头疼 不看了

这个干什么的 ?

那个tem->stackList[-1]=NULL;的意思不就是tem->top=0;吗?