堆栈(Stack)实现(C++)

本文介绍了堆栈数据结构,包括其先进后出的特性,以及静态堆栈和动态堆栈的区别。通过C++代码展示了如何使用数组实现静态堆栈和链表实现动态堆栈,包括入栈、出栈、查看栈顶等基本操作。同时还提供了示例程序来演示这些操作的使用。
摘要由CSDN通过智能技术生成

一. 堆栈(Stack)

在这里插入图片描述
堆栈(Stack 是一种可以存取的数据结构,其主要特征是先进后出(Last-in-first-out, LIFO)

栈顶(Top 是最后入栈的数据

堆栈可分为静态堆栈(Static Stack)和动态堆栈(Dynamic Stack)两类:

  • Static Stack: 栈的大小固定,可以用数组实现;
  • Dynamic Stack: 栈的大小随需要改变,可以用链表实现

二. 基本操作(Basic Operation)

  1. 入栈(Push):Equivalent to an insert

在这里插入图片描述

  1. 出栈(Pop): Deletes the most recently inserted element

在这里插入图片描述

在这里插入图片描述

  1. 查看栈顶(Top): Examines the most recently inserted element Data Structures

在这里插入图片描述

三. C++代码

  1. 静态堆栈(Static Stack):
#include <iostream>

using namespace std;

class Stack 
{
	public:
		Stack(int size);// constructor
		~Stack();
		bool IsEmpty() { return top == -1; }
		bool IsFull() { return top == maxTop; }
		double Top();
		void Push(const double x);
		double Pop();
		void DisplayStack();
	private:
		int maxTop;// max stack size = size -1
		int top;// current top of stack
		double* values;// element array
};

Stack::Stack(int size) 
{
	maxTop = size -1;
	values = new double[size];
	top = -1;
}

Stack::~Stack() 
{ 
	delete[] values; 
}
		
void Stack::Push(const double x) 
{
	if (IsFull())
		cout<< "Error: the stack is full." << endl;
	else 
		values[++top] = x;
}

double Stack::Pop() 
{
	if (IsEmpty()) 
	{
		cout<< "Error: the stack is empty." << endl;
		return -1;
	}
	else 
	{
		return values[top--];
	}
}

double Stack::Top() 
{
	if (IsEmpty()) 
	{
		cout<< "Error: the stack is empty." << endl;
		return -1;
	}
	else
		return values[top];
}

void Stack::DisplayStack() 
{
	cout<< "top -->";
	for (int i= top; i>= 0; i--) 
		cout<< "\t|\t" << values[i] << "\t|" << endl;
	cout<< "\t|---------------|" << endl;
}

int main(void) 
{
	Stack stack(3);
	stack.Push(5.0);
	stack.Push(6.5);
	stack.Push(-3.0);
	stack.Push(-8.0);
	
	stack.DisplayStack();
	cout<< "Top: " << stack.Top() << endl;
	stack.Pop();
	cout<< "Top: " << stack.Top() << endl;
	
	while (!stack.IsEmpty()) 
		stack.Pop();
	stack.DisplayStack();
	
	return 0;
}
  1. 动态堆栈(Dynamic Stack):
#include <iostream>

using namespace std;

class Node {
	public:
		double data; // data
		Node* next; // pointer to next
	};
	
class List {
	public:
		List(void);// constructor	
		~List(void);// destructor
		bool IsEmpty();
		int InsertNode(int index, double x);
		int DeleteNode(double x);
		void DeleteAll();
		void DisplayList();
	private:
		Node* head;
		friend class Stack;
};

class Stack : public List 
{
	public:
		Stack() {}// constructor
		~Stack() {}// destructor
		double Top();
		void Push(double x);
		double Pop();
		void DisplayStack();
};


List::List(void) { 
	head = NULL; 
}
			
List::~List(void) {
	delete head;
}

bool List::IsEmpty() { 
	if (head->next == NULL) {                 
    	return true;
    }
    return false;
}

int List::InsertNode(int index, double x) {
	if(index < 0) 
		return -1;
	int currIndex = 1;
	Node* currNode = head;
	while(currNode && index > currIndex) {
		currNode = currNode->next;
		currIndex++;
	}
	if(index > 0 && currNode == NULL) 
		return -1;
	Node* newNode = new Node;
	newNode->data = x;
	if(index == 0) {
		newNode->next = head;
		head = newNode;
	}
	else {
		newNode->next = currNode->next;
		currNode->next = newNode;
	}
	return 0;
}

int List::DeleteNode(double x) {
	Node* prevNode = NULL;
	Node* currNode = head;
	int currIndex = 1;
	while(currNode && currNode->data != x) {
		prevNode = currNode;
		currNode = currNode->next;
		currIndex++;
	}
	if(currNode) {
		if (prevNode) {
			prevNode->next = currNode->next;
			delete currNode;
		}
		else{
			head = currNode->next;
			delete currNode;
		}
		return currIndex;
	}
	return 0;
}

void List::DeleteAll()
{
    Node* currNode = head->next;
    Node* newNode = new Node;
    while (currNode != NULL)                    
    {
        newNode = currNode;
        currNode = currNode->next;
        head->next = currNode;
        newNode->next = NULL;
        delete newNode;
    }
    head = NULL;                 
}

void List::DisplayList() {
	int num = 0;
	Node* currNode = head;
	while(currNode != NULL){
		cout << currNode->data << endl;
		currNode = currNode->next;
		num++;
	}
	cout<< "Number of nodes in the list: " << num << endl;
}

double Stack::Top() 
{ 
	if (head == NULL) 
	{
		cout<< "Error: the stack is empty." << endl;
		return -1;
	}
	else 
		return head->data;
}
	
void Stack::Push(double x) 
{
	InsertNode(0, x);
}
		 
double Stack::Pop() 
{ 
	if (head == NULL) 
	{
		cout<< "Error: the stack is empty." << endl;
		return -1;
	}
	else 
	{
		double val = head->data;
		DeleteNode(val);
		return val;
	}
}

void Stack::DisplayStack() 
{
	DisplayList(); 
}

int main(void) 
{
	Stack stack;
	stack.Push(5.0);
	stack.Push(6.5);
	stack.Push(-3.0);
	stack.Push(-8.0);
	
	stack.DisplayStack();
	cout<< "Top: " << stack.Top() << endl;
	stack.Pop();
	cout<< "Top: " << stack.Top() << endl;
	
	while (!stack.IsEmpty()) 
		stack.Pop();
	stack.DisplayStack();
	
	return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cascatrix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值