A Queue is a linear data structure in which follows the First In First Out ( FIFO ) order while performing its operations. In Queue what we have inserted first will be deleted first. In Queue deletion operation is performed from one end named as Rear end or Tail of the Queue and the insertion operation is performed from another end named as Front or Head of the Queue.
Queue Array Implementation In C++:
//Queue implementation using c++
#include<iostream>
using namespace std;
#define MAX_SIZE 101
class Queue {
private:
int A[MAX_SIZE];
int front,rear;
public:
Queue(){
front=-1;
rear=-1;
}
bool IsEmpty(){
return (front ==-1 && rear==-1);
}
bool IsFull(){
return (front==0 && rear==MAX_SIZE-1);
}
void Display(){
if(IsEmpty()){
cout<<"Queue is Empty";
return;
}
cout<<"ELements: ";
for(int i=front;i<=rear;i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
void enQueue(int x){
if(IsFull()){
cout<<"Error: Queue Is Full";
}
else {
front=0;
rear++;
A[rear] = x;
cout<<"Inserted "<<x<<endl;
}
Display();
}
int deQueue(){
if(IsEmpty()){
cout<<"Error: Queue is Empty";
return 0;
}
if(front==0 && rear==0){
front=-1;
rear=-1;
}
else {
cout<<"Deleted "<<A[front]<<endl;
front++;
}
Display();
}
int Front(){
return A[front];
}
};
int main(){
Queue NewQ;
NewQ.enQueue(5);
NewQ.enQueue(56);
NewQ.enQueue(252);
NewQ.deQueue();
return 0;
}
Output:
Queue Linked List Implementation In C++:
First of all, while implementing a program we have to use the more efficient way. So that here we are going to implement all our Queue operations with O(1) time complexity. While implementing Queue with linked list the De queue operation will take O(1) time because we are deleting at the head which is also called the front. Insertion or deletion performed at the head in a linked list will take O(1) time.
But when we are performing Enqueue or Insertion the time complexity will become O(n) as we have to reach the rear and insert the new element at the rear, it takes O(n) time to reach the rear by crossing all the elements. If you are thinking like this to implement then you are wrong, there is no need to reach the rear by crossing all elements and update at the rear. We just have to store the rear in a variable from time to time with every operation. Then the time complexity will be O(1).
Let's see the implementation part now.
Code:
// Queue Linked List Implementation Using C++
#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* front = NULL;
Node* rear = NULL;
void Display(){
cout<<"Elements in Queue: ";
Node* temp = front;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"\n";
}
void enQueue(int x){
Node* temp = new Node();
temp->data = x;
temp->next = NULL;
if(front==NULL && rear==NULL){
front = temp;
rear = temp;
cout<<"Inserted "<<rear->data<<endl;
Display();
return;
}
rear->next=temp;
rear=temp;
cout<<"Inserted "<<rear->data<<endl;
Display();
}
void deQueue(){
if(front==NULL && rear==NULL){
cout<<"Error: Queue is empty"<<endl;
return;
}
if(front==rear){
cout<<"Deleted "<<front->data<<endl;
front=NULL;
rear=NULL;
}
else {
cout<<"Deleted "<<front->data<<endl;
front = front->next;
}
Display();
}
void Front(){
cout<<"Front: "<<front->data<<endl;
}
int main(){
enQueue(5);
enQueue(7);
enQueue(15);
Front();
deQueue();
Front();
enQueue(45);
Front();
}
//code ends
Output:
Queue List Implementation In Python:
# Queue list implementation using pythonOutput:
class Queue:
def __init__(self):
self.elements = []
def isEmpty(self):
return self.elements == []
def enQueue(self,x):
self.x = x
self.elements.append(x)
def deQueue(self):
self.elements.pop(0)
def Top(self):
self.top = self.elements[0]
return self.top
def Rear(self):
self.rear = self.elements[-1]
return self.rear
newQ = Queue()
newQ.enQueue(4)
newQ.enQueue(69)
newQ.enQueue(7)
print("Elements in Queue: ",newQ.elements)
print("Top: ",newQ.Top())
print("Rear: ",newQ.Rear())
newQ.deQueue()
print("Elements in Queue: ",newQ.elements)
print("Top: ",newQ.Top())
print("Rear: ",newQ.Rear())
newQ.enQueue(655)
print("Elements in Queue: ",newQ.elements)
print("Top: ",newQ.Top())
print("Rear: ",newQ.Rear())
#code ends
Queue Linked List Implementation In Python:
# Queue Linked List Implementation in Python
class Node:
def __init__(self,data):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.top=None
self.rear=None
def display(self):
if self.head is None:
print("Queue is Empty")
return
print("Elements in queue: ")
llstr = ''
temp = self.head
while temp:
llstr += str(temp.data) + ' '
temp = temp.next
print(llstr)
def enQueue(self,data):
if self.rear == None:
self.head = Node(data)
self.rear = self.head
else:
self.rear.next = Node(data)
self.rear = self.rear.next
self.display()
def deQueue(self):
if self.rear == None:
print("Error: Queue is Empty\n")
else:
self.head = self.head.next
self.display()
def Top(self):
print("TOp: ",self.head.data)
return self.head.data
def Rear(self):
print("Rear: ",self.rear.data)
return self.rear.data
newQ = Queue()
newQ.enQueue(65)
newQ.Top()
newQ.Rear()
newQ.enQueue(26)
newQ.Top()
newQ.Rear()
newQ.enQueue(45)
newQ.Top()
newQ.Rear()
newQ.enQueue(849)
newQ.Top()
newQ.Rear()
newQ.deQueue()
newQ.Top()
newQ.Rear()
# Code Ends
Output:
No comments:
Post a Comment