Created
July 7, 2018 16:34
-
-
Save miladabc/a5257dd91bf623e8d339b083fcf74843 to your computer and use it in GitHub Desktop.
Queue implementation with Linked List
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
//Milad Abbasi | |
//06-07-2018 | |
//Queue implementation with Linked List | |
#include <iostream> | |
using namespace std; | |
class Node | |
{ | |
public: | |
int data; | |
Node *next; | |
}; | |
class Queue | |
{ | |
Node *front, *rear; | |
public: | |
Queue() | |
{ | |
front = rear = NULL; | |
} | |
void enqueue(int value) | |
{ | |
Node *tmp = new Node; | |
tmp->data = value; | |
tmp->next = NULL; | |
if(!rear) | |
front = rear = tmp; | |
else | |
{ | |
rear->next = tmp; | |
rear = tmp; | |
} | |
} | |
void dequeue(void) | |
{ | |
if(!front) | |
cout<<"Queue is empty!"; | |
else | |
{ | |
Node *tmp = new Node; | |
tmp = front; | |
front = front->next; | |
delete tmp; | |
} | |
} | |
int peek(void) | |
{ | |
return front->data; | |
} | |
bool isEmpty() | |
{ | |
return front == rear; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment