From 4e79d1b068f321ab8ba6a9fbb217b38fffff07cb Mon Sep 17 00:00:00 2001 From: Pooja Date: Sun, 8 Mar 2020 13:27:51 +0530 Subject: [PATCH 1/2] Create queue_using_linkedlist.cpp --- data_structure/queue_using_linkedlist.cpp | 97 +++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 data_structure/queue_using_linkedlist.cpp diff --git a/data_structure/queue_using_linkedlist.cpp b/data_structure/queue_using_linkedlist.cpp new file mode 100644 index 000000000..a27d3db37 --- /dev/null +++ b/data_structure/queue_using_linkedlist.cpp @@ -0,0 +1,97 @@ +/* + Write a program to implement Queue using linkedlist. +*/ +#include + + +struct linkedlist{ + int data; + linkedlist *next; + +}; +class stack_linkedList{ + public: + linkedlist *front; + linkedlist *rear; + + stack_linkedList(){ + front=rear=NULL; + } + void enqueue(int); + int dequeue(); + void display(); + +}; +void stack_linkedList::enqueue(int ele){ + + linkedlist *temp=new linkedlist(); + temp->data=ele; + temp->next=NULL; + + if(front==NULL) + front=rear=temp; + else{ + rear->next=temp; + rear=temp; + } +} +int stack_linkedList::dequeue(){ + linkedlist *temp; + int ele; + if(front==NULL) + std::cout<<"\nStack is empty"; + else{ + temp=front; + ele=temp->data; + if(front==rear) //if length of queue is 1; + rear=rear->next; + front=front->next; + delete(temp); + } + return ele; +} +void stack_linkedList::display(){ + + if(front==NULL) + std::cout<<"\nStack is empty"; + + else { + + linkedlist *temp; + temp=front; + while(temp!=NULL){ + std::cout<data<<" "; + temp=temp->next; + } + } +} + +int main(){ + + int op,data; + stack_linkedList ob; + std::cout<<"\n1. enqueue(Insertion) "; + std::cout<<"\n2. dequeue(Deletion)"; + std::cout<<"\n3. Display"; + std::cout<<"\n4. Exit"; + + while(1){ + std::cout<<"\nEnter your choice "; + std::cin>>op; + if(op==1) + { + std::cout<<"Enter data "; + std::cin>>data; + ob.enqueue(data); + } + else if(op==2) + data=ob.dequeue(); + else if(op==3) + ob.display(); + else if(op==4) + exit(0); + else + std::cout<<"\nWrong choice "; + + } +} From 56413a542d6441e1b10a7aef87b890f0aa21b964 Mon Sep 17 00:00:00 2001 From: Pooja Date: Tue, 21 Apr 2020 21:25:55 +0530 Subject: [PATCH 2/2] Update queue_using_linkedlist.cpp --- data_structure/queue_using_linkedlist.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structure/queue_using_linkedlist.cpp b/data_structure/queue_using_linkedlist.cpp index a27d3db37..8bc130c27 100644 --- a/data_structure/queue_using_linkedlist.cpp +++ b/data_structure/queue_using_linkedlist.cpp @@ -94,4 +94,5 @@ int main(){ std::cout<<"\nWrong choice "; } + return 0; }