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

ds 12

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 views

ds 12

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/ 9

Name :

Nisha Kadam
Department : Computer
Engineering
Class : SE C
Roll No. : 75
Subject : Fundamentals of Data
Structure
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

Assignment –12
PROBLEM STATEMENT:-

A double-ended queue (deque) is a linear list in


which addition and deletion may be made at either
end.
Obtain a data representation mapping a deque into
a one-dimensional array.

Write a c++ program to simulate deque with


functions to add and delete elements from either
end of deque

-----------------------------------------
-----------------------------------------
------

PROGRAM:-
#include<iostream>
using namespace std;
#define SIZE 10

class dequeue
{
private: int arr[SIZE];
int front, rear;
public: dequeue()
{
front=-1;
rear=-1;
}
void insert_front(int );
void insert_rear(int );
void delete_front();
void delete_rear();
bool isEmpty();
bool isFull();
int get_rear();
int get_front();

};
int dequeue::get_rear()
{
if(isEmpty())
{
cout<<"\n Underflow !!";
return -1;
}
else
return arr[rear];
}

int dequeue::get_front()
{
if(isEmpty())
{
cout<<"\n Underflow !!";
return -1;
}
else
return arr[front];
}

bool dequeue::isFull()
{
if( (front==0 && rear==SIZE-1) || (front ==
rear+1))
return true;
else
return false;
}

bool dequeue::isEmpty()
{
if(front == -1)
return true;
else
return false;
}

void dequeue::insert_front(int x)
{
if(isFull())
{
cout<<"\n OVERFLOW!!! Queue is
Full!!";
}
else{
//if queue is empty
if(front==-1)
front=rear=0;
//if front points to first position
else if(front==0)
front=SIZE-1;
else
--front;

arr[front]=x;
}
}

void dequeue::insert_rear(int x)
{
if(isFull())
cout<<"\n OVERFLOW!!! Queue is
Full!!";
else
{
//if queue is empty
if(front== -1)
front=rear=0;
else if(rear==SIZE-1)
rear=0;
else
++rear;
arr[rear]=x;
}
}

void dequeue::delete_front()
{
if(isEmpty() )
cout<<"\n UNDERFLOW !! Queue is
Empty !!";
else
{
int x=arr[front];
//if only one element is present
if( front == rear)
front = rear =-1;
//if front points to last element
else if (front == SIZE-1)
front = 0;
else
++front;
cout<<"\n Element deleted from front :
"<<x;
}
}

void dequeue::delete_rear()
{
if(isEmpty() )
cout<<"\n UNDERFLOW !! Queue is
Empty !!";
else
{
int x=arr[rear];
//if only one element
if( front == rear )
front = rear =-1;
//if rear points to first element
else if( rear==0)
rear= SIZE-1;
else
--rear;
cout<<"\n Element deleted from rear :
"<<x;
}
}

int main()
{
int ch;
int n;
dequeue dq;

do
{
cout<<"\n\t\t\t1 : Insert to Front";
cout<<"\n\t\t\t2 : Insert to Rear";
cout<<"\n\t\t\t3 : Delete from front";
cout<<"\n\t\t\t4 : Delete from rear";
cout<<"\n\t\t\t5 : Display element at front";
cout<<"\n\t\t\t6 : Display element at rear";
cout<<"\n\t\t\t7 : Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<"\nEnter the element to insert
in dequeue from front: ";
cin>>n;
dq.insert_front(n);
break;
case 2 : cout<<"\nEnter the element to insert
in dequeue from rear: ";
cin>>n;
dq.insert_rear(n);
break;
case 3: dq.delete_front();
break;

case 4: dq.delete_rear();
break;
case 5: cout<<"\n Element at front :
"<<dq.get_front();
break;
case 6: cout<<"\n Element at rear :
"<<dq.get_rear();
break;
case 7 : cout<<"\nEnd of Program\n";
break;
default: cout<<"\nInvalid choice !! Try again\
n\n";
}
}while(ch != 7);
return 0;
}

OUTPUT :-
(
b
a
s
e
)

t
p
o
@
t
p
o
-
V
o
s
t
r
o
-
3
9
0
2
:
~
$

c
d
S
E
C
O
C
7
5
(
b
a
s
e
)

t
p
o
@
t
p
o
-
V
o
s
t
r
o
-
3
9
0
2
:
~
/
S
E
C
O
C
7
5
$

g
+
+

i
.
c
p
p
(base) tpo@tpo-Vostro-3902:~/SECOC75$ ./a.out

1 : Insert to Front
2 : Insert to Rear
3 : Delete from front
4 : Delete from rear
5 : Display element at front
6 : Display element at rear
7 : Exit

Enter your choice : 1


Enter the element to insert in dequeue from front:
10

1 : Insert to Front
2 : Insert to Rear
3 : Delete from front
4 : Delete from rear
5 : Display element at front
6 : Display element at rear
7 : Exit

Enter your choice : 3

Element deleted from front : 10

You might also like