Design, Develop and Implement a menu driven Program in C for the following Array operations a. Creating an Array of N Integer Elements b. Display of Array Elements with Suitable Headings c. Inserting an Element (ELEM) at a given valid Position (POS) d. Deleting an Element at a given valid Position(POS) e. Exit. Support the program with functions for each of the above operations.
CODE IN Cpp Language :-
/**
*
*
* @Author :- SMITPATEL
*/
#include"cstdlib"
#include"iostream"
#include"stack"
using namespace std;
const int MAX = 100;
int tp = -1;
int M[MAX];
void push(int item);
int pop();
void palindrome();
void show();//display function
void push(int item)
{
if( tp == MAX - 1)
{
cout << "**__**__**__Stack Overflow__**__**__**" << endl;
return ;
}
tp = tp + 1;
M[tp] = item;
}
int pop()
{
int item;
if( tp == -1)
{
cout << "**__**__**__Stack Underflow__**__**__**" << endl;
return -1;
}
item = M[tp];
tp = tp-1;
return item;
}
void show()
{
int i;
if(tp == -1)
{
cout << "**__**__**__There is no Stack__**__**__**" << endl << endl;//Stack is empty.
return;
}
cout << endl <<"\tStack Element are : "<< endl;
for( i=tp; i>=0; i--)
{
cout << "\t\t\t "<< M[i] << endl;
}
}
void palindrome()
{
int flag = 1, i;
cout << endl << "\t Stack Content are : "<<endl;
for(i=tp; i>=0; i--)
cout << "\t\t\t\t\t "<< M[i] << endl;
cout << endl <<"\t Reverse of Stack Content are : ";
for(i=0; i<=tp; i++)
cout <<endl<< "\t\t\t\t\t " << M[i] ;
for(i=0; i<=tp/2; i++)
{
if(M[i] != M[tp-i] )
{
flag = 0;
break;
}
}
if(flag == 1 )
{
cout << endl << endl <<" **__**__**__This is Palindrome number__**__**__**" << endl;
}
else
{
cout << endl << endl <<" **__**__**__This is not a Palindrome number__**__**__**" << endl;
}
}
int main()
{
int choice, item;
while(true)
{
cout << endl << endl<< "**__**__**__Code with $MiTP@TEL__**__**__**" << endl << endl;
cout << "\tPress 1 Push an Element to Stack" << endl;//also Overflow(push means insert/add)
cout << "\tPress 2 Pop an Element from Stack" << endl;//also Underflow(pop means delete/remove)
cout << "\tPress 3 Palindrome series" << endl;
cout << "\tPress 4 Display" << endl;
cout << "\tPress 5 Exit" << endl << endl;
cout << "\tEnter your choice : " ;
cin >> choice;
switch(choice)
{
case 1:
cout << endl << "\tEnter an Element for push : ";
cin >> item;
push(item);
break;
case 2:
item = pop();
if(item != -1)
cout << endl <<"\tElement popped is : " << item << endl;
break;
case 3:
palindrome();
break;
case 4:
show();
break;
case 5:
exit(1);
default:
cout << "\tPlease Enter valid Choice " << endl;
}
}
}
Comments
Post a Comment