数据结构学习笔记————C++实现顺序栈与链栈的模板类

本文深入探讨了栈数据结构的两种实现方式:顺序栈和链栈。通过代码示例详细讲解了栈的基本操作,如入栈、出栈、取栈顶元素及打印栈内元素等。并提供了测试代码验证栈的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

顺序栈SeqStack.h

#pragma once
//实现一个功能没有必要实现面面俱到的想好每一步再下手
//可以先有个大概思路,直接上手,遇到问题,见招拆招 ,最后自然会水到渠成的完成原先的设定。
#include <iostream>
#define DEFAULT_SIZE = 50;
using namespace std;
template <typename DataType>
class SeqStack
{
private:
	int max_length;
	int top;
	DataType *p;//指向栈的指针
public:
	SeqStack(int max_length_ = DEFAULT_SIZE);//初始化一个空栈 定义好最大存储空间
	SeqStack(const DataType data_[], int n, int max_length_ = DEFAULT_SIZE);//初始化一个含数据的栈把数据传进去,定义好最大存储空间和当前空间
	void clear();//清空
	~SeqStack();
	void push(DataType data_);//入栈
	DataType pop();//出栈
	DataType get_top();//取栈顶元素
	void Print_All();//打印栈中所有元素 (从栈顶到栈底的顺序)


};

template<typename DataType>
inline SeqStack<DataType>::SeqStack(int max_length_)//注意只能在函数的声明中写默认参数,在定义时不能加默认参数
{
	if (p != NULL)
	{
		delete[]p;
	}
	p = new DataType[max_length_];
	top = -1;
	max_length = Max_length_;


}

template<typename DataType>
inline SeqStack<DataType>::SeqStack(const DataType data_[], int n, int max_length_)
{
	top = n - 1;
	max_length = max_length_;
	if (p != NULL)
	{
		delete[]p;
	}
	p = new DataType[max_length];
	for (int i = 0; i < n; i++)
	{
		p[i] = data_[i];
		cout << p[i] << endl;
	}
}

template<typename DataType>
inline void SeqStack<DataType>::clear()
{
	while (top != -1)
	{
		pop();

	}
}

template<typename DataType>
inline SeqStack<DataType>::~SeqStack()
{
	delete[]p;
}

template<typename DataType>
inline void SeqStack<DataType>::push(DataType data_)
{
	if (top > max_length - 1)
	{
		std::cout << "顺序栈元素已满,该元素无法入栈" << endl;
	}
	else
	{
		p[top + 1] = data_;
		top = top + 1;
	}


}

template<typename DataType>
inline DataType SeqStack<DataType>::pop()
{

	if (top < 0)
	{
		std::cout << "顺序栈元素已空,没有元素可以出栈" << endl;
		return 0;
	}
	else
	{
		top = top - 1;
		return p[top + 1];

	}
}

template<typename DataType>
inline DataType SeqStack<DataType>::get_top()
{
	if (top < 0)
	{
		std::cout << "栈已空,无法取得栈顶元素" << endl;
		return 0;
	}
	else
	{
		return p[top];
	}

}

template<typename DataType>
inline void SeqStack<DataType>::Print_All()
{
	std::cout << "当前顺序栈中所有元素有:" << endl;
		for (int i = top; i > -1; i--)
		{
			cout << p[i] << endl;

		}
}

链栈的结点类Node.h

#pragma once
#include <iostream>
template <typename DataType>
class Node 
{
public:
	DataType data;
	Node<DataType> *next;
	Node();
	Node(DataType data_, Node<DataType> *next_);

	

};

template<typename DataType>
inline Node<DataType>::Node()
{
	next = NULL;
}

template<typename DataType>
inline Node<DataType>::Node(DataType data_, Node<DataType>* next_)
{
	data = data_;
	next = next_;
}

链栈LinkStack.h

#include "Node.h"
template <typename DataType>
class LinkStack
{
public:
	Node<DataType> *top;
	LinkStack();
	LinkStack(const DataType data_[], int length);//链表的表头作为栈的栈顶
	void push(DataType data_);
	void pop();
	DataType get_top();
	void clear();
	~LinkStack();
	int get_length();
	void Print_All();


	
};

template<typename DataType>
inline LinkStack<DataType>::LinkStack()
{
	top = NULL;
}

template<typename DataType>
inline LinkStack<DataType>::LinkStack(const DataType data_[],int length)
{
	for (int i = 0; i < length; i++)
	{
		Node<DataType> *p = new Node<DataType>(data_[i], top);
		if (p == NULL)
		{
			cout << "动态内存已耗尽" << endl;
		}
		else
		{
			top = p;
		}

	}
	
	
}

template<typename DataType>
inline void LinkStack<DataType>::push(DataType data_)
{
	Node<DataType> *p = new Node<DataType>(data_,top);
	if (p == NULL)
	{
		cout << "动态内存已耗尽" << endl;
	}
	else
	{
		top = p;
	}


}

template<typename DataType>
inline void LinkStack<DataType>::pop()
{
	if (top == NULL)
	{
		cout << "栈已空" << endl;
	}
	else
	{
		Node<DataType> *p = new Node<DataType>();
		p = top;
		top = top->next;//注意再删除栈顶时 不能只有top = top->next头指针的移动,还得把旧的结点delete
		delete p;


	}
	
}

template<typename DataType>
inline DataType LinkStack<DataType>::get_top()
{
	if (top == NULL)
	{
		cout << "该栈为空栈,无法返回元素" << endl;
		return 0;
	}
	else
	{
		return top->data;
	}
	
}

template<typename DataType>
inline void LinkStack<DataType>::clear()
{
	while (top != NULL)
	{
		pop();
	}
}

template<typename DataType>
inline LinkStack<DataType>::~LinkStack()
{
	clear();
	delete top;
}

template<typename DataType>
inline int LinkStack<DataType>::get_length()
{
	Node<DataType> *p = new Node<DataType>();
	int cout = 0;
	for (p = top; p != NULL; p = p->next)
	{
		cout += 1;
	}
	cout << cout << endl;
	
}

template<typename DataType>
inline void LinkStack<DataType>::Print_All()
{
	Node<DataType> *p = new Node<DataType>();
	cout << "当前链栈的数据有" << endl;
	for (p = top; p != NULL; p = p->next)
	{
		cout << p->data << endl;
	}
	
	
}

 

测试源文件main.cpp

#include "SeqStack.h"
#include "LinkStack.h"
#include "stdlib.h"
int main()
{

	/*测试顺序栈*/
	int data1[] = { 10,8,4,21,1 };
	SeqStack<int> stack1(data1, 5, 50);
	stack1.Print_All();
	std::cout <<"栈顶元素为"<< stack1.get_top() << std::endl;
	stack1.push(32);
	stack1.Print_All();
	stack1.pop();
	stack1.Print_All();
	stack1.clear();
	/*测试链栈*/
	int data2[] = {45,12,6,4,89};
	LinkStack<int> stack2(data2,5);
	stack2.Print_All();
	std::cout << "栈顶元素为" << stack2.get_top() << std::endl;
	stack2.push(32);
	stack2.Print_All();
	stack2.pop();
	stack2.Print_All();
	stack2.clear();

	system("pause");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值