0% found this document useful (0 votes)
7 views1 page

Stack

This document contains a C program that implements a stack data structure with basic operations such as push, pop, and display. The stack has a fixed size of 5 and provides feedback when attempting to push to a full stack or pop from an empty stack. The main function demonstrates the usage of these operations by pushing and popping values and displaying the stack's contents.

Uploaded by

ahnafatif87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Stack

This document contains a C program that implements a stack data structure with basic operations such as push, pop, and display. The stack has a fixed size of 5 and provides feedback when attempting to push to a full stack or pop from an empty stack. The main function demonstrates the usage of these operations by pushing and popping values and displaying the stack's contents.

Uploaded by

ahnafatif87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h>
#define SIZE 5

int arr[SIZE];
int top=-1;

void push(int n){


if(top==SIZE-1){
printf("Stack is full");
}
else{
top=top+1;
arr[top]=n;
printf(" Pushed a value into the stack: %d\n ", n);
}

void pop(){
if(top==-1){
printf("Stack is empty");
}
else{

int x = arr[top];
top=top-1;
printf("Popped from the Stack: %d \n", x);
}
}
void display(){
for(int i=top;i>=0;i--){
printf("%d ", arr[i]);
}
}

int main()
{
push(10);
push(20);
push(30);
push(40);
push(50);
display();
pop();
pop();
display();
return 0;
}

You might also like