0% found this document useful (0 votes)
11 views

Stack Operations Based On The Provided Sources

C keys as words
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Stack Operations Based On The Provided Sources

C keys as words
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Here is sample C code for stack operations based on the provided sources:

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5

int Top = -1, arr[MAXSIZE];

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!!");
}
}
}

void Push() //Specifying the Push Operation.


{
int a;
if(Top==MAXSIZE-1)
{
printf("\nStack is Overflow!!");
}
else
{
printf("\nEnter element that you want to insert in the stack: ");
scanf("%d",&a);
Top=Top+1;
arr[Top]=a;
}
}

void Pop() //Specifying the Pop Operation.


{
if(Top==-1)
{
printf("\nStack is Underflow!!");
}
else
{
printf("\nPop element from the Stack: %d \n",arr[Top]);
Top=Top-1;
}
}

void Peek() //Specifying the Peek Operation.


{
if(Top==-1)
{
printf("\nStack is Underflow!!");
}
else
{
printf("\nPeek element from the Stack: %d\n", arr[Top]);
}
}

void IsFull() //Specifying the IsFull Operation.


{
if(Top == MAXSIZE -1)
{
printf("\nStack is Full!!");
}
else
{
printf("\nStack is not Full!!");
}
}

void IsEmpty() //Specifying the IsEmpty Operation.


{
if(Top == -1)
{
printf("\nStack is Empty!!");
}
else
{
printf("\nStack is not Empty!!");
}
}

void display() //Specifying the Show Operation.


{
if(Top==-1)
{
printf("\nStack is Underflow!!");
}
else
{
printf("\nThe Elements of the stack are: ");
for(int x=Top;x>=0;--x)
printf("%d\n",arr[x]);
}
}

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:

• The stack is implemented using an array.


• The variable Top keeps track of the top element of the stack.
• The stack follows the Last-In, First-Out (LIFO) principle.

Note: Before running this code, remember to compile it using a C compiler like GCC.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy