0% found this document useful (0 votes)
2 views12 pages

AIM:-Create and Perform Different Operations On Double-Ended Queues Using Linked List Implementation. CODE

The document contains code for implementing a double-ended queue (Deque) using a linked list in C++. It includes methods for inserting and deleting elements from both ends, as well as retrieving the front and rear elements, checking if the deque is empty, and calculating its size. Additionally, it provides code for calculating the factorial of a number using both recursion and iteration.

Uploaded by

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

AIM:-Create and Perform Different Operations On Double-Ended Queues Using Linked List Implementation. CODE

The document contains code for implementing a double-ended queue (Deque) using a linked list in C++. It includes methods for inserting and deleting elements from both ends, as well as retrieving the front and rear elements, checking if the deque is empty, and calculating its size. Additionally, it provides code for calculating the factorial of a number using both recursion and iteration.

Uploaded by

CS 73 Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

AIM:-Create and perform different

operations on Double-ended Queues using


linked list implementation.
CODE:-
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *prev, *next;
static Node* getnode(int data)
{
Node* newNode =
(Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = newNode->next = NULL;
return newNode;
}
};
class Deque
{
Node* front;
Node* rear;
int Size;

public:
Deque()
{
front = rear = NULL;
Size = 0;
}
void insertFront(int data);
void insertRear(int data);
void deleteFront();
void deleteRear();
int getFront();
int getRear();
int size();
bool isEmpty();
void erase();
};
bool Deque::isEmpty()
{
return (front == NULL);
}
int Deque::size()
{
return Size;
}
void Deque::insertFront(int data)
{
Node* newNode = Node::getnode(data);
if (newNode == NULL)
cout << "OverFlow\n";
else
{
if (front == NULL)
rear = front = newNode;
else
{
newNode->next = front;
front->prev = newNode;
front = newNode;
}
Size++;
}
}
void Deque::insertRear(int data)
{
Node* newNode = Node::getnode(data);
if (newNode == NULL)
cout << "OverFlow\n";
else
{
if (rear == NULL)
front = rear = newNode;
else
{
newNode->prev = rear;
rear->next = newNode;
rear = newNode;
}

Size++;
}
}
void Deque::deleteFront()
{
if (isEmpty())
cout << "UnderFlow\n";
else
{
Node* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
else
front->prev = NULL;
free(temp);
Size--;
}
}
void Deque::deleteRear()
{
if (isEmpty())
cout << "UnderFlow\n";
else
{
Node* temp = rear;
rear = rear->prev;
if (rear == NULL)
front = NULL;
else
rear->next = NULL;
free(temp);
Size--;
}
}
int Deque::getFront()
{
if (isEmpty())
return -1;
return front->data;
}
int Deque::getRear()
{
if (isEmpty())
return -1;
return rear->data;
}
void Deque::erase()
{
rear = NULL;
while (front != NULL)
{
Node* temp = front;
front = front->next;
free(temp);
}
Size = 0;
}
int main()
{
Deque dq;
cout << "Insert element '5' at rear end\n";
dq.insertRear(5);

cout << "Insert element '10' at rear end\n";


dq.insertRear(10);

cout << "Rear end element: "


<< dq.getRear() << endl;

dq.deleteRear();
cout << "After deleting rear element new rear"
<< " is: " << dq.getRear() << endl;

cout << "Inserting element '15' at front end \n";


dq.insertFront(15);

cout << "Front end element: "


<< dq.getFront() << endl;

cout << "Number of elements in Deque: "


<< dq.size() << endl;

dq.deleteFront();
cout << "After deleting front element new "
<< "front is: " << dq.getFront() << endl;

return 0;
}
OUTPUT:-
AIM:- WAP TO CALCULATE FACTORIAL AND
TO COMPUTE THE FACTORS OF A GIVEN NO
(i) using recurion (ii) using iteration.
CODE:-
(i) Using recursion
#include<iostream>
using namespace std;

int factorial(int n);

int main()
{
int n;

cout << "Enter a positive integer: ";


cin >> n;

cout << "Factorial of " << n << " = " <<


factorial(n);

return 0;
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}

OUTPUT:-

(ii) Using iteration


#include <iostream>
using namespace std;
int main() {
int n = 6, fact = 1, i;
for(i=1; i<=n; i++)
fact = fact * i;
cout<<"Factorial of "<< n <<" is "<<fact;
return 0;
}

OUTPUT:-

You might also like