#include <iostream>
using namespace std;
////////////////////////////////////////////
/// ============= ARRAY ===================
////////////////////////////////////////////
class ArrayDS {
private:
int arr[100];
int size;
public:
ArrayDS(){ size = 0; }
void insert(int x){
if(size >= 100){
cout << "Array full!\n";
return;
}
arr[size++] = x;
}
void remove(){
if(size>0) size--;
else cout << "Array empty!\n";
}
bool search(int x){
for(int i=0;i<size;i++)
if(arr[i]==x) return true;
return false;
}
void print(){
if(size==0) { cout << "Array empty!\n"; return; }
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void printReverse(){
if(size==0) { cout << "Array empty!\n"; return; }
for(int i=size-1;i>=0;i--)
cout<<arr[i]<<" ";
cout<<endl;
}
};
////////////////////////////////////////////
/// ============ LINKED LIST =============
////////////////////////////////////////////
class LinkedList{
private:
struct Node{
int data;
Node* next;
};
Node* head;
public:
LinkedList(){
head = NULL;
}
void insertBeg(int x){
Node* n = new Node;
n->data = x;
n->next = head;
head = n;
}
void insertEnd(int x){
Node* n = new Node;
n->data = x;
n->next = NULL;
if(head==NULL){
head = n;
return;
}
Node* t = head;
while(t->next != NULL) t = t->next;
t->next = n;
}
void deleteValue(int x){
if(head==NULL){ cout<<"List empty!\n"; return; }
if(head->data==x){
Node* t = head;
head = head->next;
delete t;
return;
}
Node* prev = head;
Node* cur = head->next;
while(cur){
if(cur->data == x){
prev->next = cur->next;
delete cur;
return;
}
prev = cur;
cur = cur->next;
}
cout<<"Value not found!\n";
}
bool search(int x){
Node* t = head;
while(t){
if(t->data==x) return true;
t = t->next;
}
return false;
}
void display(){
if(head==NULL){ cout<<"List empty!\n"; return; }
Node* t = head;
while(t){
cout<<t->data<<" ";
t = t->next;
}
cout<<endl;
}
};
////////////////////////////////////////////
/// ================ STACK ================
////////////////////////////////////////////
class StackDS{
private:
int arr[100];
int top;
public:
StackDS(){ top = -1; }
void push(int x){
if(top>=99){ cout<<"Stack overflow!\n"; return; }
arr[++top] = x;
}
void pop(){
if(top>=0) top--;
else cout<<"Stack empty!\n";
}
int topElement(){
if(top>=0) return arr[top];
cout<<"Stack empty!\n";
return -1;
}
void display(){
if(top<0){ cout<<"Stack empty!\n"; return; }
for(int i=top;i>=0;i--)
cout<<arr[i]<<" ";
cout<<endl;
}
};
////////////////////////////////////////////
/// ================= QUEUE ===============
////////////////////////////////////////////
class QueueDS{
private:
int arr[100];
int front,rear;
public:
QueueDS(){
front = rear = 0;
}
void enqueue(int x){
if(rear>=100){ cout<<"Queue full!\n"; return; }
arr[rear++] = x;
}
void dequeue(){
if(front<rear) front++;
else cout<<"Queue empty!\n";
}
int frontElement(){
if(front<rear) return arr[front];
cout<<"Queue empty!\n";
return -1;
}
void display(){
if(front>=rear){ cout<<"Queue empty!\n"; return; }
for(int i=front;i<rear;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};
////////////////////////////////////////////
/// ================ MENUS ===============
////////////////////////////////////////////
int getIntInput(){
int x;
while(!(cin>>x)){
cout<<"Invalid input! Enter a number: ";
cin.clear();
cin.ignore(1000,'\n');
}
return x;
}
void arrayMenu(){
ArrayDS a;
int c,v;
while(true){
cout<<"\nArray Menu\n";
cout<<"1 insert\n2 delete\n3 print\n4 reverse\n5 search\n6 back\n";
c = getIntInput();
if(c==6) return;
if(c==1){ cout<<"value:"; v = getIntInput(); a.insert(v); }
else if(c==2) a.remove();
else if(c==3) a.print();
else if(c==4) a.printReverse();
else if(c==5){
cout<<"value:"; v = getIntInput();
if(a.search(v)) cout<<"Found\n";
else cout<<"Not Found\n";
}
}
}
void linkedMenu(){
LinkedList l;
int c,v;
while(true){
cout<<"\nLinkedList Menu\n";
cout<<"1 insert beg\n2 insert end\n3 delete value\n4 search\n5 display\n6 back\n";
c = getIntInput();
if(c==6) return;
if(c==1){ cout<<"value:"; v = getIntInput(); l.insertBeg(v); }
else if(c==2){ cout<<"value:"; v = getIntInput(); l.insertEnd(v); }
else if(c==3){ cout<<"value:"; v = getIntInput(); l.deleteValue(v); }
else if(c==4){
cout<<"value:"; v = getIntInput();
if(l.search(v)) cout<<"Found\n";
else cout<<"Not Found\n";
}
else if(c==5) l.display();
}
}
void stackMenu(){
StackDS s;
int c,v;
while(true){
cout<<"\nStack Menu\n";
cout<<"1 push\n2 pop\n3 top\n4 display\n5 back\n";
c = getIntInput();
if(c==5) return;
if(c==1){ cout<<"value:"; v = getIntInput(); s.push(v); }
else if(c==2) s.pop();
else if(c==3) cout<<"Top="<<s.topElement()<<endl;
else if(c==4) s.display();
}
}
void queueMenu(){
QueueDS q;
int c,v;
while(true){
cout<<"\nQueue Menu\n";
cout<<"1 enqueue\n2 dequeue\n3 front\n4 display\n5 back\n";
c = getIntInput();
if(c==5) return;
if(c==1){ cout<<"value:"; v = getIntInput(); q.enqueue(v); }
else if(c==2) q.dequeue();
else if(c==3) cout<<"Front="<<q.frontElement()<<endl;
else if(c==4) q.display();
}
}
////////////////////////////////////////////
/// ================= MAIN ================
////////////////////////////////////////////
int main(){
int choice;
while(true){
cout<<"\n==== MAIN MENU ====\n";
cout<<"1 Array\n2 LinkedList\n3 Stack\n4 Queue\n5 Exit\n";
cout<<"Choice: ";
choice = getIntInput();
switch(choice){
case 1: arrayMenu(); break;
case 2: linkedMenu(); break;
case 3: stackMenu(); break;
case 4: queueMenu(); break;
case 5: return 0;
default: cout<<"Invalid choice!\n";
}
}
}