C++栈的顺序存储和链式存储的实现

栈是最常见的数据结构,其特点是后进先出(Last In First Out)也是链表的特殊形式,所以和链表一样,有两种存储方式,第一是顺序存储的栈,方便快速读写数据,但是栈的长度必须先固定;第二种是链式存储的栈,可以不用定义栈的长度,可以大量插入数据,如果不是物理内存使用完的话,可以存储大量的数据。

首先,顺序存储的栈的实现,代码如下:

#pragma once
#define MAXSIZE 10
template<typename EleType>
class OrderStack
{

public:
	OrderStack();
	~OrderStack();
	bool GetTop(EleType& e);
	bool Push(const EleType& e);
	bool Pop(EleType& e);
	void Show()const;
private:
	EleType data[MAXSIZE];
	int top;
	bool Empty()const;
	bool Full()const;
	int StackLength()const;
};

#include "OrderStack.h"
#include <iostream>
using namespace std;
template<typename EleType>
bool OrderStack<EleType>::Pop(EleType& e)
{
	if (Empty())
	{
		cout << "The stack is empty!\n";
		return false;
	}
	e = data[top];
	--top;
	return true;
}

template<typename EleType>
bool OrderStack<EleType>::Push(const EleType& e)
{
	if (Full())
	{
		return false;
	}
	++top;
	data[top] = e;
	return true;
}

template<typename EleType>
bool OrderStack<EleType>::GetTop(EleType& e)
{
	if (Empty())
	{
		return false;
	}
	e = data[top];
	return true;
}

template<typename EleType>
OrderStack<EleType>::~OrderStack()
{
	 
}

template<typename EleType>
OrderStack<EleType>::OrderStack() :top(-1)
{

}

template<typename EleType>
int OrderStack<EleType>::StackLength() const
{
	return top + 1;
}

template<typename EleType>
bool OrderStack<EleType>::Full() const
{
	return (top == MAXSIZE - 1);
}

template<typename EleType>
bool OrderStack<EleType>::Empty() const
{
	return (top == -1);
}


template<typename EleType>
void OrderStack<EleType>::Show() const
{
	if (Empty())
	{
		cout << "The stack is Empty!\n";
		return;
	}
	cout << "The stack length is: " << StackLength() << endl;
	cout << "The stack is: ";
	for (int i = top; i >= 0;--i)
	{
		cout << data[i] << " ";
	}
	cout << endl;
}

第二,栈的链式存储,代码如下:


#pragma once
template<typename EleType>
class ChainStack
{
public:
	ChainStack();
	~ChainStack();
	bool GetTop(EleType& e);
	bool Push(const EleType& e);
	bool Pop(EleType& e);
	void Show()const;
private:
	bool Empty()const;
	//bool Full()const;
	int StackLength()const;
	struct Node
	{
		EleType data;
		Node* next;
		Node(){ next = nullptr; }
	};
	Node* top;
	int length;
};

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



template<typename EleType>
int ChainStack<EleType>::StackLength() const
{
	return length;
}

// template<typename EleType>
// bool ChainStack<EleType>::Full() const
// {
// 
// }

template<typename EleType>
bool ChainStack<EleType>::Empty() const
{
	return (length == 0);
}

template<typename EleType>
void ChainStack<EleType>::Show() const
{
	if (Empty())
	{
		cout << "The stack is empty!\n";
		return;
	}
	cout << "The stack length is " << length << endl;
	cout << "The stack is ";
	Node* temp=top;
	for (int i = 1; i <= length;++i)
	{
		cout << temp->data << " ";
		temp = temp->next;
	}
	cout << endl;
}

template<typename EleType>
bool ChainStack<EleType>::Pop(EleType& e)
{
	if (Empty())
	{
		cout << "The stack is empty!\n";
		return false;
	}
	e = top->data;
	Node* temp = top;
	top = top->next;
	--length;
	delete temp;
	return true;
}

template<typename EleType>
bool ChainStack<EleType>::Push(const EleType& e)
{
	Node* temp = new Node;
	temp->data = e;
	if (Empty())
	{
		top = temp;
	}
	else
	{
		temp->next = top;
		top = temp;
	}
	++length;
	return true;	 
}

template<typename EleType>
bool ChainStack<EleType>::GetTop(EleType& e)
{
	if (Empty())
	{
		cout << "The stack is empty!\n";
		return false;
	}
	e = top->data;
	return true;
}

template<typename EleType>
ChainStack<EleType>::~ChainStack()
{
	Node* temp = top;
	while (top)
	{
		temp = top;
		top = temp->next;
		delete temp;
	}

}

template<typename EleType>
ChainStack<EleType>::ChainStack() :length(0), top(nullptr)
{

}



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值