c语言数据结构(5)——栈-实现顺序栈的所有代码

时间:2024-03-02 22:45:06
typedef int SDataType;
typedef struct SeqStack
{
	SDataType val[MAXSIZE];
	int top;
}SeqStack;

void ListStackInit(ListStack* stack)
{
	assert(stack);
	stack->top = NULL;
	stack->lenth = 0;
}

void ListStackPush(ListStack* stack, LDataType e)
{
	StackNode* newnode = malloc(sizeof(StackNode));
	assert(newnode);
	newnode->val = e;
	newnode->next = stack->top;
	stack->top = newnode;
	stack->lenth++;
}

bool ListStackEmpty(ListStack* stack)
{
	return stack->top == NULL;
}

void ListStackPop(ListStack* stack)
{
	if (!ListStackEmpty(stack))
	{
		perror("stack is empty\n");
		return;
	}
	StackNode* del = stack->top;
	stack->top = stack->top->next;
	free(del);
}

LDataType ListStackTop(ListStack* stack)
{
	assert(stack);
	return stack->top->val;
}