[数据结构]Linked_queue
2016-03-30 18:11
405 查看
//LInked_queue.h #pragma once enum Error_code { success, overflow, underflow }; template<class Queue_entry> class Linked_queue { public: Linked_queue(); ~Linked_queue(); Error_code append(const Queue_entry &item); Error_code serve(); Error_code retriver(Queue_entry &item)const; Error_code serve_and_retrieve(Queue_entry &item); bool empty()const; void clear(); int size(); void print(); private: struct Node { Node(); Node(Queue_entry item, Node *add_on = NULL); Node *next; Queue_entry entry; }; int count; Node *head; }; template<class Queue_entry> inline Linked_queue<Queue_entry>::Node::Node() { next = NULL; } template<class Queue_entry> inline Linked_queue<Queue_entry>::Node::Node(Queue_entry item, Node * add_on) { entry = item; next = add_on; }
//Linked_queue.cpp #include<iostream> #include"LInked_queue.h" using namespace std; template<class Queue_entry> Linked_queue<Queue_entry>::Linked_queue() { count = 0; Node *head = NULL; } template<class Queue_entry> Linked_queue<Queue_entry>::~Linked_queue() { clear(); } template<class Queue_entry> Error_code Linked_queue<Queue_entry>::append(const Queue_entry & item) { if (count==0) { head = new Node(item); count++; return success; } else { Node *temp = head; while (temp->next)temp=temp->next; temp->next = new Node(item); count++; return success; } } template<class Queue_entry> Error_code Linked_queue<Queue_entry>::serve() { if (count == 0)return underflow; Node *temp = head; head = head->next; delete temp; count--; return success; } template<class Queue_entry> Error_code Linked_queue<Queue_entry>::retriver(Queue_entry & item) const { if (empty())return underflow; item = head->entry; return success; } template<class Queue_entry> Error_code Linked_queue<Queue_entry>::serve_and_retrieve(Queue_entry & item) { if (count == 0)return underflow; retriver(item); serve(); return success; } template<class Queue_entry> bool Linked_queue<Queue_entry>::empty() const { if (count == 0)return true; else return false; } template<class Queue_entry> void Linked_queue<Queue_entry>::clear() { while (count) serve(); } template<class Queue_entry> int Linked_queue<Queue_entry>::size() { return count; } template<class Queue_entry> void Linked_queue<Queue_entry>::print() { Node *temp = head; while (temp) { cout << temp->entry << ' '; temp = temp->next; } }
//main.cpp #include<iostream> #include"Linked_queue.cpp" using namespace std; void UI() { cout << "******************************************Linked_queue*************************************************" << endl; cout << "MENU:" << endl; cout << "1.Append an item." << endl; cout << "2.Pop the first item of the queue." << endl; cout << "3.Print the size of the queue." << endl; cout << "4.Clear the queue." << endl; cout << "5.Print the first item." << endl; cout << "6.Print and pop the first item." << endl; cout << "0.to end the programm." << endl; } void main() { UI(); Linked_queue<int> test; while (1) { system("cls"); UI(); cout << "Please enter the number before the opreator." << endl; char key; cin >> key; /*cin.get(key); cin.ignore(100, '\n');*/ while (key != '1' && key != '2' && key != '3' && key != '4' && key != '5' && key != '6' && key != '0'&& key != '7') { cout << "Unvalidated Input!Please enter again." << endl; cin >> key; /*cin.get(key); cin.ignore(100, '\n');*/ } switch (key) { case '1': { int item; cout << "Please enter an int item." << endl; cin >> item; if (test.append(item) == success)cout << "Append success!" << endl; system("pause"); break; } case'2': { if (test.serve() == success)cout << "Pop success!" << endl; else cout << "There is no item in the queue." << endl; system("pause"); break; } case'5': { int item; if (test.retriver(item) == success) cout << "The first item of the queue is\"" << item << "\"." << endl; else cout << "There is no item in the queue." << endl; system("pause"); break; } case'4': { test.clear(); cout << "Clear success!" << endl; system("pause"); break; } case'3': { cout << "The size of the queue is " << test.size() << endl; system("pause"); break; } case'6': { int item; if (test.retriver(item) == success) cout << "The first item of the queue is\"" << item << "\"." << endl; if (test.serve() == success)cout << "Pop success!" << endl; else cout << "There is no item in the queue." << endl; system("pause"); break; } case'7': { test.print(); system("pause"); break; } case'0':cout << "You'll exit the programm later." << endl; system("pause"); return; } } }
相关文章推荐
- 《数据结构》 队列(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
- 【暑假】[实用数据结构]UVa11235 Frequent values