A stack can be implemented by means of Array, Structure, Pointer and Linked-List. Stack can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack using arrays which makes it a fixed size stack implementation.
Basic Operations:
push() – pushing (storing) an element on the stack.
pop() – removing (accessing) an element from the stack. To use a stack efficiently we need to check status of stack as well. For the same purpose, the following functionality is added to stacks;
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
ALGORITHM:
Step 1: Start.
Step 2: Initialize stack size MAX and top of stack -1.
Step 3: Push integer element on to stack and display the contents of the stack. if stack is full give a message as ‘Stack is Overflow’.
Step 4: Pop element from stack along with display the stack contents. if stack is empty give a message as ‘Stack is Underflow’.
Step 5: Check whether the stack contents are Palindrome or not.
Step 6: Stop.
#include
#include
#define MAX 4
int stack[MAX], item;
int ch, top = -1, count = 0, status = 0;
/*PUSH FUNCTION*/
void push(int stack[], int item)
{
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
/*POP FUNCTION*/
int pop(int stack[])
{
int ret;
if(top == -1)
printf("\n\nStack is Underflow");
else
{
ret = stack[top--];
status--;
printf("\nPopped element is %d", ret);
}
return ret;
}
/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */
void palindrome(int stack[])
{
int i, temp;
temp = status;
for(i=0; i=0; i--)
printf("\n ------\n| %d |", stack[i]);
printf("\n");
}
}
/*MAIN PROGRAM*/
void main()
{
clrscr();
do
{
printf("\n\n----MAIN MENU----\n");
printf("\n1. PUSH (Insert) in the Stack");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter a element to be pushed: ");
scanf("%d", &item);
push(stack, item);
display(stack);
break;
case 2: item=pop(stack);
display(stack);
break;
case 3: palindrome(stack);
break;
case 4: exit(0);
break;
default: printf("\nEND OF EXECUTION");
}//end switch
}while (ch != 4);
getch();
}
SAMPLE OUTPUT: ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 1 The stack contents are: ----- | 1 | ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 2 The stack contents are: ------ | 2 | ------ | 1 | (-------- AFTER THE 4 TIMES PUSH OPERATION -------) ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 9 Stack is Overflow The stack contents are: ------ | 1 | ------ | 2 | ------ | 2 | ------ | 1 | ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 2 Popped element is 1 The stack contents are: ------ | 2 | ------ | 2 | ------ | 1 | (-------- AFTER THE 4 TIMES POP OPERATION -------) ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 2 Stack is Underflow The stack contents are: Stack is Empty (-------- CHECKING FOR PALINDROME OR NOT USING STACK-------) ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 1 The stack contents are: ----- | 1 | ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 2 The stack contents are: ------ | 2 | ------ | 1 | ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter a element to be pushed: 1 The stack contents are: ------ | 1 | ------ | 2 | ------ | 1 | ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 3 Stack contents are is Palindrome (-------- CHECKING FOR PALINDROME OR NOT USING STACK-------) ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 1 The stack contents are: ----- | 1 | (AFTER 3 TIMES PUSH) ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 1 Enter an element to be pushed: 3 The stack contents are: ------ | 3 | ------ | 2 | ------ | 1 | ----MAIN MENU---- 1. PUSH (Insert) in the Stack 2. POP (Delete) from the Stack 3. PALINDROME check using Stack 4. Exit (End the Execution) Enter Your Choice: 3 Stack contents are not Palindrome