Your ultimate guide in programming...

DOUBLY QUEUE IMPLEMENTATION C++

DOUBLY QUEUE IMPLEMENTATION C++


// DOUBLY QUEUE IMPLEMENTATION
#include<iostream>
using namespace std;
struct Node{ //NODE STRUCTURE
Node *prev;
int data;
Node *next;
};
class Doubly_Queue{ //DOUBLY_QUEUE CLASS
Node *front;
Node *rear;
public:
Doubly_Queue()
{
front = NULL;
rear = NULL;
}
void insertion(int x) //INSERTION FUNCTION
{
Node *ptr = new Node;
ptr->data = x;
if(rear == NULL)
{
ptr->prev = NULL;
ptr->next = NULL;
rear = ptr;
front = ptr;
}
else
{
rear->next = ptr;
ptr->prev = rear;
ptr->next = NULL;
rear = ptr;
}

}
int remove() // REMOVE OR DELETE FUNCTION
{
int copy = front->data;
Node *rptr = front;
front = front->next;
if(front == NULL)
{
rear = NULL;
}
delete rptr;
return copy;
}
void previous() // PERVIOUS VALUES DISPLAYING FUNCTION
{
Node *curr = rear;
while(curr != NULL)
{
cout<<curr->data<<" ";
curr = curr->prev;
}
}
void display() // NORMAL DISPLAY FUNCTOIN
{
Node *curr = front;
while(curr != NULL)
{
cout<<curr->data<<" ";
curr = curr->next;
}
}
};
// MAIN DRIVEN...
int main()
{
Doubly_Queue q; // OBJECT OF CLASS
char choice;
do{
int input;
cout<<"1) TO INSERT: \n";
cout<<"2) TO REMOVE: \n";
cout<<"3) FOR PREVIOUS DISPLAY: \n";
cout<<"4) FOR NORMAL DISPLAYING: ";
cin>>input;
system("clear");
switch (input)
{
case 1:
int num;
cout<<"ENTER NUMBER TO INSERT :";
cin>>num;
q.insertion(num);
break;
case 2:{
cout<<"REMOVED NUMBER IS: "<<q.remove()<<endl;
break;
}
case 3:{
q.previous();
break;
}
case 4:{
q.display();
break;
}
default:
cout<<"ERROR 404: ";
break;
}
cout<<"DO YOU WANT TO MORE: (Y/N)...";
cin>>choice;
system("clear");
}while(choice == 'y');
return 0;

}

Share:

No comments:

Post a Comment

Newest Post

Coding Problem To Solve Exercise 3

                   Coding Problem To Solve Exercise 3 Objectives: Practice and understanding of basic c++ programs Control Structure(repetit...

Popular Posts

Recent Posts