DSL/assignment-31.cpp

211 lines
3.8 KiB
C++
Raw Normal View History

2023-10-26 20:35:33 +05:30
#include <iostream>
using namespace std;
#define SIZE 5
class Dequeue
{
int a[10], front, rear, count;
public:
Dequeue()
{
front = -1;
rear = -1;
count = 0;
}
void addBegin(int item)
{
int i;
if (front == -1)
{
front++;
rear++;
a[rear] = item;
count++;
}
else if (rear >= SIZE - 1)
{
cout << "\nInsertion is not possible,overflow!!!!";
}
else
{
for (i = count; i >= 0; i--)
{
a[i] = a[i - 1];
}
a[i] = item;
count++;
rear++;
}
}
void addEnd(int item)
{
if (front == -1)
{
front++;
rear++;
a[rear] = item;
count++;
}
else if (rear >= SIZE - 1)
{
cout << "\nInsertion is not possible,overflow!!!";
return;
}
else
{
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++)
{
cout << a[i] << " ";
}
}
};
int main()
{
int c, item;
Dequeue d1;
do
{
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)
{
case 1:
cout << "Enter the element to be inserted:";
cin >> item;
d1.addBegin(item);
break;
case 2:
cout << "Enter the element to be inserted:";
cin >> item;
d1.addEnd(item);
break;
case 3:
d1.display();
break;
case 4:
d1.deleteFront();
break;
case 5:
d1.deleteEnd();
break;
case 6:
exit(1);
break;
default:
cout << "Invalid choice";
break;
}
} while (c != 7);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Output ~~~~~~~~~~~~~~~~~~~~~~~~~
****DEQUEUE OPERATION****
1-Insert at beginning
2-Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-6>:1
Enter the element to be inserted:10
****DEQUEUE OPERATION****
1-Insert at beginning
2-Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-6>:2
Enter the element to be inserted:50
****DEQUEUE OPERATION****
1-Insert at beginning
2-Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-6>:4
The deleted element is 10
****DEQUEUE OPERATION****
1-Insert at beginning
2-Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-6>:6
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */