DSL/assignment-31.cpp

173 lines
2.8 KiB
C++
Raw Normal View History

#include<iostream>
2023-10-26 20:35:33 +05:30
using namespace std;
class deque1
2023-10-26 20:35:33 +05:30
{
public:
int f,r,data[100],n;
deque1()
2023-10-26 20:35:33 +05:30
{
f = r = -1;
cout<<"Enter number of elements: ";
cin>>n;
2023-10-26 20:35:33 +05:30
}
void enqueuef();
void enqueuer();
void dequeuef();
void dequeuer();
bool isEmpty();
bool isFull();
void display();
};
void deque1::enqueuef()
{
int id;
cout<<"\nEnter element: ";
cin>>id;
if(!isFull())
2023-10-26 20:35:33 +05:30
{
if(f == -1)
f = r = 0;
else if(f == 0)
f = n - 1;
2023-10-26 20:35:33 +05:30
else
f--;
data[f] = id;
2023-10-26 20:35:33 +05:30
}
else
cout<<"\nQueue is full....\n";
}
2023-10-26 20:35:33 +05:30
void deque1::enqueuer()
{
int id;
cout<<"\nEnter element: ";
cin>>id;
if(!isFull())
{
if(f == -1)
f = r = 0;
else if(r == n - 1)
r = 0;
2023-10-26 20:35:33 +05:30
else
r++;
data[r] = id;
2023-10-26 20:35:33 +05:30
}
else
cout<<"\nQueue is full....\n";
}
void deque1::dequeuef()
{
if(!isEmpty())
2023-10-26 20:35:33 +05:30
{
if(f == r)
f = r = -1;
else if(f == n-1)
f = 0;
2023-10-26 20:35:33 +05:30
else
f++;
2023-10-26 20:35:33 +05:30
}
else
cout<<"\nQueue is empty....\n";
}
void deque1::dequeuer()
{
if(!isEmpty())
2023-10-26 20:35:33 +05:30
{
if(f == r)
f = r = -1;
else if(r == 0)
r = n-1;
2023-10-26 20:35:33 +05:30
else
r--;
2023-10-26 20:35:33 +05:30
}
else
cout<<"\nQueue is empty....\n";
}
2023-10-26 20:35:33 +05:30
void deque1::display()
{
int i;
cout<<"\nDeque: ";
if(!isEmpty())
{
i = f;
do
2023-10-26 20:35:33 +05:30
{
cout<<data[i]<<" ";
i = (i+1)%n;
}while(i != (r+1)%n);
2023-10-26 20:35:33 +05:30
}
else
cout<<"\nQueue is empty....\n";
cout<<"\n";
}
2023-10-26 20:35:33 +05:30
bool deque1::isFull()
2023-10-26 20:35:33 +05:30
{
if((f==0 && r >= n - 1)|| f==r+1)
return true;
else
return false;
}
2023-10-26 20:35:33 +05:30
bool deque1::isEmpty()
{
if(r == -1)
return true;
else
return false;
}
2023-10-26 20:35:33 +05:30
int main()
{
deque1 obj;
int ch;
bool flag = true;
while(flag)
{
cout<<"\n****YOUR CHOICES ARE****\n";
cout<<"\n1. Enqueue(at front) \n2. Enqueue(at rear) \n3. Dequeue(at front) \n4. Dequeue(at rear) \n5. Display queue \n6. Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
2023-10-26 20:35:33 +05:30
{
case 1:
obj.enqueuef();
obj.display();
2023-10-26 20:35:33 +05:30
break;
case 2:
obj.enqueuer();
obj.display();
2023-10-26 20:35:33 +05:30
break;
case 3:
obj.dequeuef();
obj.display();
2023-10-26 20:35:33 +05:30
break;
case 4:
obj.dequeuer();
obj.display();
2023-10-26 20:35:33 +05:30
break;
2023-10-26 20:35:33 +05:30
case 5:
obj.display();
2023-10-26 20:35:33 +05:30
break;
case 6:
flag = false;
2023-10-26 20:35:33 +05:30
break;
default:
cout<<"\nEnter valid choice!!!\n";
2023-10-26 20:35:33 +05:30
break;
}
}
2023-10-26 20:35:33 +05:30
return 0;
}