棧的數組(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.運行結果截圖
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章