[数据结构]Linked_stack

来源:互联网 发布:淘宝搜索宝贝显示地址 编辑:程序博客网 时间:2024/05/01 17:46
//Link_Stack.h#pragma onceenum 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;}}}



0 0
原创粉丝点击