Header Ads

Stack and Queue

 ...........Some problem of Queue and Stack............


//......................Queue.....................
//Problem01 :Write C++ code to implement a queue and its operations

Solved of this problem:

#include <iostream>

using namespace std;

int queue[100], k= 100, front = - 1, rear = - 1;

int InsertInput;


void Insert() {

for(int i=0; i<5; i++)

   if (rear == k- 1)

   {

       cout<<"YOur Queue is Overflow"<<endl;

   }

   else {

      if (front == - 1)

      front = 0;

      cout<<"Insert your queue elements :"<<" ";

      cin>>InsertInput;

      rear++;

      queue[rear] = InsertInput;

   }

}

void Delete() {

int Delete;

   if (front == - 1 || front > rear) {

      cout<<"Queue Underflow ";

      return ;

   }

    else {

     cout<<"Enter a element for delete from queue:"<<" ";

     cin>>Delete;

      cout<<"Delete your elements from queue : "<< queue[front] <<endl;

      front++;

   }

}

void Display() {

   if (front == - 1)

   {

          cout<<"Your queue is empty"<<endl;

   }

   else {

      cout<<"Your queue elements are : "<<" ";

      for (int i = front; i <= rear; i++)

    {

         cout<<queue[i]<<" ";

      }

         cout<<endl;

   }

}

int main() {

    Insert();

    Delete();

    Display();

     return 0;

   }


                                                  ''OR''


#include <iostream>

using namespace std;

int queue[100], k= 100, front = - 1, rear = - 1;

void Insert() {

int InsertInput;
if (rear == k- 1)
cout<<"YOur Queue is Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert your queue elements :"<<" ";
cin>>InsertInput;
rear++;
queue[rear] = InsertInput;
}
}
void Delete() {

if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Delete your elements from queue : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Your queue is empty"<<endl;
else {
cout<<"Your queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int userInput;
cout<<"1) Insert your queue elements"<<endl;
cout<<"2) Delete your elements from queue"<<endl;
cout<<"3) Display your all elements of queue"<<endl;
cout<<"4) Exit"<<endl;

for(int userInput=0; userInput!=5; userInput++)
{
cout<<"Enter your choice:"<<" ";
cin>>userInput;
switch (userInput) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"You have input a invalid number"<<endl;

}
}
return 0;
}




 Console Output of this problem:







Problem02: Write C++ code to implement a stack and its operations.

 

Solved of this problem :

#include <iostream>

using namespace std;

int stack[100], k=100, high=-1;

void push(int value) {

   if(high>=k-1)

   cout<<"Stack Overflow"<<endl;

   else {

      high++;

      stack[high]=value;

   }

}

void pop() {

   if(high<=-1)

   cout<<"Stack Underflow"<<endl;

   else {

      cout<<"The popped element is :"<<" "<< stack[high]<<endl;

      high--;

   }

}

void display() {

   if(high>=0) {

      cout<<"Your stack elements are:";

      for(int i=high; i>=0; i--)

      cout<<stack[i]<<" ";

      cout<<endl;

   } else

   cout<<"Your stack is empty"<<endl;

}

int main() {

   int userInput, value;

   cout<<"1) Push in stack"<<endl;

   cout<<"2) Pop from stack"<<endl;

   cout<<"3) Display stack"<<endl;

   cout<<"4) Exit"<<endl;

   do {

      cout<<"Enter your choice: "<<" ";

      cin>>userInput;

      switch(userInput) {

         case 1: {

            cout<<"Enter your value for pushed:"<<" ";

            cin>>value;

            push(value);

            break;

         }

         case 2: {

            pop();

            break;

         }

         case 3: {

            display();

            break;

         }

         case 4: {

            cout<<"Exit"<<endl;

            break;

         }

         default: {

            cout<<"Invalid Choice"<<endl;

         }

      }

   }while(userInput!=4);

   return 0;

}

Console output of this problem is:
 

No comments

Powered by Blogger.