[数据结构]Linked_stack
2016-03-30 18:17
369 查看
//Link_Stack.h #pragma once enum Error_code{success,underflow,overflow}; template<class Stack_entry> class Linked_Stack { public: Linked_Stack(); bool empty()const; Error_code pop(); Error_code top(Stack_entry &item)const; Error_code push(const Stack_entry &item); ~Linked_Stack(); void operator=(const Linked_Stack &original); Linked_Stack(const Stack_entry &item); void print(); private: struct Node { Node(); Node(const Stack_entry &item, Node *add_on = NULL); Node *next; Stack_entry entry; }; Node *head; }; template<class Stack_entry> inline Linked_Stack<Stack_entry>::Node::Node() { next = NULL; } template<class Stack_entry> inline Linked_Stack<Stack_entry>::Node::Node(const Stack_entry & item, Node * add_on) { next = add_on; entry = item; }
//Linked_stack.cpp #include<iostream> #include"Link_Stack.h" using namespace std; template<class Stack_entry> Linked_Stack<Stack_entry>::Linked_Stack(){ head = NULL; } template<class Stack_entry> bool Linked_Stack<Stack_entry>::empty() const { if (head)return true; else return false; } template<class Stack_entry> Error_code Linked_Stack<Stack_entry>::pop() { if (!head)return underflow; Node *temp = head; head = head->next; delete temp; return success; } template<class Stack_entry> Error_code Linked_Stack<Stack_entry>::top(Stack_entry & item) const { if (head)return underflow; item = head->entry; return success; } template<class Stack_entry> Error_code Linked_Stack<Stack_entry>::push(const Stack_entry & item) { head = new Node(item, head); return success; } template<class Stack_entry> Linked_Stack<Stack_entry>::~Linked_Stack() { while (head) { Node *temp = head; head = head->next; delete temp; } } template<class Stack_entry> void Linked_Stack<Stack_entry>::operator=(const Linked_Stack & original) { Node *temp = original.head; Linked_Stack <Stack_entry> middle; if (head == original.head)return; while (temp) { middle.push(temp->entry); temp = temp->next; } while (head) pop(); temp = middle.head; while (temp) { push(temp->entry); temp = temp->next; } } template<class Stack_entry> Linked_Stack<Stack_entry>::Linked_Stack(const Stack_entry & original) { Node *new_head, *new_temp, *original_temp = original.head; if (original == NULL) { new_head == NULL; return; } new_temp = new_head = new Node(original_temp->entry); while (original_temp) { original_temp = original_temp->next; new_temp->next = new Node(original_temp->entry); new_temp = new_temp->next; } } template<class Stack_entry> void Linked_Stack<Stack_entry>::print() { Node *temp = head; while (temp) { cout << temp->entry << ' '; temp = temp->next; } cout << endl; }
//main.cpp #include<iostream> #include"Linked_Stack.cpp" using namespace std; void main() { cout << "1.push test1." << endl; cout << "2.push test2." << endl; cout << "3.pop test1." << endl; cout << "4.pop test1." << endl; cout << "5.make test1=test2." << endl; cout << "6.make test2=test1." << endl; cout << "7.print test1." << endl; cout << "8.print test2." << endl; Linked_Stack<int> test1, test2; while (1) { int key=0; cin >> key; switch (key) { case 1: { int item; cout << "please cin item." << endl; cin >> item; test1.push(item); cout << "--------------------------------------------------" << endl; break; } case 2: { int item; cout << "please cin item." << endl; cin >> item; test2.push(item); cout << "--------------------------------------------------" << endl; break; } case 3: test1.pop(); cout << "--------------------------------------------------" << endl; break; case 4: test2.pop(); cout << "--------------------------------------------------" << endl; break; case 5: test1 = test2; cout << "--------------------------------------------------" << endl; break; case 6: test2 = test1; cout << "--------------------------------------------------" << endl; break; case 7: test1.print(); cout << "--------------------------------------------------" << endl; break; case 8: test2.print(); cout << "--------------------------------------------------" << endl; break; } } }
相关文章推荐
- [数据结构]Linked_queue
- 《数据结构》 队列(Queue)操作代码集合
- 数据结构 - 简单的单链表结构
- 【暑假】[实用数据结构] AC自动机
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
- 【暑假】[实用数据结构]UVAlive 3026 Period
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
- 【暑假】[基本数据结构]根据BFS与DFS确定树
- 【暑假】[基本数据结构]根据in_order与post_order构树
- 【暑假】[基本数据结构]基本的数据结构知识点总结梳理
- 【暑假】[实用数据结构]动态连续和查询问题
- 【暑假】[实用数据结构]范围最小值问题(RMQ)
- 【暑假】[实用数据结构]动态范围查询问题
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
- 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?
- 【暑假】[实用数据结构]UVAlive 3135 Argus
- 【暑假】[实用数据结构]UVa11997 K Smallest Sums
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong