栈的探究——顺序栈和链栈

基本介绍

栈是最基础的数据结构,他有两种存储结构———顺序结构和链式结构,总的说有判空,判满,取栈顶元素,进栈和出栈。其中链栈由于其特殊结构,是不需要判满的,因为它得存储空间不是连续的。介绍栈的有许许多多的博客,我这里就简单说一会,重点是上代码。
栈就是先进后出只能在一端进行插入和删除元素,然后知道上面的基本操作基本就没有什么问题了,应用得自己多练习才可。多话不说,直接上代码。

顺序栈

初始化栈

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;
}

总代码及实现代码

#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;
}

运行结果

在这里插入图片描述

链栈

初始化栈

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

判空

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;
}

析构

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

总代码

#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;
}

运行结果

在这里插入图片描述

ps
我在另一篇文章写了一个栈的应用:制作计算器,有兴趣的同学可以来参考一下。链接我放在下面:
链接: 栈的应用——制作计算器.

如果对您有所帮助的话1,不妨点个赞支持一下呗!

猜你喜欢

转载自blog.csdn.net/Freedom_cao/article/details/108286626