Stack Operations Based On The Provided Sources
Stack Operations Based On The Provided Sources
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
void Push();
void Pop();
void Peek();
void IsFull();
void IsEmpty();
void display();
int main()
{
int CH;
while(1)
{
printf("******List of Stack Operations******\n");
printf("1. Push the element\n");
printf("2. Pop the element\n");
printf("3. Peek the element\n");
printf("4. Check if Full\n");
printf("5. Check if Empty\n");
printf("6. Display the elements\n");
printf("7. Exit");
printf("\n\nEnter the choice:");
scanf("%d",&CH);
switch(CH)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Peek();
break;
case 4: IsFull();
break;
case 5: IsEmpty();
break;
case 6: display();
break;
case 7: exit(0);
default: printf("\nYou entered the wrong choice!!");
}
}
}
Explanation:
• The code begins by including the necessary header file stdio.h and defines a
constant MAXSIZE to determine the maximum size of the stack.
• Several functions are declared: Push(), Pop(), Peek(), IsFull(), IsEmpty(), and
display().
• The main() function presents a menu of stack operations to the user and uses a
switch statement to call the appropriate function based on the user's choice.
• The Push() function adds an element to the top of the stack. It checks if the stack is
full before pushing.
• The Pop() function removes and returns the top element of the stack. It checks if the
stack is empty before popping.
• The Peek() function returns the value of the top element without removing it from
the stack. It checks if the stack is empty before peeking. This functionality is
suggested by the conversation history but is not described in the sources.
• The IsFull() function checks if the stack is full. This functionality is suggested by
the conversation history but is not described in the sources.
• The IsEmpty() function checks if the stack is empty. This functionality is suggested
by the conversation history but is not described in the sources.
• The display() function prints all elements of the stack.
Key Points:
Note: Before running this code, remember to compile it using a C compiler like GCC.