用链表手动实现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
- 用链表手动实现C++栈操作(5种)
- 如何手动实现C语言中的字符串操作
- Java_手动实现链表操作
- 黑客技术之“手动操作栈”
- C语言实现栈操作
- C++/G++ 手动扩栈
- C语言实现出入栈操作
- C语言实现栈的操作
- C语言实现栈数据结构操作
- C语言实现栈的操作
- 栈的基本操作实现-c/c++
- C++:利用两个栈实现队列操作
- C语言实现栈的基本操作
- C语言数组实现栈操作
- C语言------数据结构(栈操作,数组实现)
- c语言手动实现反转链表 Reverse
- 栈的实现与操作(C语言实现)
- WIN7/XP用注册表关联指定后缀名和打开程序(手动【图文】和C编程两种实现)
- c++设计模式之迭代器模式
- cookie、session总结
- jQuery-Ajax 使用
- Java实战及解析 — Maven快速入门
- Callable与Future的介绍
- 用链表手动实现C++栈操作(5种)
- python中安装各种库小结
- HTML5实现获取地理位置信息并定位功能
- iOS --NSURLConnection 请求总结
- dll动态库(二)项目中引用自定义动态库
- [LeetCode] 79. Word Search
- log4j总结
- JSP 客户端请求
- 静态变量与全局变量