0% found this document useful (0 votes)
51 views8 pages

C++ Deque and Queue Implementations

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

C++ Deque and Queue Implementations

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT 4 :

PROBLEM 1:

CODE:

#include<iostream>

using namespace std;

template <class T>

class Node

public:

Node<T>* next;

Node<T>* prev;

T data;

};

template <class T>

class Deque

private:

Node<T>* head;

Node<T>* rear;

public:

Deque();

void insert(T x);

void inject(T x);

void removeFront();

void remove();

void Display();

T getFront()

{return head->data;}

T getRear()

{return rear->data;}

};
template<class T>

Deque<T>::Deque()

head=NULL;

rear=NULL;

template<class T>

void Deque<T>::insert(T x)

Node<T>* newNode=new Node<T>;

newNode->data=x;

if(!head)

head=newNode;

rear=newNode;

else

newNode->next=head;

head->prev=newNode;

head=newNode;

head->prev=rear;

rear->next=head;

template<class T>

void Deque<T>::inject(T x)

if(!head)

Node<T>* newNode=new Node<T>;


newNode->data=x;

head=newNode;

rear=newNode;

else

Node<T>* newNode=new Node<T>;

newNode->data=x;

newNode->prev=rear;

rear->next=newNode;

rear=newNode;

rear->next=head;

head->prev=rear;

template<class T>

void Deque<T>::remove()

Node<T>* temp=rear;

rear=rear->prev;

head->prev=rear;

delete temp;

template<class T>

void Deque<T>::removeFront()

Node<T>* temp=head;

head=head->next;

rear->next=head;

delete temp;

}
template<class T>

void Deque<T>::Display()

Node<T>* curr=head;

do

cout<<curr->data<<" ";

curr=curr->next;

}while(curr!=head);

cout<<endl;

int main()

Deque<double>dq;

[Link](1.655);

[Link](534.65);

[Link](43.43);

[Link](76.48);

cout<<"The initial deque is: ";

cout<<"The rear is : "<<[Link]()<<endl;

cout<<"The front is : "<<[Link]()<<endl;

[Link]();

cout<<"The rear is : "<<[Link]()<<endl;

cout<<"The front is : "<<[Link]()<<endl;

[Link]();

[Link]();

cout<<"The deque after deletion the front and rear elements is: ";

[Link]();

cout<<"The rear after deletion is : "<<[Link]()<<endl;

cout<<"The front after deletion is : "<<[Link]()<<endl;

return 0;}
OUTPUT:

PROBLEM 2:

CODE:

#include <iostream>

#include <random>

using namespace std;

const int MAX_QUEUE = 5;

random_device rd;

template <class T>

class Queue {

private:

T items[MAX_QUEUE];

int front, back, count;

public:

Queue()

:front(0), back(0), count(0) {}

bool isEmpty() const {

return count == 0;

bool isFull() const {

return count == MAX_QUEUE;

}
void enqueue(const T& obj) { //insert it backwards

if (isEmpty()) {

items[front] = obj;

count++;

else if (!isFull()) {

back = (back + 1) % MAX_QUEUE;

items[back] = obj;

count++;

void deleteAt(int i) {

for (; i != back; i = (i + 1) % MAX_QUEUE) {

items[i] = items[i + 1];

back = (back - 1) % MAX_QUEUE;

count--;

T& getFront() {

return items[front];

T& getRear() {

return items[back];

void dequeue() {

if (!isEmpty()) {

front = (front + 1) % MAX_QUEUE;

count--;

void display() {
for (int curr = front, ctr = count; ctr > 0; curr = (curr + 1) % MAX_QUEUE, ctr--) {

cout << items[curr] << ' ';

cout << '\n';

};

int main() {

Queue<int> queue;

int k, i = 0;

int size = MAX_QUEUE;

cin >> k;

uniform_int_distribution<int> dist(1, 9);

for (int l = 0; l < MAX_QUEUE; l++) [Link](dist(rd));

[Link]();

while (size != 1)

for (int j=k; j>1; i = (i + 1) % size, j--);

[Link](i);

[Link]();

size--;

cout << "Winner is: " << [Link]() << endl;

OUTPUT:

You might also like