数据结构——栈的实现顺序栈,链式栈(C++实现)

本文介绍了顺序栈和链式栈的C++实现,包括push、pop等基本操作,并展示了运行结果。顺序栈在满栈情况下会提示“栈已满”,最终弹出元素为9876543210;链式栈则展示了按出栈顺序打印的元素,依次为4赵六、3王五、2李四、1张三,并进行了元素的弹出操作。
摘要由CSDN通过智能技术生成

顺序栈实现代码:

/*************************************************************************
	> File Name: seqStack.cpp
	> Author: 念念
	> Mail: 2845906049@qq.com 
	> Created Time: 2021年08月17日 星期二 9时54分23秒
    > Function: 栈的实现(顺序栈)
 ************************************************************************/
 
#include <iostream>
using namespace std;

#define MAXSIZE 10

class SeqStack
{
public:
    SeqStack(){top = -1;}
	~SeqStack(){}
	void push(int value);
	int pop();
	bool isEmpty(){return top==-1?true:false;}
private:
    int stack[MAXSIZE];
    int top;
};

void SeqStack::push(int value)
{
	if(top == 9)
	{
		cout << "栈已满!\n";
		return;
	}
	stack[++top] = value;
}

int SeqStack::pop()
{
	if(isEmpty())
	{
		cout << "栈为空!\n";
		return 0;
	}
	return stack[top--];
}

int main()
{
	SeqStack ss;
	for(int i = 0;i < 12; i++)
	{
		ss.push(i);
	}
	for(int i = 0; i < 12 && !ss.isEmpty(); i++)
	{
		cout << ss.pop() << " ";
	}
	cout << endl;

    return 0;
}

运行结果:

栈已满!
栈已满!
9 8 7 6 5 4 3 2 1 0 

链式栈实现代码:

/*************************************************************************
	> File Name: linkStack.cpp
	> Author: 念念
	> Mail: 2845906049@qq.com 
	> Created Time: 2021年08月17日 星期二 10时34分39秒
    > Function: 栈的实现(链式栈)
 ************************************************************************/
#include <iostream>
#include <string>
using namespace std;

struct Node
{
    int num;
    string name;
    Node * next;
};

class LinkStack
{
public:
    LinkStack();
    ~LinkStack();
    Node *createNode(int num,string name);
    void push(Node *node);
    Node* pop();
    bool isEmpty();
    void print();
private:
    Node * top;//栈顶指针
};

LinkStack::LinkStack()
{
    top = new(Node);
    top->num = 0;
    top->name = "空栈";
    top->next = NULL;
}
LinkStack::~LinkStack()
{
    delete top;
}
Node * LinkStack::createNode(int num,string name)
{
    Node * node = new(Node);
    node->num = num;
    node->name = name;
    return node;
}
void LinkStack::push(Node * node)
{
    node->next = top->next;
    top->next = node;   
}
Node* LinkStack::pop()
{
    if(isEmpty())
    {
        return top; //此处不返回空,防止因外界使用返回值的成员变量造成段错误
    }
    Node *p = top->next;
    top->next = top->next->next;
    p->next = NULL; //将其置为空,防止在类外获取后续指针
    return p;
}
bool LinkStack::isEmpty()
{
    return NULL == top->next ? true : false;
}

void LinkStack::print()
{
     if(isEmpty())
    {
        cout << "空栈!\n";
        return ;
    }
    cout << "打印(按出栈顺序):\n";
    Node * temp = top->next;
    while(NULL != temp)
    {
        cout << "num:" << temp->num << "\tname:" << temp->name << endl;
        temp = temp->next;
    }
}

int main()
{
    LinkStack ls;
    Node *temp;

    ls.push(ls.createNode(1,"张三")); 
    ls.push(ls.createNode(2,"李四")); 
    ls.push(ls.createNode(3,"王五"));
    ls.push(ls.createNode(4,"赵六"));

    ls.print();
    
    for(int i = 0; i < 6 && !ls.isEmpty(); i++)
    {
        temp = ls.pop();
        cout << "弹出:" << temp->num << temp->name << endl;
    }
    return 0;
}

打印结果:

打印(按出栈顺序):
num:4	name:赵六
num:3	name:王五
num:2	name:李四
num:1	name:张三
弹出:4赵六
弹出:3王五
弹出:2李四
弹出:1张三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

念念⁡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值