Created
June 27, 2019 08:29
-
-
Save manojnaidu619/e44e64c92b4e9497e4ea86c9791087a2 to your computer and use it in GitHub Desktop.
Double Ended Queue using array in CPP
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
#include <iostream> | |
using namespace std; | |
struct queue{ | |
int size; | |
int front; | |
int rear; | |
int *Q; | |
}; | |
struct queue p; | |
void create_queue(int size){ | |
p.size = size; | |
p.rear = p.front = -1; | |
p.Q = new int[p.size]; | |
} | |
void enq_rear(int ele){ | |
if(p.rear == (p.size-1)){ | |
cout << "queue is full!" << endl; | |
} | |
else{ | |
p.rear++; | |
p.Q[p.rear] = ele; | |
} | |
} | |
void deq_rear(){ | |
if(p.rear==-1){ | |
cout << "Queue is empty!" << endl; | |
}else{ | |
p.rear--; | |
} | |
} | |
void deq_front(){ | |
if(p.front==p.rear){ | |
cout << "Queue is empty!" << endl; | |
}else{ | |
p.front++; | |
} | |
} | |
void enq_front(int ele){ | |
if(p.front==-1){ | |
cout << "Cannot Enqueue, Delete using deq_front first!" << endl; | |
}else{ | |
p.Q[p.front] = ele; | |
p.front--; | |
} | |
} | |
void display(){ | |
if(p.front == p.rear){ | |
cout << "Queue is Empty!" << endl; | |
return; | |
} | |
for(int i=(p.front+1);i<=p.rear;i++){ | |
cout << p.Q[i] << " "; | |
} | |
} | |
void get_front_rear(){ | |
cout << endl << "Front : " << p.front << " | " << "Rear : " << p.rear << endl; | |
} | |
int main(){ | |
create_queue(20); | |
for(int i=1;i<=10;i++){ | |
enq_rear(i); | |
} | |
display(); | |
get_front_rear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment