How the C language simulation stack

Copyright: please indicate the source! https://blog.csdn.net/ZouHuiDong/article/details/90343026

Stack is a storage type, last-out, very common and very popular. To implement a stack using C language is very simple, just an array and an int variable on it.

A simple stack

what do you need

  • A char array - for simulating Stack
  • An int variable - location to save the top of the stack
char Value[12];//一个大小为12的栈
int Head;//用于保存栈顶的位置

Simple operation

  • In the top of the stack to achieve added value
  • To achieve extraction (deleting) the top of the stack value

To add value, only you need to add value in the top of the stack and the stack add 1 can be.

//添加值,假如要添加chNew的值
char chNew;
scanf("%c",&chNew);//得到要添加的值

chValue[Head] = chNew;//添加值
Head ++;//栈顶上移

Similarly, to extract or delete values, only necessary to subtract a value below the stack (the stack because the stack above Head is the most a value above), and the stack 1 can be reduced.

//如果要提取出来就先把栈顶的值存到另外一个变量里(如果提取出来只需要显示一下就不需要再多创建一个变量):
char chGet = chValue[Head-1];
printf("你提取出来的值:%c",chGet);
///////////////////////////////////////////////////////
chValue[Head-1] = 0;//删去值
Head --;//栈顶下移

The stack may be determined whether the upper or lower limit:

//在插入时判断是否达到上限:
if(Head>=12)//因为栈的大小是12,可根据不同需要来更改
{
	printf("栈已满,不可以继续添加!");
	return ;//不再往下执行
}

//在删去时判断是否达到下限:
if(Head==0)
{
	printf("栈已空,不可以继续删除!");
	return ;//不再往下执行
}

A more perfect program

To achieve a more perfect stack simulation program, we need to summarize:

  • A more kind of the main interface, the case of displaying the stack
  • The user decides to wait in the main interface to the stack what action
  • Add and delete functions

Code:

#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <graphics.h>

struct stack //结构体 栈
{
	char chStack[12];//栈的数组
	int head;//栈的头
};

struct stack Stack;//创建栈

void Print(char chValue[12])//打印界面
{
	system("cls");
	printf("这是栈:");
	int i;
	for (i=11;i>=0;i--)
		printf("\n[%c]",chValue[i]);
	printf("\n\n按i增加数值,按p弹出最上面的数值");
}

void getinput()//获取用户输入
{
	char chGet;
	while (1)
	{
		if (kbhit())//如果发现用户有按下按键
		{
			chGet = getch();//得到用户按下的按键
			char chValue[1];
			if (chGet == 'i')//如果是i,增加值
			{
				if (Stack.head >= 12)//如果达到上限,不能再加
				{
					MessageBox(NULL,"已经是栈顶了!","提示",MB_OK);
					return;
				}
				//询问要增加的值
				InputBox(chValue,2,"请输入要增加的字符","增加数值",NULL,0,0,true);
				Stack.chStack[Stack.head] = chValue[0];//增加值
				Stack.head ++;//栈顶上移
			}
			else if (chGet == 'p')//如果是p,删除值
			{
				if (Stack.head == 0)//如果已经到下限,就不能再减
				{
					MessageBox(NULL,"已经是栈底了!","提示",MB_OK);
					return;
				}
				Stack.chStack[Stack.head-1] = 0;//移除值
				Stack.head --;//栈顶下移
			}
			return;
		}
	}
	
}

int main()
{
	system("title 模拟栈程序");
	//初始化
	int i;
	for (i=0;i<12;i++)
		Stack.chStack[i] = 0;	
	Stack.head = 0;
	
	while (1)
	{
		Print(Stack.chStack);
		getinput();
	}
	
	getch();
	return 0;
}

Note: <graphics.h> is not built-in header files. This is EasyX library header files, you can go to www.easyx.cn download. MessageBox InputBox code and uses the header file itself can be replaced with another. But if you use EasyX library, then you can use a graphical interface, the better.

Run shot:
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/ZouHuiDong/article/details/90343026