栈与栈的简单应用

栈是比较简单的数据结构,这里简单列举了栈的几项常见功能,并且有栈的实际应用
具体包括进制转换/括号匹配是否合法/编辑文档的回退与清行/中缀表达式转后缀表达式
这些都是常见应用,表达式转换只是处理了加减乘除以及指数运算符,更复杂的运算符程序需要进行较大改动。
注意在定义栈的类型,不同的应用ElemType可能不同,需要更改

include <stdio.h>

include<stdlib.h>

include<stdbool.h>

include<string.h>

typedef char ElemType;

typedef struct
{
	ElemType* base;
	int top;
	int SatckSize;
}Stack;
typedef Stack* PStack;



void InitStack(PStack S, int size);//初始化栈
bool Pop(PStack S, ElemType* PopItem);//入栈
bool Push(PStack S, ElemType PushItem);//出栈
ElemType GetTop(PStack S, ElemType* TopItem);//得到栈顶元素
void DestoryStack(PStack S);//销毁栈
void ClearStack(PStack S);//清空栈
bool IsEmpty(PStack S);//判断栈是否为空

//ElementType为int型
/*void Conversion(PStack S, int n, int q)//实现进制转换,n表示数,q表示进制
{
	while (n)
	{
		Push(S, n % q);
		n = n / q;
	}
	int a;
	while (!IsEmpty(S))
	{
		Pop(S, &a);
		printf("%d", a);
	}
	printf("\n");
}*/
//ElementType为char型
/*bool Matching(char* str, PStack S)//给一组括号判断是否合法
{
	int i = 0,len=strlen(str);
	char ch;
	while (i < len)
	{
		ch = str[i++];
		if(ch=='('||ch=='['||ch=='{') Push(S, ch);
		else
		{
			char Top;
			switch (ch)
			{
			case')': {if (!IsEmpty(S)&&GetTop(S, &Top)  == '(')Pop(S, &Top); else return false; break; }
			case']': {if (!IsEmpty(S)&& GetTop(S, &Top)  == '[')Pop(S, &Top); else return false; break; }
			case'}': {if (!IsEmpty(S)&& GetTop(S, &Top)  == '{')Pop(S, &Top); else return false; break; }
			default:
				break;
			}
		}
	}
	if (IsEmpty(S)) return true;


}*/
//ElemType为char型,编辑文档,将字符放在缓冲栈内,#代表回退一个字符,@代表清空一行,实际应用起来该函数不大现实
/*void Editing(char* str, PStack S)//用符号$标志着字符输入完毕
{
	int i = 0;
	char ch = str[i++];
	char word;
	while (ch != '$')
	{
		while (ch != '\n'&&ch!='$')
		{
			switch (ch)
			{
			case'@':ClearStack(S); break;
			case'#':Pop(S, &ch); break;
			default:
				Push(S, ch);
				break;
			}
			ch = str[i++];
		}
		//完成后及时对栈元素进行处理,之后有一个换行
		/*while (!IsEmpty(S))
		{
			Pop(S, &word);
			printf("%c", word);
		}printf("\n");/*
		ClearStack(S);
		if(ch!='$') ch = str[i++];
	}
}
*/
//ElementType为char型
/*void Transform(char* str, PStack S)//表达式转换,中缀转后缀
{
	char ch;
	char Item;
	int len = strlen(str),i=0;
	Push(S, '#');
	while (i<len)
	{
		ch = str[i++];
		if (ch >= '0' && ch <= '9') printf("%c ", ch);
		else if (ch == '+'||ch=='-'){
			while (GetTop(S, &Item) != '(' && Item != '#') { Pop(S, &Item); printf("%c ", Item); }
			Push(S, ch);
		}
		else if (ch == '*' || ch == '/') {
			while (GetTop(S, &Item) == '^') { Pop(S, &Item); printf("%c ", Item); }
			Push(S, ch);
		}
		else if (ch == '('||ch=='^') {
			Push(S, ch);
		}
		else if (ch == ')') {
			while (GetTop(S, &Item) != '(') { Pop(S, &Item); printf("%c ", Item); }
			Pop(S, &Item);
		}
		else {
			while (GetTop(S, &Item) != '#') { Pop(S, &Item); printf("%c ", Item); }
		}
	
	}
	while (GetTop(S, &Item) != '#') { Pop(S, &Item); printf("%c ", Item); }
}
*/

int main()
{
	PStack S;
	S = (PStack)malloc(sizeof(Stack));
	int size = 50;
	InitStack(S,size);

	//char str[100] = "whli##ilr#e(s#*s)\noutcha@putchar(*s=#++);$";
	//Editing(str, S);

	//char str[10] = {'(','(','(',')',')','{','}',')' };
	//if (Matching(str, S)) printf("Success");
	//else printf("Failure");

	//Conversion(S, 48, 2);

	/*char str2[50] = "4/2+(3-9^2)*8=";
	Transform(str2, S);*/


}

void InitStack(PStack S, int size)
{
	if (!S) exit(1);
	if (size > 0) S->base = (ElemType*)malloc(sizeof(ElemType) * size);
	if (!S->base)exit(1);
	S->SatckSize = size;
	S->top = 0;

}

bool Pop(PStack S, ElemType* PopItem)
{
	if (S->top <= 0)
	{
		printf("Stack is empty\n"); return false;
	}
	S->top--;
	*PopItem = *(S->base + S->top);
	return true;
}

bool Push(PStack S, ElemType PushItem)
{
	if (S->top >= S->SatckSize)
	{
		printf("Stack is full\n"); return false;
	}
	*(S->base + S->top) = PushItem;
	S->top++;
	return true;
}

ElemType GetTop(PStack S, ElemType* TopItem)
{
	if (S->top <= 0)
	{
		printf("NO Item\n");
		return false;
	}
	*TopItem = *(S->base + S->top - 1);
	return *TopItem;
}

void DestoryStack(PStack S)
{
	free(S->base);
	free(S);
}

void ClearStack(PStack S)
{
	S->top = 0;
}

bool IsEmpty(PStack S)
{
	if (S->top == 0) return true;
	else return false;
}
posted @ 2021-03-28 17:13  empty_thought  阅读(93)  评论(0)    收藏  举报