用链表手动实现C++栈操作(5种)

这篇博客介绍了如何使用C++手动实现链表栈的五种操作:push、pop、top、empty和size。同时,文章中提到了模板template、结构体struct以及链表和指针pointer的基本知识。
摘要由CSDN通过智能技术生成

栈操作

  • push()
  • pop()
  • top()
  • empty()
  • size()

顺便温习下 模版template, 结构体struct, 链表linked list, 指针pointer.(这是来搞翻译的么…)

贴码:

  • 还非得整两个文件,两个文件在一个文件夹中即可,可以g++手动编译下 g++ -o stack main.cpp
  • 合并到一个文件中也行。

文件stack.h

// stack.h
#ifndef STACK_H
#define STACK_H

#include <ostream>
using namespace std;

template <class T>
class Stack{
public:
    Stack();
    Stack(int max);
    ~Stack();
    void Push(T const &x);
    void Pop();
    const T Top() const;
    size_t Size() const { return count; }
    bool IsEmpty() const { return count==0; }

private:
    struct Node {
        T data;
        Node *next;
    };

    typedef Node node; //类型别名
    node *top;
    size_t count;
    int max;
};

template <class T>
Stack<T>::Stack():top(nullptr), count(0), max(-1){
    cout << "init, come on!" << endl;
    cout << "at the first, count = " << count << endl;
}

template <class T>
Stack<T>::Stack(int max):top(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值