You are on page 1of 8

QUEUE USING LINKED LIST:

#include<iostream>

using namespace std;

class Node {
public:
int key;
int data;
Node * next;

Node()
{
key = 0;
data = 0;
next = NULL;
}
Node(int k, int d) {
key = k;
data = d;
next = NULL;
}
};

class Queue_Using_Linked_List
{
public:
Node *front_of_Linked_List;
Node *rear_of_Linked_List;

Queue_Using_Linked_List()
{
front_of_Linked_List = NULL;
rear_of_Linked_List = NULL;
}

bool Queue_is_Empty()
{
if(front_of_Linked_List==NULL && rear_of_Linked_List==NULL)
{
return true;
}
else
{
return false;
}
}

bool NodeExist(Node *n)


{
Node *temp = front_of_Linked_List;
bool exist=false;
while(temp!=NULL)
{
if(temp->key==n->key)
{
exist=true;
break;
}
temp=temp->next;
}
return exist;
}

void insert_in_Queue(Node *n)


{
if ( Queue_is_Empty())
{
front_of_Linked_List = n;
rear_of_Linked_List= n;
cout<<"NODE INSERTED"<<endl;
}
else if(NodeExist(n))
{
cout<<"NODE ALREADY EXISTS"
<<"Enter different Key value"<<endl;
}
else
{
rear_of_Linked_List->next=n;
rear_of_Linked_List=n;

cout<<"NODE INSERTED"<<endl;
}

}
Node* remove_from_Queue()
{
Node *temp=NULL;
if (Queue_is_Empty())
{
cout << "EMPTY QUEUE" << endl;
return NULL;
}
else
{
if(front_of_Linked_List==rear_of_Linked_List)
{
temp=front_of_Linked_List;
front_of_Linked_List = NULL;
rear_of_Linked_List = NULL;
return temp;
}
else
{
temp=front_of_Linked_List;
front_of_Linked_List = front_of_Linked_List->next;
return temp;
}

}
}

void display_Queue()
{
if(Queue_is_Empty())
{
cout << "EMPTY QUEUE" << endl;
}
else
{
cout << "VALUES OF THIS QUEUE ARE :" << endl;
Node *temp=front_of_Linked_List;
while(temp!=NULL)
{
cout<<"("<<temp->key<<","<<temp->data<<")"<<" -> ";
temp=temp->next;
}
cout<<endl;
}
}
};

int main() {
cout<<"\t\t By: Aleeza Anjum"<<endl;
cout<<"\t\t Reg No: 20-CS-101"<<endl;
Queue_Using_Linked_List q;
int option,key, data;

do {
cout<<"\n\n\t\t |----------------------------------------------------------|"<<endl;
cout << "\t\t |OPERATIONS TO PERFORM" <<"SELECT OPTION. ENTER
0 TO EXIT. |" << endl;
cout << "\t\t1.| INSERTION IN LNKED List |"<< endl;
cout << "\t\t2.| DELETION FROM LINKED LIST |" <<
endl;
cout << "\t\t3.| DISPLAY LINKED LIST |" << endl;
cout << "\t\t4.| CLEAR SCREEN |" << endl;
cout<< "\t\t |----------------------------------------------------------|"<<endl;
cin >> option;

Node * new_node = new Node();


switch (option)
{
case 0:
break;
case 1:
cout << "INSERTING...." <<endl;
cout << "Enter KEY and VALUE of NODE to insert:"<<endl;
cin >> key;
cin >> data;
new_node->key = key;
new_node->data = data;
q.insert_in_Queue(new_node);
break;
case 2:
cout << "DELETING..... " <<endl;
new_node = q.remove_from_Queue();
cout<<"Deleted Value is: ("<<new_node->key<<","
<<new_node->data<<")";
delete new_node;
cout<<endl;

break;
case 3:
cout << "DISPLAYING......... " << endl;
q.display_Queue();
cout << endl;
break;

case 4:
system("cls");
break;
default:
cout << "Enter Proper Option number " << endl;
}

} while (option != 0);

return 0;
}

You might also like