C++实现一个链栈

什么是链栈

链栈不名思意,就是既具有链表的特性,又具有栈的特性。
即:

  1. 链栈中的元素由指针域和数据域组成,通过指针指向下一个元素;2.链栈同时又具有栈的特性,先进后出。

如何实现链栈

根据上述链栈所具有的特征,这里需要创建一个链表结构体和一个栈结构体。然后创建一个链栈类,通过对链表和栈的特性的应用来实现链栈。(这里只是一个思路)

链栈的实现

开发环境

作者采用的是visual studio 2017,编译的是debug版本下的x86。如下图:
在这里插入图片描述
创建的是控制台输出程序,创建过程这里忽略。

代码实现

共包含三个文件:listStackDemo1.cpp,ListStack.h,ListStack.cpp。其中listStackDemo1.cpp就是主函数所在的文件,相当于以往的main.cpp,其中是链栈的使用。ListStack.h和ListStack.cpp类主要实现链栈。
下面是具体代码:
ListStack.h

#pragma once

typedef struct ListNode
{
	char data;
	struct ListNode *pNext;
}stuNode;

typedef struct Stack
{
	stuNode *pTop;
	int nSize;
}stuListStack;


class ListStack
{
public:
	ListStack();
	bool isEmpty();
	void push(char c);
	char pop();
	int getSizeCount();
	void print();
private:
	stuListStack m_listStack;
};

ListStack.cpp

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

ListStack::ListStack()
{
	m_listStack.pTop = nullptr;
	m_listStack.nSize = 0;
}

bool ListStack::isEmpty()
{
	if (m_listStack.pTop == nullptr)
	{
		return true;
	}
	return false;
}

void ListStack::push(char c)
{
	stuNode *pNode = new stuNode;
	pNode->data = c;
	pNode->pNext = m_listStack.pTop;
	m_listStack.pTop = pNode;
	m_listStack.nSize++;
}

char ListStack::pop()
{
	if (isEmpty())
	{
		return -1;
	}
	char c = m_listStack.pTop->data;
	stuNode *pDelete = m_listStack.pTop;
	m_listStack.pTop = m_listStack.pTop->pNext;
	m_listStack.nSize--;
	delete pDelete;

	return c;
}

int ListStack::getSizeCount()
{
	return m_listStack.nSize;
}

void ListStack::print()
{
	stuNode *p = m_listStack.pTop;
	while (p) 
	{
		cout << p->data << " ";
		p = p->pNext;
	}
	cout << endl;
}

listStackDemo1.cpp

#include <iostream>
#include "ListStack.h"

using namespace std;

int main()
{
	ListStack listStack;
	int nCount = listStack.getSizeCount();
	cout<<"链栈的元素个数:"<< nCount <<endl;
	listStack.push('A');
	listStack.push('B');
	listStack.push('C');
	listStack.push('D');
	listStack.push('E');
	listStack.print();
	cout << "请输入出栈次数:"<<endl;
	int m;
	cin >> m;
	for (int i = 0; i < m;++i) 
	{
		cout<<listStack.pop()<<" ";
	}
	cout << endl;
	nCount = listStack.getSizeCount();
	cout <<"出栈后链栈中的元素个数:"<< nCount<<endl;
	listStack.print();

	return 0;
}

运行结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是 C++ 实现链栈实现基本查询功能的示例代码: ```c++ #include <iostream> using namespace std; struct Node { int data; Node* next; }; class Stack { private: Node* top; // 链栈的栈顶指针 public: Stack() { top = NULL; } // 构造函数,将栈顶指针初始化为 NULL ~Stack(); // 析构函数,释放链栈的所有节点内存空间 bool isEmpty() const { return top == NULL; } // 判断链栈是否为空 void push(int item); // 入栈 int pop(); // 出栈 int peek() const; // 获取栈顶元素 }; Stack::~Stack() { Node* p; while (top != NULL) { p = top; top = top->next; delete p; } } void Stack::push(int item) { Node* newNode = new Node; newNode->data = item; newNode->next = top; top = newNode; } int Stack::pop() { if (isEmpty()) { cout << "栈空,不能出栈!" << endl; return 0; } Node* p = top; int item = p->data; top = top->next; delete p; return item; } int Stack::peek() const { if (isEmpty()) { cout << "栈空,没有栈顶元素!" << endl; return 0; } return top->data; } int main() { Stack s; s.push(1); s.push(2); s.push(3); cout << "栈顶元素:" << s.peek() << endl; int item = s.pop(); cout << "出栈元素:" << item << endl; cout << "栈顶元素:" << s.peek() << endl; item = s.pop(); cout << "出栈元素:" << item << endl; cout << "栈顶元素:" << s.peek() << endl; item = s.pop(); cout << "出栈元素:" << item << endl; cout << "栈是否为空:" << (s.isEmpty() ? "是" : "否") << endl; return 0; } ``` 代码中定义了一个 `Node` 结构体表示链栈中的节点,使用一个 `top` 指针来表示链栈的栈顶。根据链栈的特点,入栈操作只需要在链表头部插入一个新的节点,出栈操作只需要删除链表头部的节点即可。`peek` 操作用于获取栈顶元素,判断链栈是否为空只需要判断 `top` 是否为 `NULL` 即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肩上风骋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值