Stack order (implemented in c/c + +)

The stack is changed by linear performance. Linear table is very free. You can insert data wherever you want, and delete data wherever you want. But there are some restrictions on the linear table, so it is not so free. Sealing the three sides of the linear table turns into a stack. The stack can only be "first in first out". In reality, there are still a lot of them, such as the conversion of base numbers, bracket matching when we write programming.... Or three steps.

Three steps of stack (sequence) implementation:

  • Define required functions (SqStack.h)
  • Implementation function (SqStack.cpp)
  • Call function (Stack.cpp)

Follow the three steps above to write the code in the form of three files with detailed comments.

SqStack.h file

/*
 *Name: white guest C
 *SqStack.h
 *Time: September 30, 2019
 *Environment: Visual Studio 2019
 */

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1 / / not possible
#define OVERFLOW -2 / / overflow

#Define stack? Init? Size 80 / / initial capacity 
#Define stackingincrement 10 / / allocation increment
typedef int Status;
typedef int ElemType;
typedef struct
{
	ElemType* base;
	ElemType* top;
	int stacksize;
}SqStack;


//Construct an empty stack
Status InitStack(SqStack& S);

//Destroy stack
Status DestroyStack(SqStack& S);

//Emptying stack
Status ClearStack(SqStack& S);

//Judge whether the stack is empty
Status StackEmpty(SqStack S);

//Determine the length of the stack
Status StackLength(SqStack S);

//Top of stack element
Status GetTop(SqStack S, ElemType& e);

//Push
Status Push(SqStack& S, ElemType e);

//Stack out 
Status Pop(SqStack& S, ElemType& e);

//Access function
Status Visit(ElemType e);

//Traversal from bottom to head
Status StackTraverse(SqStack S/*, Status Visit(ElemType e)*/);

SqStack.cpp file

/*
 *Name: white guest C
 *SqStack.cpp
 *Time: September 30, 2019
 *Environment: Visual Studio 2019
 */


#include "SqStack.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

Status InitStack(SqStack& S)
{
	S.base = (ElemType*)malloc(sizeof(ElemType) * STACK_INIT_SIZE);
	//S.base = new ElemType[STACK_INIT_SIZE];
	
	if (!S.base)
	{
		//Storage allocation failed
		exit(OVERFLOW);
	}
	//Empty stack
	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
	return OK;
}

Status DestroyStack(SqStack& S)
{
	free(S.base);
	S.base = NULL;
	S.top = NULL;
	S.stacksize = 0;
	return OK;
}

Status ClearStack(SqStack& S)
{
	if (S.top == S.base)
	{
		return ERROR;
	}
	S.top = S.base;
	return OK;
}

Status StackEmpty(SqStack S)
{
	if (S.top == S.base)
	{
		return OK;
	}
	else
	{
		return ERROR;
	}
}

Status StackLength(SqStack S)
{
	return S.top - S.base;
}

Status GetTop(SqStack S, ElemType& e)
{
	if (S.top == S.base)
	{
		return ERROR;
	}
	e = *(S.top - 1);
	return OK;
}

Status Push(SqStack& S, ElemType e)
{
	if (S.top == S.base + S.stacksize) //Stack full, additional storage space 
	{
		S.base = (ElemType*)realloc(S.base, sizeof(ElemType) * S.stacksize + STACKINCREMENT);
		if (!S.base)
		{
			exit(OVERFLOW);
		}
		S.top = S.base + S.stacksize;//Additional failure 
		S.stacksize += STACKINCREMENT;//Update stack capacity 
	}
	*S.top++ = e; //e first enter the stack, and then the top pointer moves down 
	return OK;
}

Status Pop(SqStack& S, ElemType& e)
{
	if (S.top == S.base)
	{
		exit(INFEASIBLE);
	}
	e = *--S.top;
	return e;
}

Status Visit(ElemType e)
{
	cout << e<<" ";
	return OK;
}

Status StackTraverse(SqStack S/*, Status Visit(ElemType e)*/)
{
	while (S.top>S.base)
	{
		//Visit(*(--S.top));
		cout << *(--S.top) << " ";
	}
	return OK;
}

Stack.cpp file

/*
 *Name: white guest C
 *Stack.cpp
 *Time: September 30, 2019
 *Environment: Visual Studio 2019
 */



#include <iostream>
#include <stdlib.h>
#include "SqStack.h"
using namespace std;

int main()
{
	SqStack S;
	int i;
	cout << "0.Sign out" << endl;
	cout << "1.Create an empty stack" << endl;
	cout << "2.Destroy stack" << endl;
	cout << "3.Emptying stack" << endl;
	cout << "4.Judge whether the stack is empty" << endl;
	cout << "5.View the length of the stack" << endl;
	cout << "6.Top of stack element" << endl;
	cout << "7.Push" << endl;
	cout << "8.Stack out " << endl;
	cout << "9.ergodic" << endl;
	do
	{
		cout << endl << "Please select an action:";
		cin >> i;
		switch (i)
		{
			case 1:
				int Init_i;
				Init_i=InitStack(S);
				if (Init_i)
				{
					cout << "Create an empty stack successfully" << endl;
				}
				else
				{
					cout << "Failed to create an empty stack" << endl;
				}
				break;

			case 2:
				DestroyStack(S);
				cout << "Destroy the stack successfully" << endl;
				break;

			case 3:
				if (ClearStack(S))
				{
					cout << "The stack is emptied." << endl;
				}
				else
				{
					cout << "The stack is already empty. It does not need to be emptied again" << endl;
				}
				
				break;

			case 4:
				if (StackEmpty(S))
				{
					cout << "Empty stacks" << endl;
				}
				else
				{
					cout << "Not empty stacks" << endl;
				}
				break;

			case 5:
				cout << "The length of the stack is" << StackLength(S) << endl;
				break;

			case 6:
				ElemType Get_e;
				GetTop(S, Get_e);
				cout << "The top elements are:" << Get_e;
				break;

			case 7:
				ElemType Push_e;
				cout << "Please enter the stack element:";
				cin >> Push_e;
				Push(S,Push_e);
				cout << Push_e << "Already in the stack" << endl;
				
				break;

			case 8:
				ElemType Pop_e;
				cout << Pop(S, Pop_e) << "Out of stack" << endl;
				break;

			case 9:
				cout << "Stack elements:";
				StackTraverse(S);
				cout << endl;
				break;

			default:
				break;
		}
	} while (i!=0);
}

Keywords: Big Data Programming

Added by Dave96 on Tue, 22 Oct 2019 00:08:14 +0300