Basic operation of stack (C language)

This article only has code, which introduces the basic operations of the stack.

It has been debugged without any major problems.
If there are mistakes, please criticize and correct.

First, the implementation and representation of the stack:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef int SElemType;
typedef int Status;

#define STACK_INIT_SIZE 100//储存空间初始分配量
#define STACKINCREMENT 10  //储存空间分配增量
#define OK 1
#define ERROR 0

//栈的表示
typedef struct
{
	SElemType* base;
	SElemType* top;
	int stacksize;
}SqStack;

Second, the basic operation of the stack:

//函数声明
Status InitStack(SqStack* S);//构造一个空栈InitStack(S)

Status DestroyStack(SqStack* S);//销毁栈DestroyStack(S)

Status ClearStack(SqStack* S);//把S置为空栈ClearStack(S)

Status StackEmpty(SqStack* S);//判断是否为空栈StackEmpty(S)

Status StackLength(SqStack* S);//返回S的元素个数,即栈的长度StackLength(S)

Status GetTop(SqStack* S, SElemType* e);//返回栈顶元素GetTop(S, &e)

Status Push(SqStack* S, SElemType e);//插入元素e为新的栈顶元素Push(S, e)

Status Pop(SqStack* S, SElemType* e);//删除栈顶元素并用e返回其值Pop(S, &e)

Status StackTraverse(SqStack* S);//遍历栈,输出其值StackTraverse(S)

 1. Construct an empty stack:

/*---------------------------------------------------------------------------------------
功能:构造一个空栈
参数:1、栈
输出:OK、ERROR
*/
//构造一个空栈InitStack(S)
Status InitStack(SqStack* S)
{
	// 分配初始空间
	S->base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
	if (S->base)
	{
		S->stacksize = STACK_INIT_SIZE;//栈的最大长度等于初始长度
		S->top = S->base;// 栈顶与栈底相同
	}
	else
	{
		return ERROR;
	}
	return OK;
}

2. Destroy the stack: 

/*---------------------------------------------------------------------------------------
功能:销毁栈
参数:1、栈
输出:OK、ERROR
*/
//销毁栈DestroyStack(S)
Status DestroyStack(SqStack* S)
{
	free(S->base);
	S->base = S->top = NULL;
	S->stacksize = 0;
	return OK;
}

3. Set S as an empty stack:

/*---------------------------------------------------------------------------------------
功能:把S置为空栈
参数:1、栈
输出:OK、ERROR
*/
//把S置为空栈ClearStack(S)
Status ClearStack(SqStack* S)
{
	S->top = S->base;
	return OK;
}

4. Determine whether it is an empty stack:

/*---------------------------------------------------------------------------------------
功能:判断是否为空栈
参数:1、栈
输出:OK、ERROR
*/
//判断是否为空栈StackEmpty(S)
Status StackEmpty(SqStack* S)
{
	if (S->top == S->base)
	{
		return OK;
	}
	return ERROR;
}

 5. Return the number of elements in S, that is, the length of the stack:

/*---------------------------------------------------------------------------------------
功能:返回S的元素个数,即栈的长度
参数:1、栈
输出:OK、ERROR
*/
//返回S的元素个数,即栈的长度StackLength(S)
Status StackLength(SqStack* S)
{
	return S->top - S->base;
}

6. Return the top element G of the stack:

/*---------------------------------------------------------------------------------------
功能:返回栈顶元素G
参数:1、栈 2、元素
输出:OK、ERROR
*/
//返回栈顶元素GetTop(S, &e)
Status GetTop(SqStack* S, SElemType* e)
{
	if (S->top == S->base)
	{
		return ERROR;
	}
	*e = *(S->top - 1);
	return OK;
}

 7. Insert element e as the new top element of the stack:

/*---------------------------------------------------------------------------------------
功能:插入元素e为新的栈顶元素
参数:1、栈 2、元素
输出:OK、ERROR
*/
//插入元素e为新的栈顶元素Push(S, e)
Status Push(SqStack* S, SElemType e)
{
	if (S->top - S->base >= S->stacksize)//栈满
	{
		S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
		if (S->base)
		{
			// 栈顶指针为栈底指针加上栈之前的最大长度
			S->top = S->base + S->stacksize;
			// 栈当前的最大长度等于栈之前的最大长度与增加的长度之和
			S->stacksize += STACKINCREMENT;
		}
		else
		{
			return ERROR;
		}
	}
	*S->top++ = e;//先赋值,后栈顶指针上移
	return OK;
}

8. Delete the top element of the stack and return its value with e:

/*---------------------------------------------------------------------------------------
功能:删除栈顶元素并用e返回其值
参数:1、栈 2、元素
输出:OK、ERROR
*/
//删除栈顶元素并用e返回其值Pop(S, &e)
Status Pop(SqStack* S, SElemType* e)
{
	if (S->top == S->base)
	{
		return ERROR;
	}
	*e = *--S->top; // 栈顶指针先下移,后赋值
	return OK;
}

 9. Traverse the stack and output its value:

/*---------------------------------------------------------------------------------------
功能:遍历栈,输出其值
参数:1、栈 2、元素
输出:OK、ERROR
*/
//遍历栈,输出其值StackTraverse(S)
Status StackTraverse(SqStack* S)
{
	SElemType* p = S->base;
	while (S->top > p)
	{
		printf("%d ", *p++);
	}
	printf("\n");
	return OK;
}

 3. Test the main program:

void menu()
{
	printf("******************************************\n");
	printf("****** 1.创建空栈    2.销毁栈       ******\n");
	printf("****** 3.清空栈      4.判断是否为空 ******\n");
	printf("****** 5.求栈的长度  6.返回栈顶元素 ******\n");
	printf("****** 7.压栈        8.删除栈顶元素 ******\n");
	printf("****** 9.输出栈      0.退出         ******\n");
	printf("******************************************\n");
}


int main()
{
	SqStack q,* S;
	S = &q;
	SElemType e = 0;
	int input = 1;
	menu();
	while (input)
	{
		printf("请输入要执行的操作:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			if (InitStack(S))
			{
				printf("创建成功!!!\n");
			}
			else
			{
				printf("创建失败~\n");
			}
			break;
		case 2:
			if (DestroyStack(S))
			{
				printf("销毁成功!!!\n");
			}
			else
			{
				printf("销毁失败~\n");
			}
			break;
		case 3:
			if (ClearStack(S))
			{
				printf("栈已清空!\n");
			}
			else
			{
				printf("清空失败~\n");
			}
			break;
		case 4:
			if (StackEmpty(S))
			{
				printf("栈为空!!!\n");
			}
			else
			{
				printf("栈不为空~\n");
			}
			break;
		case 5:
			printf("栈的长度为:%d\n", StackLength(S));
			break;
		case 6:
			if (GetTop(S, &e))
			{
				printf("栈顶元素为:%d\n", e);
			}
			else 
			{
				printf("没有栈顶元素~\n");
			}
			break;
		case 7:
			printf("请输入要插入的元素值:>");
			scanf("%d", &e);
			if (Push(S, e))
			{
				printf("元素插入成功!\n");
			}
			else
			{
				printf("插入失败~\n");
			}
			break;
		case 8:
			if (Pop(S, &e))
			{
				printf("栈顶元素删除成功\n");
			}
			else
			{
				printf("栈为空,无法删除~\n");
			}
			printf("删除的元素为:%d\n", e);
			break;
		case 9:
			printf("此时栈内元素为:");
			StackTraverse(S);
			break;
		case 0:
			printf("退出成功!!!\n");
			break;
		default:
			printf("输入错误,请重新输入!!!\n");
			break;
		}
	}
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/absorb2601078490/article/details/124950481
Recommended