entire e31 updated old one had error and was goofy

This commit is contained in:
Tanmay 2023-12-11 17:06:21 +05:30
parent 3a6ad7c77f
commit 4e5b364e11

View File

@ -1,161 +1,172 @@
#include<iostream>
using namespace std;
#define SIZE 5
class Dequeue
class deque1
{
int a[10], front, rear, count;
public:
Dequeue()
int f,r,data[100],n;
deque1()
{
front = -1;
rear = -1;
count = 0;
f = r = -1;
cout<<"Enter number of elements: ";
cin>>n;
}
void addBegin(int item)
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())
{
if(f == -1)
f = r = 0;
else if(f == 0)
f = n - 1;
else
f--;
data[f] = id;
}
else
cout<<"\nQueue is full....\n";
}
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;
else
r++;
data[r] = id;
}
else
cout<<"\nQueue is full....\n";
}
void deque1::dequeuef()
{
if(!isEmpty())
{
if(f == r)
f = r = -1;
else if(f == n-1)
f = 0;
else
f++;
}
else
cout<<"\nQueue is empty....\n";
}
void deque1::dequeuer()
{
if(!isEmpty())
{
if(f == r)
f = r = -1;
else if(r == 0)
r = n-1;
else
r--;
}
else
cout<<"\nQueue is empty....\n";
}
void deque1::display()
{
int i;
if (front == -1)
cout<<"\nDeque: ";
if(!isEmpty())
{
front++;
rear++;
a[rear] = item;
count++;
}
else if (rear >= SIZE - 1)
i = f;
do
{
cout << "\nInsertion is not possible,overflow!!!!";
cout<<data[i]<<" ";
i = (i+1)%n;
}while(i != (r+1)%n);
}
else
{
for (i = count; i >= 0; i--)
{
a[i] = a[i - 1];
cout<<"\nQueue is empty....\n";
cout<<"\n";
}
a[i] = item;
count++;
rear++;
}
}
void addEnd(int item)
{
if (front == -1)
bool deque1::isFull()
{
front++;
rear++;
a[rear] = item;
count++;
}
else if (rear >= SIZE - 1)
{
cout << "\nInsertion is not possible,overflow!!!";
return;
}
if((f==0 && r >= n - 1)|| f==r+1)
return true;
else
{
a[++rear] = item;
return false;
}
}
void deleteFront()
{
if (front == -1)
{
cout << "Deletion is not possible:: Dequeue is empty";
return;
}
else
{
if (front == rear)
{
front = rear = -1;
return;
}
cout << "The deleted element is " << a[front];
front = front + 1;
}
}
void deleteEnd()
{
if (front == -1)
{
cout << "Deletion is not possible:Dequeue is empty";
return;
}
else
{
if (front == rear)
{
front = rear = -1;
}
cout << "The deleted element is " << a[rear];
rear = rear - 1;
}
}
void display()
{
for (int i = front; i <= rear; i++)
bool deque1::isEmpty()
{
cout << a[i] << " ";
if(r == -1)
return true;
else
return false;
}
}
};
int main()
{
int c, item;
Dequeue d1;
do
deque1 obj;
int ch;
bool flag = true;
while(flag)
{
cout << "\n\n****DEQUEUE OPERATION****\n";
cout << "\n1-Insert at beginning";
cout << "\n2-Insert at end";
cout << "\n3_Display";
cout << "\n4_Deletion from front";
cout << "\n5-Deletion from rear";
cout << "\n6_Exit";
cout << "\nEnter your choice<1-6>:";
cin >> c;
switch (c)
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)
{
case 1:
cout << "Enter the element to be inserted:";
cin >> item;
d1.addBegin(item);
obj.enqueuef();
obj.display();
break;
case 2:
cout << "Enter the element to be inserted:";
cin >> item;
d1.addEnd(item);
obj.enqueuer();
obj.display();
break;
case 3:
d1.display();
obj.dequeuef();
obj.display();
break;
case 4:
d1.deleteFront();
obj.dequeuer();
obj.display();
break;
case 5:
d1.deleteEnd();
obj.display();
break;
case 6:
exit(1);
flag = false;
break;
default:
cout << "Invalid choice";
cout<<"\nEnter valid choice!!!\n";
break;
}
} while (c != 7);
}
return 0;
}