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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈操作

  • 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(nullptr), count(0) {
    this->max = max;
    cout << "max = " << max << endl;
}

template <class T>
Stack<T>::~Stack() {
    node *current = top;

    while (current != nullptr) {
        node *next = current->next;
        delete current;
        current = next;
    }

    top = nullptr;
}

template <class T>
void Stack<T>::Push(const T &x) { //is equal: T const &x
    if (count == max) {
        cout << "Stack is full. Can't push." << endl;
        return void();
    }

    node *newTop = new node;
    newTop->data = x;

    if (top == nullptr) {
        newTop->next = nullptr;
    }
    else {
        newTop->next =top;
    }

    top = newTop;
    count++;
    cout << "push data: " << top->data << endl;
}

template <class T>
void Stack<T>::Pop() {
    if (top == nullptr || count == 0) {
        cout << "There have no data. Pop fail." << endl;
        return void();
    }

    node *delNode = top;
    top = top->next;
    count--;
    cout << "pop data: " << delNode->data << endl;
    delete delNode;
}

template <class T>
const T Stack<T>::Top() const {
    if (top == nullptr || count == 0) {
        cout << "There have no data, can't top." << endl;
        return 0;
    }
    return top->data;
}

#endif /*STACK_H*/

文件main.cpp

// main.cpp
#include <iostream>
#include "stack.h"
using namespace std;

int main(void) {
    try {
        Stack<int> s1;
        s1.Push(1);
        s1.Push(2);
        cout << "size: " << s1.Size() << endl;
        s1.Pop();
        cout << "top: " << s1.Top() << endl;
        s1.Pop();
        s1.Pop();
        cout << s1.Top() << endl;
        cout << "size: " << s1.Size() << endl;

        cout << "-------------------------------" << endl;

        Stack<int> s2(3);
        if(s2.IsEmpty()) {
            cout << "Stack is empty." << endl;
        }
        s2.Push(1);
        s2.Push(2);
        s2.Push(3);
        s2.Push(4);
        cout << "Stack's size: " << s2.Size() << endl;

        cout << "-------------------------------" << endl;

        Stack<char> s3;
        cout << "size: " << s3.Size() << endl;

        s3.Push('a');
        s3.Push('b');

        cout << "-------------------------------" << endl;

        Stack<string> s4(3);
        string a = "abc";
        cout << "a = " << a << endl;
        s4.Push(a);
        s4.Push("bcd");
        s4.Push("cde");
        cout << "top: " << s4.Top() << endl;
        s4.Pop();
        cout << "top: " << s4.Top() << endl;

        //    cout << NULL << endl;
    } catch (exception const &ex) {
        cerr << "Exception: " << ex.what() << endl;
        return -1;
    }
}

output:

init, come on!
at the first, count = 0
push data: 1
push data: 2
size: 2
pop data: 2
top: 1
pop data: 1
There have no data. Pop fail.
There have no data, can't top.
0
size: 0
-------------------------------
max = 3
Stack is empty.
push data: 1
push data: 2
push data: 3
Stack is full. Can't push.
Stack's size: 3
-------------------------------
init, come on!
at the first, count = 0
size: 0
push data: a
push data: b
-------------------------------
max = 3
a = abc
push data: abc
push data: bcd
push data: cde
top: cde
pop data: cde
top: bcd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值