Exploring the stack-sequential stack and chain stack

basic introduction

The stack is the most basic data structure. It has two storage structures- sequential structure and chain structure . Generally speaking, there are empty and full, the top element of the stack is taken, and the stack is pushed and popped. The chain stack does not need to be full due to its special structure, because its storage space is not continuous. There are many blogs that introduce the stack, I will briefly talk about it here, focusing on the code.
The stack isFirst in last outOnly insert and delete elements at one end, And then know that there is basically no problem with the basic operations above, and you have to practice more by yourself. Don't talk too much, just go to the code.

Sequential stack

Initialization stack

template<typename T>
inline stack<T>::stack()
{
    
    
	count = 0;
}

Empty

template<typename T>
inline bool stack<T>::empty() const
{
    
    
	if (count == 0)	
		return true;
	return false;
}

Full sentence

template<typename T>
inline bool stack<T>::full() const
{
    
    
	if (count == maxlen) return true;
	return false;
}

Take the top element of the stack

template<typename T>
inline Error_code stack<T>::top(T &x)
{
    
    
	if (empty())	return underflow;
	x = data[count-1];
	return success;
}

Push

template<typename T>
inline Error_code stack<T>::push(T x)
{
    
    
	if (full())	return overflow;
	data[count] = x;
	count++;
	return success;
}

Unstack

template<typename T>
inline Error_code stack<T>::pop()
{
    
    
	if (empty())	return underflow;
	count--;
	return success;
}

Total code and implementation code

#pragma once
const int maxlen = 10000;
enum Error_code{
    
    success,underflow,overflow};
template <typename T>
class stack
{
    
    
public:
	stack();
	bool empty() const;
	bool full() const;
	Error_code top(T &x);
	Error_code push(T x);
	Error_code pop();
private:
	int count;
	T data[maxlen];
};

template<typename T>
inline stack<T>::stack()
{
    
    
	count = 0;
}

template<typename T>
inline bool stack<T>::empty() const
{
    
    
	if (count == 0)	
		return true;
	return false;
}

template<typename T>
inline bool stack<T>::full() const
{
    
    
	if (count == maxlen) return true;
	return false;
}

template<typename T>
inline Error_code stack<T>::top(T &x)
{
    
    
	if (empty())	return underflow;
	x = data[count-1];
	return success;
}

template<typename T>
inline Error_code stack<T>::push(T x)
{
    
    
	if (full())	return overflow;
	data[count] = x;
	count++;
	return success;
}

template<typename T>
inline Error_code stack<T>::pop()
{
    
    
	if (empty())	return underflow;
	count--;
	return success;
}
#include "stack.h"
#include <iostream>
using namespace std;

int main()
{
    
    
	stack<int> s;
	cout << "请输入一个数字: ";
	int n,m;
	cin >> n;
	cout << "进栈前的数:";
	for (int i = 1;i <= n;i++)
	{
    
    
		cout << i << " ";
		s.push(i);
	}
	cout << endl;
	cout << "出栈的数:";
	for (int i = 1;i <= n;i++)
	{
    
    
		s.top(m);
		cout << m << " ";
		s.pop();
	}
	return 0;
}

operation result

Insert picture description here

Chain stack

Initialization stack

template<typename T>
inline linked_stack<T>::linked_stack()
{
    
    
	top = NULL;
	count = 0;
}

Empty

template<typename T>
inline bool linked_stack<T>::empty() const
{
    
    
	return count==0;
}

Take the top element of the stack

template<typename T>
inline Error_code linked_stack<T>::get_top(T& x)
{
    
    
	if (empty())	return underflow;
	x = top->data;
	return success;
}

Push

template<typename T>
inline Error_code linked_stack<T>::push(T x)
{
    
    
	node* p = new node;
	p->data = x;
	p->next = top;
	top = p;
	count++;
	return success;
}

Unstack

template<typename T>
inline Error_code linked_stack<T>::pop()
{
    
    
	if (empty())	return underflow;
	node* p = top;
	top = top->next;
	delete p;
	count--;
	return success;
}

destruct

template<typename T>
inline linked_stack<T>::~linked_stack()
{
    
    
	while (!empty())
		pop();
}

Total code

#pragma once
#include <string>
enum Error_code {
    
    success,underflow};
template <typename T>
class linked_stack
{
    
    
public:
	linked_stack();
	~linked_stack();
	bool empty() const;
	Error_code get_top(T &x);
	Error_code push(T x);
	Error_code pop();
private:
	struct node
	{
    
    
		T data;
		node* next;
	};
	int count;
	node* top;
};

template<typename T>
inline linked_stack<T>::linked_stack()
{
    
    
	top = NULL;
	count = 0;
}

template<typename T>
inline linked_stack<T>::~linked_stack()
{
    
    
	while (!empty())
		pop();
}

template<typename T>
inline bool linked_stack<T>::empty() const
{
    
    
	return count==0;
}

template<typename T>
inline Error_code linked_stack<T>::get_top(T& x)
{
    
    
	if (empty())	return underflow;
	x = top->data;
	return success;
}

template<typename T>
inline Error_code linked_stack<T>::push(T x)
{
    
    
	node* p = new node;
	p->data = x;
	p->next = top;
	top = p;
	count++;
	return success;
}

template<typename T>
inline Error_code linked_stack<T>::pop()
{
    
    
	if (empty())	return underflow;
	node* p = top;
	top = top->next;
	delete p;
	count--;
	return success;
}

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

int main()
{
    
    
	linked_stack<int> s;
	cout << "请输入一个数字: ";
	int n, m;
	cin >> n;
	cout << "进栈前的数:";
	for (int i = 1;i <= n;i++)
	{
    
    
		cout << i << " ";
		s.push(i);
	}
	cout << endl;
	cout << "出栈的数:";
	for (int i = 1;i <= n;i++)
	{
    
    
		s.get_top(m);
		cout << m << " ";
		s.pop();
	}
	return 0;
}

operation result

Insert picture description here

ps
I wrote a stack application in another article: making a calculator, students who are interested can refer to it. I put the link below:
link: stack application-making calculator .

If it is helpful to you, please give a like and support it!

Guess you like

Origin blog.csdn.net/Freedom_cao/article/details/108286626