栈的基本操作

#include <iostream>
#include <stdlib.h>
using namespace std;
const int INIT_SIZE=100;
const int INCREASE=10;
struct SqStack {
	int *base;
	int *top;
	int stacksize;
};
int InitStack(SqStack &S) {
	S.base=(int *)malloc(INIT_SIZE*sizeof(int));
	if (!S.base)
		exit(-1);
	S.top=S.base;
	S.stacksize=INIT_SIZE;
	return 1;
}
int GetTop(SqStack S,int e)
{
	if (S.top==S.base)
	return -1;
	e=*--S.top;
	return 1;
}
int Push(SqStack &S,int e)
{
	if (S.top-S.base>=S.stacksize) {
	S.base=(int *)realloc(S.base,(S.stacksize+INCREASE)*sizeof(int));
	if (!S.base)
	exit(-1);
	S.top=S.base+S.stacksize;
	S.stacksize+=INCREASE;
	}
	*S.top++=e;
	return 1;
}
int Pop(SqStack &S,int &e) {
	if (S.base==S.top)
	return -1;
	e=*--S.top;
	return 1;
}
void DestroyStack(SqStack &S)
{
	S.top = NULL;
	S.stacksize = 0;
	free(S.base);
	S.base = NULL;
}
void ClearStack(SqStack &S)
{
	S.top = S.base;
}
void StackEmpty(SqStack &S)
{
	if (S.top-S.base>0)
		cout << "FALSE" << endl;
	else
		cout << "TRUE" << endl;
}
void StackLength(SqStack &S)
{
	cout << S.top - S.base << endl;
}
void StackTraverse(SqStack &S)
{
	if (S.top==S.base) {
		cout << "The stack is empty." << endl;
	}
	else {
		while (S.top>S.base) {
			cout << *--S.top << " ";
		}
		cout << endl;
	}
}
int main()
{
	int n,e;
	SqStack stack;
	stack.base = stack.top = NULL;
	stack.stacksize = 0;
	cout << "Please enter what you want to do.The zero is exit." << endl
		 << "1. Initial the stack." << endl
		 << "2. Get top elem." << endl
		 << "3. Push elem to the stack." << endl
		 << "4. Pop elem from the stack." << endl
		 << "5. Destroy the stack." << endl
		 << "6. Clear the stack." << endl
		 << "7. Is the stack empty?" << endl
		 << "8. Length of the stack." << endl
		 << "9. Print the elem of the stack." << endl;
	while (cin>>n&&n) {
		switch (n) {
			case 1:
				if (InitStack(stack))
				cout<<"Initialize the success."<<endl;
				else
				cout<<"Initialize the failed."<<endl;
				break;
			case 2:
				if (GetTop(stack,e)==1)
				cout<<"Success!The result is "<<e<<"."<<endl;
				else
				cout<<"Failed.The stack is empty."<<endl;
				break;
			case 3:
				cout<<"Please enter the number you want to push."<<endl;
				cin>>e;
				if (Push(stack,e))
				cout<<"Push successfully!"<<endl;
				else
				cout<<"Push failed."<<endl;
				break;
			case 4:
				if (Pop(stack,e))
				cout<<"Pop successfully.The result is "<<e<<"."<<endl;
				else
				cout<<"The stack is empty."<<endl;
				break;
			case 5:
				if (!stack.base)
					cout << "The stack isn't initialized." << endl;
				else {
					DestroyStack(stack);
				}
				break;
			case 6:
				if (!stack.base)
					cout<<"The stack isn't initialized."<<endl;
				else {
					ClearStack(stack);
				}
				break;
			case 7:
				if (!stack.base)
					cout<<"The stack isn't initialized."<<endl;
				else {
					StackEmpty(stack);
				}
				break;
			case 8:
				if (!stack.base)
					cout<<"The stack isn't initialized."<<endl;
				else {
					StackLength(stack);
				}
				break;
			case 9:
				if (!stack.base)
					cout<<"The stack isn't initialized."<<endl;
				else {
					StackTraverse(stack);
				}
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/xyqxyq/p/10211374.html