[go: up one dir, main page]

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

Day 3

The document contains a C program that implements a stack data structure with operations such as push, pop, peek, and checks for stack overflow and underflow. It allows the user to interactively manage the stack through a menu-driven interface. The program includes error handling for various stack operations and dynamically allocates memory for the stack.

Uploaded by

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

Day 3

The document contains a C program that implements a stack data structure with operations such as push, pop, peek, and checks for stack overflow and underflow. It allows the user to interactively manage the stack through a menu-driven interface. The program includes error handling for various stack operations and dynamically allocates memory for the stack.

Uploaded by

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

#include <stdio.h> } printf("Stack overflow.

Cant
#include <stdlib.h> int pop(int *stack,int *top) push.\n");
void push(char *stack,int *top,char { break;
c,int k) if(*top==-1) return 0; case 2: if(pop(stack,&top))
{ else {
if(*top==k-1) return; { printf("Element popped
else stack[(*top)++]=c; (*top)--; successfully");
} return 1; print(stack,&top);
char pop(char *stack,int *top) } }
{ } else
if(*top==-1) return '\0'; void peek(int *stack,int *top) printf("Stack underflow.
else return stack[--(*top)]; { Cant pop.\n");
} if(*top==-1) break;
int main() printf("Stack underflow. Cant case 3: peek(stack,&top);
{ peek."); break;
int k,n=100,top=-1,c=1; else{ case 4: if(full(top,n))
k=n; printf("Element peeked printf("Stack is full");
char successfully\n"); else printf("Stack is not
*s=(char*)malloc((n+1)*sizeof(char)); printf("%d",stack[*top]); full");
printf("Enter string : "); } break;
fgets(s,n+1,stdin); } case 5: if(emp(top))
char int full(int top,int n) printf("Stack is not empty");
*stack=(char*)malloc(k*sizeof(char)); { else printf("Stack is
for(int i=0;s[i]!='\0';i++) if(top==n-1) empty");
{ return 1; break;
if(s[i]=='['||s[i]=='{'||s[i]=='(') else return 0; }
push(stack,&top,s[i],k); } }while(q!=6);
else if(s[i]=='}'||s[i]==')'||s[i]==']') int emp(int top) free(stack);
{ { return 0;
char x=pop(stack,&top); if(top==-1) return 0; }
if(s[i]==']'&&x!='['||s[i]==')'&&x! else return 1;
='('||s[i]=='}'&&x!='{') }
{ int main()
c=0; {
break; int *stack,k,top=-1,n,q;
} printf("Enter the stack size : ");
} scanf("%d",&n);
} stack=(int*)malloc(n*sizeof(int));
if(top==-1&&c==1) do
printf("Successful"); {
else printf("Not successful"); printf("\nEnter 1 to push
return 0; element.\n");
} printf("Enter 2 to pop element.\
#include <stdio.h> n");
#include <stdlib.h> printf("Enter 3 to peek element.\
void print(int *stack,int *top) n");
{ printf("Enter 4 to check if the stack
printf("\n"); is full.\n");
for(int i=0;i<=*top;i++) printf("Enter 5 to check if the stack
printf("%d ",stack[i]); is empty.\n");
printf("\n"); printf("Enter 6 to exit\n");
} printf("Enter your choice : ");
int push(int *stack,int *top,int k) scanf("%d",&q);
{ switch(q)
if(*top==k-1) return 0; {
else case 1 : if(push(stack,&top,n))
{ {
printf("Enter element : "); printf("Element pushed
int x; successfully");
scanf("%d",&x); print(stack,&top);
stack[++(*top)]=x; }
return 1; else
}

You might also like