Skip to content

Instantly share code, notes, and snippets.

@sarveshbhatnagar
Created April 2, 2018 19:07
Cpp code for queue
//
// main.cpp
// queue
// Created by Sarvesh Bhatnagar on 05/03/18.
// Copyright © 2018 introtoalgo.com. All rights reserved.
//
#include <iostream>
using namespace std;
struct doublyLinkedList{
int id;
struct doublyLinkedList *next;
struct doublyLinkedList *prev;
};
struct doublyLinkedList *temp,*first,*last;
void makeDoublyLinkedList();
void enQueue();
void deQueue();
int main() {
cout<<"Enter Number of Queue Items you want to insert"<<endl;
int n;
cin>>n;
for (int i = 0; i<n; i++) {
enQueue();
}
cout<<"Enter how many Queue items you want to process? "<<endl;
cin>>n;
for (int i = 0; i<n; i++) {
deQueue();
}
return 0;
}
void enQueue(){
temp = new doublyLinkedList();
temp->prev = NULL;
temp->next = NULL;
if (first == NULL) {
first = temp;
}
else{
last->prev = temp;
temp->next = last;
}
last = temp;
cout<<"Enter id"<<endl;
cin>>temp->id;
}
void deQueue(){
temp = first;
if (first != NULL) {
cout<<first->id<<endl;
if (first->prev != NULL) {
first = first->prev;
}
else{
cout<<"Queue Emptied"<<endl;
first = NULL;
}
free(temp);
}
else{
cout<<"Empty Queue"<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment