Linear table - 04 stack

Posted by rReLmy on Tue, 01 Mar 2022 12:12:51 +0100

Write before:
Today, let's learn about the stack structure. The stack can also be called directly in C + + STL, but we can use C + + to realize the stack structure.

Definition of stack

The stack meets the principle of "first in and last out", that is, it can only be inserted and deleted from the tail. The implementation of the stack can be realized by array and linked list. We usually use array for simulation. The following explanations are based on the implementation of array.
To realize the structure of the stack, you need to store data and a pointer to the top of the stack. If you use an array, the starting position of the pointer is at the subscript 0 or - 1. I'm usually used to initializing the pointer at the subscript - 1.

struct Stack {
	int Sacklist[MAXVERTIES] = { 0 };	//Initialize elements in stack
	int top = -1;	//Initialize pointer position
};

Stack insertion

As we continue to put data into this stack, the pointer subscript will continue to increase.

void Push(Stack& S, int key) {
	if (S.top == MAXVERTIES) {
		cout << "Stack full!" << endl;
		return;
	}
	++S.top;
	S.Sacklist[S.top] = key;
}

Stack deletion

When you want to delete or remove the elements in the stack, you can only start from the top element, and the subscript of the pointer will decrease in the process.

However, it should be noted here that deleting or taking out the elements in the array will not disappear, but the pointer changes, and the previous content will be overwritten the next time the data is inserted.

//Delete stack top element
void Pop(Stack& S) {
	if (S.top == -1) {
		cout << "Stack is empty!" << endl;
		return;
	}
	int temp = S.Sacklist[S.top];
	--S.top;
}

Return stack top element

//Return stack top element
int top(Stack& S) {
	return S.Sacklist[S.top];
}

Find elements in stack

//Judge whether the element in the stack exists (if it exists, return the corresponding subscript; if it does not exist, return 0)
int find(Stack& S, int key) {
	if (S.top == -1) {
		cout << "Stack is empty!" << endl;
		return -1;
	}
	//Define a temporary pointer to the top of the stack
	for (int temp = S.top; temp != -1; --temp) {
		if (S.Sacklist[temp] == key)
			return temp;
	}
	cout << "No such element found!" << endl;
	return 0;
}

Delete the specified element

It should be noted that when we want to delete any element in the stack, we need to first take all the elements above the deleted element out of the stack, store the out of stack elements in a temporary stack, then delete the element, and then put the element just out of the stack into the stack.





//Delete the specified element
void delete_index(Stack& S, int key) {
	Stack temp;	//A temporary stack is needed to receive the elements poured out of the original stack
	int index = find(S, key);	//Find this element
	if (index <= 0)
		return;
	while (S.top != index) {
		Push(temp, S.Sacklist[S.top]);
		Pop(S);
	}
	Pop(S);	//Delete the specified element
	while (temp.top != -1) {
		S.Sacklist[++S.top] = temp.Sacklist[temp.top];	//Rewind the elements in the temporary stack
		--temp.top;
	}
}

Traversal stack

//Traverse the entire stack
void show(Stack& S) {
	if (S.top == -1)
		cout << "Stack is empty!" << endl;
	int temp = S.top;	//Define a temporary pointer
	while (temp != -1) {
		cout << S.Sacklist[temp] << " ";
		--temp;
	}
	cout << endl;
}

All codes

#include <bits/stdc++.h>
using namespace std;
#define MAXVERTIES 20

//Stack structure
struct Stack {
	int Sacklist[MAXVERTIES] = { 0 };	//Initialize elements in stack
	int top = -1;	//Initialize pointer position
};

//Push elements into the stack
void Push(Stack& S, int key) {
	if (S.top == MAXVERTIES) {
		cout << "Stack full!" << endl;
		return;
	}
	++S.top;
	S.Sacklist[S.top] = key;
}

//Delete stack top element
void Pop(Stack& S) {
	if (S.top == -1) {
		cout << "Stack is empty!" << endl;
		return;
	}
	int temp = S.Sacklist[S.top];
	--S.top;
}

//Return stack top element
int top(Stack& S) {
	return S.Sacklist[S.top];
}

//Judge whether the element in the stack exists (if it exists, return the corresponding subscript; if it does not exist, return 0)
int find(Stack& S, int key) {
	if (S.top == -1) {
		cout << "Stack is empty!" << endl;
		return -1;
	}
	//Define a temporary pointer to the top of the stack
	for (int temp = S.top; temp != -1; --temp) {
		if (S.Sacklist[temp] == key)
			return temp;
	}
	cout << "No such element found!" << endl;
	return 0;
}

//Delete specified element
void delete_index(Stack& S, int key) {
	Stack temp;	//A temporary stack is needed to receive the elements poured out of the original stack
	int index = find(S, key);	//Find this element
	if (index <= 0)
		return;
	while (S.top != index) {
		Push(temp, S.Sacklist[S.top]);
		Pop(S);
	}
	Pop(S);	//Delete the specified element
	while (temp.top != -1) {
		S.Sacklist[++S.top] = temp.Sacklist[temp.top];	//Rewind the elements in the temporary stack
		--temp.top;
	}
}

//Traverse the entire stack
void show(Stack& S) {
	if (S.top == -1)
		cout << "Stack is empty!" << endl;
	int temp = S.top;	//Define a temporary pointer
	while (temp != -1) {
		cout << S.Sacklist[temp] << " ";
		--temp;
	}
	cout << endl;
}

int main() {
	Stack S;
	Push(S, 1);
	Push(S, 8);
	Push(S, 5);
	Push(S, 4);
	Push(S, 2);
	show(S);
	Pop(S);
	show(S);
	delete_index(S, 2);
	delete_index(S, 5);
	show(S);
}


If you have any questions, please discuss them in the comment area below~

[previous lecture] linear list - 03 bidirectional circular linked list

Topics: C++ data structure stack array