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