//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;
}
}
}