栈的数组(C语言)实现

在上一篇博客中,笔者实现了栈的链表结构,在这一篇博客中,笔者将实现栈的数组结构,功能(入栈、出栈、计算栈长度、返回栈顶元素等)与上一篇基本类似,可对比着看。本次工程的运行环境为:Windows 10。集成开发工具为:visual studio 2019。

1.头文件Stack_Array.h

#pragma once
#ifndef STACK_ARRAY_H
#define STACK_ARRAY_H

#define EmptyTop  -1


struct Node;
typedef struct Node* Stack;
typedef int ElementType;

struct Node
{
	int Capacity;
	int TopOfStack;
	ElementType* Array;
};


Stack CreatStack(int NumOfElement);//创造空栈,并且返回头指针

void DisposeStack(Stack S);//释放栈

int IsEmpty(Stack S);//判断栈是否为空

int IsFull(Stack S);//判断栈是否为满

void MakeEmpty(Stack S);//置空

int LengthOfStack(Stack S);//计算栈长度

void PrintStack(Stack  S);//打印栈

void Push(Stack S, ElementType data);//入栈

void Pop(Stack S);//出栈

ElementType Top(Stack S);//返回栈顶元素


#endif // !STACK_LIST_H

2.源文件Stack_Array.c

#include<stdio.h>
#include<malloc.h>
#include"Stack_Array.h"



Stack CreatStack(int NumOfElement)//创造空栈,并且返回头指针
{
	Stack S;
	S = malloc(sizeof(struct Node));
	if (S == NULL)
	{
		printf("Stack malloc Error: out of space!!\n");
		exit(1);
	}

	S->Array = malloc(sizeof(ElementType) * NumOfElement);
	if (S->Array == NULL)
	{
		printf("Array malloc Error:out of space!!\n");
		exit(1);
	}

	S->Capacity = NumOfElement;
	S->TopOfStack = EmptyTop;

	return S;
}



void DisposeStack(Stack S)//释放栈
{
	if (S != NULL)
	{
		free(S->Array);
		free(S);
	}
}



int IsEmpty(Stack S)//判断栈是否为空
{
	return S->TopOfStack == EmptyTop;
}



int IsFull(Stack S)//判断栈是否为满
{
	return S->TopOfStack == S->Capacity - 1;
}



void MakeEmpty(Stack S)//置空
{
	S->TopOfStack = EmptyTop;
}



int LengthOfStack(Stack S)//计算栈长度
{
	return S->TopOfStack + 1;
}



void PrintStack(Stack  S)//打印栈
{
	int n = S->TopOfStack;
	if (IsEmpty(S))
	{
		printf("Print Warning: the stack is empty!!!\n");
	}

	else
	{
		while (n != EmptyTop)
		{
			printf("%d ", S->Array[n]);
			n--;
		}
		printf("\n");
	}
}



void Push(Stack S, ElementType data)//入栈
{
	if (IsFull(S))
	{
		printf("Push Error: the stack is full!!!\n");
		exit(1);
	}

	S->Array[++S->TopOfStack] = data;
}



void Pop(Stack S)//出栈
{
	if (IsEmpty(S))
	{
		printf("Pop Error: the stack is empty!!!\n");
		exit(1);
	}

	S->TopOfStack--;
}



ElementType Top(Stack S)//返回栈顶元素
{
	if (IsEmpty(S))
	{
		printf("Top Error: the stack is empty!!!\n");
		exit(1);
	}
	return S->Array[S->TopOfStack];
}



3.主程序main.c

#include"Stack_Array.h"



int main()
{
	int len = 10;//指定栈长度
	Stack	S = CreatStack(len);//创建空栈

	for (int i = 0; i < len; i++)
	{
		Push(S, i);//入栈
	}

	PrintStack(S);//打印栈
	printf("the length of the stack is: %d\n", LengthOfStack(S));//打印栈长度
	printf("the top element of the stack is: %d\n", Top(S));//打印栈顶元素




	for (int i = 0; i < 3; i++)
	{
		Pop(S);//出栈
	}

	PrintStack(S);
	printf("the length of the stack is: %d\n", LengthOfStack(S));
	printf("the top element of the stack is: %d\n", Top(S));



	MakeEmpty(S);//栈置空
	PrintStack(S);
	printf("the length of the stack is: %d\n", LengthOfStack(S));
	printf("the top element of the stack is: %d\n", Top(S));




	return 0;
}

4.运行结果截图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值