0% found this document useful (0 votes)
18 views

Assignment 4

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)
18 views

Assignment 4

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
You are on page 1/ 8

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;

dq.insert(1.655);

dq.inject(534.65);

dq.insert(43.43);

dq.inject(76.48);

cout<<"The initial deque is: ";

cout<<"The rear is : "<<dq.getRear()<<endl;

cout<<"The front is : "<<dq.getFront()<<endl;

dq.Display();

cout<<"The rear is : "<<dq.getRear()<<endl;

cout<<"The front is : "<<dq.getFront()<<endl;

dq.remove();

dq.removeFront();

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

dq.Display();

cout<<"The rear after deletion is : "<<dq.getRear()<<endl;

cout<<"The front after deletion is : "<<dq.getFront()<<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++) queue.enqueue(dist(rd));

queue.display();

while (size != 1)

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

queue.deleteAt(i);

queue.display();

size--;

cout << "Winner is: " << queue.getFront() << endl;

OUTPUT:

You might also like