栈的链式实现(c语言)


(一)栈的链式结构体
typedef int Status;
typedef int EleType;
typedef struct StackNode {
	EleType data;
	struct StackNode* next;
}StackNode,* LinkStackPoi;

typedef struct LinkStack {
	LinkStackPoi top;
	int count;
}LinkStack;
(二)创建一个空栈
Status InitLinkStack(LinkStack* stack)
{
	if (!stack)
	{
		return	ERROR;
	}
	stack->top = NULL;
	stack->count = 0;
	return OK;
}
(三)清空栈
Status ClearLinkStack(LinkStack* stack)
{
	if (!stack||!stack->count)
	{
		return	ERROR;
	}
	while (stack->count)
	{
		StackNode* node = stack->top;
		stack->top = node->next;
		free(node);
		stack->count--;
	}
	return OK;
}
(四)判空
Status EmptyLinkStack(LinkStack* stack) {
	if (!stack)
	{
		return ERROR;
	}
	return stack->count == 0 ? 1 : 0;
}

(五)获取元素个数
int GetLengthLinkStack(LinkStack* stack)
{
	if (!stack )
	{
		return	-1;
	}
	return stack->count;
}
(六)入栈
Status push(LinkStack* stack,EleType e)
{
	if (!stack)
	{
		return ERROR;
	}
	StackNode* node = (StackNode*)malloc(sizeof(StackNode));
	node->next = stack->top;
	node->data = e;
	stack->top = node;
	stack->count++;
	return OK;
}
(七)出栈
Status pop(LinkStack* stack,EleType *e)
{
	if (!stack && stack->count)
	{
		return	ERROR;
	}
	StackNode* node = stack->top;
	*e = node->data;
	stack->top = node->next;
	free(node);
	stack->count--;
	return OK;
}
(八)输出
void PrintfLinkStack(LinkStack* stack)
{
	if (!stack&&stack->count)
	{
		return;
	}
	StackNode* node = stack->top;
	while (node)
	{
		printf("%d,", node->data);
		node = node->next;
	}
	puts("");
	return;
}

(八)调用的主函数
int main(int argc, char *argv[])
{
	LinkStack stack;
	InitLinkStack(&stack);
	push(&stack, 1);
	push(&stack, 2);
	push(&stack, 3);
	push(&stack, 4);
	push(&stack, 5);
	puts("链栈元素:");
	PrintfLinkStack(&stack);
	printf("链栈元素个数:%d\n", GetLengthLinkStack(&stack));
	EleType e1,e2;
	pop(&stack, &e1);
	printf("弹出第一个元素:%d\n", e1);
	pop(&stack, &e2);
	printf("弹出第二个元素:%d\n", e2);
	puts("链栈元素:");
	PrintfLinkStack(&stack);
	printf("链栈元素个数:%d", GetLengthLinkStack(&stack));
	printf("\n");
	return 0;
}
全部的组合代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int EleType;
typedef struct StackNode {
	EleType data;
	struct StackNode* next;
}StackNode,* LinkStackPoi;

typedef struct LinkStack {
	LinkStackPoi top;
	int count;
}LinkStack;

Status InitLinkStack(LinkStack* stack)
{
	if (!stack)
	{
		return	ERROR;
	}
	stack->top = NULL;
	stack->count = 0;
	return OK;
}

Status ClearLinkStack(LinkStack* stack)
{
	if (!stack||!stack->count)
	{
		return	ERROR;
	}
	while (stack->count)
	{
		StackNode* node = stack->top;
		stack->top = node->next;
		free(node);
		stack->count--;
	}
	return OK;
}

Status EmptyLinkStack(LinkStack* stack) {
	if (!stack)
	{
		return ERROR;
	}
	return stack->count == 0 ? 1 : 0;
}
//获取元素个数
int GetLengthLinkStack(LinkStack* stack)
{
	if (!stack )
	{
		return	-1;
	}
	return stack->count;
}
Status GetTop(LinkStack* stack, StackNode** stackNode)
{
	if (!stack)
	{
		return	ERROR;
	}
	*stackNode = stack->top;
	return OK;
}

Status pop(LinkStack* stack,EleType *e)
{
	if (!stack && stack->count)
	{
		return	ERROR;
	}
	StackNode* node = stack->top;
	*e = node->data;
	stack->top = node->next;
	free(node);
	stack->count--;
	return OK;
}

Status push(LinkStack* stack,EleType e)
{
	if (!stack)
	{
		return ERROR;
	}
	StackNode* node = (StackNode*)malloc(sizeof(StackNode));
	node->next = stack->top;
	node->data = e;
	stack->top = node;
	stack->count++;
	return OK;
}
void PrintfLinkStack(LinkStack* stack)
{
	if (!stack&&stack->count)
	{
		return;
	}
	StackNode* node = stack->top;
	while (node)
	{
		printf("%d,", node->data);
		node = node->next;
	}
	puts("");
	return;
}
int main(int argc, char *argv[])
{
	LinkStack stack;
	InitLinkStack(&stack);
	push(&stack, 1);
	push(&stack, 2);
	push(&stack, 3);
	push(&stack, 4);
	push(&stack, 5);
	puts("链栈元素:");
	PrintfLinkStack(&stack);
	printf("链栈元素个数:%d\n", GetLengthLinkStack(&stack));
	EleType e1,e2;
	pop(&stack, &e1);
	printf("弹出第一个元素:%d\n", e1);
	pop(&stack, &e2);
	printf("弹出第二个元素:%d\n", e2);
	puts("链栈元素:");
	PrintfLinkStack(&stack);
	printf("链栈元素个数:%d", GetLengthLinkStack(&stack));
	printf("\n");
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值