堆栈的链式存储结构(c++实现)

//堆栈的链式存储结构
#pragma once
#include<iostream>
using namespace std;
class Node
{
public:
	Node();
	~Node();
	friend class Stack;
private:
	int data;
	Node *next;
};
class Stack
{
public:
	Stack();
	~Stack();
	void Pop();
	void Push();
	bool IsEmpty();
	void Show_Stack();
private:
	Node *head;


};
//堆栈的链式存储结构
#include"堆栈的链式存储.h"
#include<iostream>
using namespace std;
Node::Node()
{

}
Node::~Node()
{

}
Stack::Stack()
{
	this->head = new Node;
	this->head->next = NULL;

}
Stack::~Stack()
{

}
void Stack::Pop()
{	
	int x = 0;
	if (this->IsEmpty())
	{
		cout << "堆栈为空!" << endl;

		
	}
	else
	{
		Node *ptemp = this->head->next;
		this->head->next = ptemp->next;
		x = ptemp->data;
		delete ptemp;
		cout << "出栈数据为" << x << endl;
	
	}

}
void Stack::Push()
{
	Node *ptemp = new Node;
	int x = 0;
	cout << "请输入你想要压栈的数据:" << endl;
	cin >> x;
	ptemp->data = x;
	ptemp->next = this->head->next;
	this->head ->next= ptemp;

	cout << "数据压栈成功" << endl;



}
bool Stack::IsEmpty()
{
	return this->head->next == NULL ? true : false;
}
void Stack::Show_Stack()
{	
	Node*p = this->head->next;
	while (p!= NULL)

	{
		cout << p->data << " ";
		p = p->next;

	}
	cout<< endl;

}
#include"堆栈的链式存储.h"
#include <iostream>
using namespace std;
int main()
{
	Stack s;
	int i = 9;
	while (i!=0)
	{
		cout << "1.压栈  2.出栈  3.遍历  4.判断是否栈空    0.退出" << endl;
		cin >> i;
		switch (i)
		{
		
		case 0:
			break;
		case 1:
			s.Push();
			break;
		case 2:
			s.Pop();
			break;
		case 3:
			s.Show_Stack();
			break;
		case 4:
			s.IsEmpty();
			break;
		}
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
堆栈是一种先进后出(Last-In-First-Out,LIFO)的数据结构,链存储堆栈的一种实现。链存储堆栈不需要预先指定堆栈的长度,可以动态地分配内存,具有灵活性和可扩展性。 链存储堆栈可以使用单向链表或双向链表来实现。以单向链表为例,堆栈的顶端称为栈顶,堆栈的底部称为栈底。 堆栈的基本操作包括入栈(Push)、出栈(Pop)、获取栈顶元素(Top)和判断堆栈是否为空(IsEmpty)。 下面是堆栈存储实现代码: ```C++ #include <iostream> using namespace std; // 定义堆栈的结点结构体 struct StackNode { int data; // 数据域 StackNode *next; // 指针域,指向下一个结点 }; // 定义堆栈类 class Stack { private: StackNode *top; // 栈顶指针 public: Stack() { top = NULL; } // 构造函数,初始化栈顶指针为空 ~Stack(); // 析构函数,释放内存 bool IsEmpty(); // 判断堆栈是否为空 void Push(int x); // 入栈 int Pop(); // 出栈 int Top(); // 获取栈顶元素 }; // 判断堆栈是否为空 bool Stack::IsEmpty() { return top == NULL; } // 入栈 void Stack::Push(int x) { StackNode *newNode = new StackNode; // 创建新结点 newNode->data = x; // 设置数据域 newNode->next = top; // 将新结点插入到栈顶 top = newNode; // 更新栈顶指针 } // 出栈 int Stack::Pop() { if (IsEmpty()) { // 判断是否为空栈 cout << "Error: Stack is empty!" << endl; return -1; } else { StackNode *temp = top; // 保存栈顶结点 int x = temp->data; // 获取栈顶元素 top = top->next; // 更新栈顶指针 delete temp; // 释放内存 return x; } } // 获取栈顶元素 int Stack::Top() { if (IsEmpty()) { // 判断是否为空栈 cout << "Error: Stack is empty!" << endl; return -1; } else { return top->data; // 返回栈顶元素 } } // 析构函数,释放内存 Stack::~Stack() { while (!IsEmpty()) { // 循环弹出栈中所有元素 Pop(); } } // 测试堆栈存储实现 int main() { Stack s; // 创建堆栈对象 // 入栈操作 s.Push(1); s.Push(2); s.Push(3); // 获取栈顶元素 cout << "Top element: " << s.Top() << endl; // 出栈操作 cout << "Pop element: " << s.Pop() << endl; cout << "Pop element: " << s.Pop() << endl; // 获取栈顶元素 cout << "Top element: " << s.Top() << endl; // 出栈操作 cout << "Pop element: " << s.Pop() << endl; // 判断堆栈是否为空 if (s.IsEmpty()) { cout << "Stack is empty!" << endl; } else { cout << "Stack is not empty!" << endl; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值