Skip to content

Instantly share code, notes, and snippets.

@miladabc
Created July 7, 2018 16:34
Show Gist options
  • Save miladabc/a5257dd91bf623e8d339b083fcf74843 to your computer and use it in GitHub Desktop.
Save miladabc/a5257dd91bf623e8d339b083fcf74843 to your computer and use it in GitHub Desktop.
Queue implementation with Linked List
//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