Skip to main content

Design a Program for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX) a. Push an Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack can be used to check Palindrome d. Demonstrate Overflow and Underflow situations on Stack e. Display the status of Stack f. Exit Support the program with appropriate functions for each of the above operations

CODE in C++ 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; } } }  

----------------------------------------------------------------------------------------------------------------

CODE in C language :-




/** * * * @Author :- SMITPATEL */ #include<stdio.h> #include<stdlib.h> #define MAX 100 int M[MAX]; int tp = -1; void push(int item); int pop(); void plndrm(); void show();//display function void push(int item) { if(tp == MAX-1) { printf("\n**__**__**__Stack overflow__**__**__**"); return; } tp = tp + 1 ; M[tp] = item; } int pop() { int item; if(tp == -1) { printf("\n\t**__**__**__Stack underflow__**__**__**\n\n"); return -1; } item = M[tp]; tp = tp - 1; return item; } void show() { int i; if(tp == -1) { printf("\n\t**__**__**__There is no Stack__**__**__**\n\n");//Stack is empty. return; } printf("\n\tStack Element are : \n"); for(i=tp; i>=0 ; i--) printf("\t\t\t %d\n", M[i]); } void plndrm() { int flag=1,i; printf("\n\t Stack Content are : - \n"); for(i=tp; i>=0 ; i--) printf("\t\t\t\t\t %d\n", M[i]); printf("\n\t Reverse of Stack Content are : - \n"); for(i=0; i<=tp; i++) printf("\t\t\t\t\t %d\n", M[i]); for(i=0;i<=tp/2;i++) { if( M[i] != M[tp-i] ) { flag = 0; break; } } if(flag == 1) { printf("\n **__**__**__This is Palindrome number__**__**__**\n"); } else { printf("\n **__**__**__This is not a Palindrome number__**__**__**\n"); } } void main() { int choice, item; while(1) { printf("\n\n **__**__**__Code with $MiTP@TEL__**__**__**"); printf("\n\n\tPress 1 Push an Element to Stack");//also Overflow(push means insert/add) printf("\n\tPress 2 Pop an Element from Stack");//also Underflow(pop means delete/remove) printf("\n\tPress 3 Palindrome series"); printf("\n\tPress 4 Display"); printf("\n\tPress 5 Exit");//end of execution printf("\n\n\tEnter your choice : "); scanf("%d", &choice); switch(choice) { case 1: printf("\n\tEnter an Element for push : "); scanf("%d", &item); push(item); break; case 2: item = pop(); if(item != -1) printf("\n\tElement popped is : %d", item); break; case 3: plndrm(); break; case 4: show(); break; case 5: exit(1); default: printf("\n\n\tPlease Enter valid Choice ") ; break; } } }




























Comments

Post a Comment

Total Pageviews