Created
February 24, 2017 09:35
-
-
Save SergiyOsadchyy/949d38e4c85d6a18b7e0b9f81a482590 to your computer and use it in GitHub Desktop.
Queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// queue.hpp | |
// Queue | |
// | |
// Created by Sergiy on 24.02.17. | |
#ifndef queue_hpp | |
#define queue_hpp | |
#include <stdio.h> | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
struct Node | |
{ | |
string label; | |
Node* next; | |
}; | |
class FIFO | |
{ | |
private: | |
Node* head; | |
Node* tail; | |
public: | |
FIFO( ); | |
bool isEmpty( ); | |
void enqueue( Node new_node ); | |
void dequeue( Node * & getNode ); | |
void getNode( Node * & getNode ); | |
void print( ); | |
}; | |
#endif /* queue_hpp */ | |
//--------------- | |
// queue.cpp | |
// Queue | |
// | |
// Created by Sergiy on 24.02.17. | |
#include "queue.hpp" | |
FIFO :: FIFO() | |
{ | |
head = tail = NULL; | |
} | |
bool FIFO :: isEmpty() | |
{ | |
if (head == NULL) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
void FIFO :: enqueue(Node new_node) | |
{ | |
Node* addNode = new Node; | |
addNode->label = new_node.label; | |
addNode->next = NULL; | |
if ( isEmpty() ) | |
{ | |
head = tail = addNode; | |
return; | |
} | |
tail->next = addNode; | |
tail = addNode; | |
return; | |
} | |
void FIFO :: dequeue(Node * & getNode) | |
{ | |
if ( isEmpty() ) | |
{ | |
cout << "\nThe queue is empty. There are no more elements\n"; | |
return; | |
} | |
getNode->label = head->label; | |
Node* aux = head; | |
head = head->next; | |
aux->next = NULL; | |
delete aux; | |
return; | |
} | |
void FIFO :: getNode(Node * & getNode) | |
{ | |
getNode->label = head->label; | |
return; | |
} | |
void FIFO :: print() | |
{ | |
Node* aux = head; | |
while ( aux != NULL ) | |
{ | |
cout << aux->label << endl; | |
aux = aux->next; | |
} | |
return; | |
} | |
//--------------- | |
// main.cpp | |
// Queue | |
// | |
// Created by Sergiy on 24.02.17. | |
#include <iostream> | |
#include <string> | |
#include "queue.hpp" | |
using namespace std; | |
int main( int argc, const char * argv[] ) | |
{ | |
FIFO new_tail; | |
string label; | |
Node new_node; | |
Node* getNode = new Node; | |
char sel; | |
while ( true ) | |
{ | |
cout << "Choose one of the following opetarions:\n"; | |
cout << "a - Add an element to your TAIL\n"; | |
cout << "b - Print your queue\n"; | |
cout << "c - Read the HEAD element\n"; | |
cout << "d - Remove the last element\n"; | |
cin >> sel; | |
switch ( sel ) | |
{ | |
case 'a': | |
cout << "\nEnter a label: "; | |
cin >> label; | |
new_node.label = label; | |
new_tail.enqueue( new_node ); | |
cout << endl << endl; | |
break; | |
case 'b': | |
cout << "\nThe queue is consists of the following elements:" << endl; | |
new_tail.print( ); | |
cout << endl << endl; | |
break; | |
case 'c': | |
cout << "\nThe HEAD element of queue has value \""; | |
new_tail.getNode( getNode ); | |
cout << getNode->label << "\"" << endl << endl; | |
break; | |
case 'd': | |
cout << "\nThe element with value \""; | |
new_tail.dequeue( getNode ); | |
cout << getNode->label << "\" was removed from the queue" << endl << endl; | |
break; | |
default: | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment