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

Stack

The document contains a C program that implements a stack data structure with functions for checking if the stack is empty or full, pushing and popping elements, and printing the stack contents. It defines a maximum size for the stack and handles overflow and underflow conditions. The main function demonstrates the usage of the stack by pushing three integers, printing the stack, popping an element, and printing the stack again.

Uploaded by

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

Stack

The document contains a C program that implements a stack data structure with functions for checking if the stack is empty or full, pushing and popping elements, and printing the stack contents. It defines a maximum size for the stack and handles overflow and underflow conditions. The main function demonstrates the usage of the stack by pushing three integers, printing the stack, popping an element, and printing the stack again.

Uploaded by

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

#include<stdio.

h>
#define MAXSIZE 1000
struct stack{
int elements[MAXSIZE];
int top;
};
int IsStackempty(struct stack *S){
return S->top == -1;
}
int IsStackfull(struct stack *S){
return S->top == MAXSIZE-1;
}
void push(struct stack *S, int item){
if(IsStackfull(S)){
printf("stack overflow! \n");
}
S->top++;
S->elements[S->top]=item;
}
int pop(struct stack *S){
if(IsStackempty(S)){
printf("Stack underflow! \n");
}
int item = S-> elements[S->top];
S->top--;
return item;
}
void printstack(struct stack *S){
if(IsStackempty(S)){
printf("Stack is empty.\n");
}
else{
printf("stack contents(top to bottom):\n");
for(int i=S->top; i>=0; i--){
printf("%d\n", S->elements[i]);
}
}
}
int main(){
struct stack source;
source.top= -1;
IsStackfull(&source);
push(&source, 10);
push(&source, 20);
push(&source, 30);
printf("print stack before pop:\n");
printstack(&source);
IsStackempty(&source);
pop(&source);
printf("print stack:\n");
printstack(&source);
}

You might also like