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

来源:互联网 发布:电脑如何连接手机网络 编辑:程序博客网 时间:2024/05/20 10:15

栈操作

  • 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 = 0push data: 1push data: 2size: 2pop data: 2top: 1pop data: 1There have no data. Pop fail.There have no data, can't top.0size: 0-------------------------------max = 3Stack is empty.push data: 1push data: 2push data: 3Stack is full. Can't push.Stack's size: 3-------------------------------init, come on!at the first, count = 0size: 0push data: apush data: b-------------------------------max = 3a = abcpush data: abcpush data: bcdpush data: cdetop: cdepop data: cdetop: bcd
0 0
原创粉丝点击