codes on stack functions, Study notes of Computer science

c code for stack operations like push pop etc

Typology: Study notes

2024/2025

Uploaded on 04/20/2026

unknown user
unknown user 🇮🇳

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Stack Operations (Array Implementation)
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int st[MAX];
int top = -1;
void push(int x);
int pop();
int peek();
void display();
void main()
{
int flag = 0;
int c,p,q,r;
while (flag == 0)
{
printf("MENU\n");
printf("1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n");
printf("Enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter an element\n");
scanf("%d",&p);
push(p);
printf("Element inserted into stack\n");
break;
case 2:
q = pop();
printf("Element %d has been poped\n",q);
break;
case 3:
r = peek();
printf("Element at the top is %d",r);
break;
case 4:
printf("Current Stack: ");
display();
break;
case 5:
flag = -1;
break;
default:
printf("Please enter a valid choice");
}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download codes on stack functions and more Study notes Computer science in PDF only on Docsity!

1. Stack Operations (Array Implementation) #include #include #define MAX 100 int st[MAX]; int top = -1; void push(int x); int pop(); int peek(); void display(); void main() { int flag = 0; int c,p,q,r; while (flag == 0) { printf("MENU\n"); printf("1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n"); printf("Enter your choice\n"); scanf("%d",&c); switch(c) { case 1: printf("Enter an element\n"); scanf("%d",&p); push(p); printf("Element inserted into stack\n"); break; case 2: q = pop(); printf("Element %d has been poped\n",q); break; case 3: r = peek(); printf("Element at the top is %d",r); break; case 4: printf("Current Stack: "); display(); break; case 5: flag = -1; break; default: printf("Please enter a valid choice"); }

void push(int x) { if(top == (MAX-1)) { printf("Stack is full \n"); } else { top++; st[top] = x; } } int pop() { int temp; if(top == -1) { printf("Stack is empty \n"); return 0; } else { temp = st[top]; top--; return temp; } } int peek() { if(top == -1) { printf("No element to display \n"); return 0; } else { return st[top]; } } void display() { int i; for(i=0; i<=top; i++) { printf("%d ",st[i]);

else { top++; st[top] = x; } } int pop() { int temp; if(top == -1) { printf("Stack is empty\n"); return 0; } else { temp = st[top]; top--; return temp; } }

3. Stack Application (Parenthesis check) #include #include #include #define MAX 100 char st[MAX]; int top = -1; int flag = 0; void push(int x); int pop(); int checkbalanced(char s[30]); void main() { char s[30]; printf("Enter a string\n"); fgets(s, sizeof(s), stdin); flag = checkbalanced(s); if(flag == 0) { printf("Parenthesis are not balanced\n"); } else { printf("Parenthesis are balanced\n"); } } int checkbalanced(char s[30]) { int i,temp; int l = strlen(s); for(i=0; i

4. Stack Application (Infix to Postfix) #include #include #include #define MAX 100 char st[MAX]; int top = -1; void infixpostfix(char s[]); void push(char x); char pop(); int priority(char ch); void main() { char str[30]; printf("Enter an expression\n"); scanf("%s",str); infixpostfix(str); } void infixpostfix(char s[]) { char res[30]; char temp; int n = strlen(s); int i=0,j=0; while(i res[j] = pop(); j++; } temp = pop(); i++; } else if((s[i]>='A' && s[i]<='Z')||(s[i]>='a' && s[i]<='z')||(s[i]>='0' && s[i]<='9')) { res[j] = s[i]; i++; j++; } else if(s[i]=='+' || s[i]=='-' || s[i]=='' || s[i]=='/' || s[i]=='%') { while(((top!=-1) && (st[top]!='(')) && (priority(st[top])>=priority(s[i]))) { res[j]=pop(); j++; } push(s[i]); i++; } else { printf("Incorrect Expression\n"); exit(1); } } while((top!=-1) && (st[top]!='(')) { res[j]=pop(); j++; } res[j]='\0'; printf("Postfix Expression \n"); puts(res); } int priority(char ch) { if(ch=='%' || ch=='/' || ch=='') { return 2; } else if(ch=='+' || ch=='-') { return 1; } else {

5. Stack Application (Postfix Evaluation) #include #include #include #include #define MAX 100 char st[MAX]; int top = -1; void push(int x); int pop(); int posteval(char str[]); void main() { char s[30]; int p; printf("Enter postfix expression \n"); scanf("%s",s); p = posteval(s); printf("%d",p); } int posteval(char str[]) { int l,i; int op2,op1,val; l = strlen(str); for(i=0; i push(str[i]-'0'); } else { op2 = pop(); op1 = pop(); switch(str[i]) { case '+': val=op1+op2; break; case '-': val=op1-op2; break; case '': val=op1op2; break; case '/': val=op1/op2; break; case '%': val=op1%op2; break; } push(val); } } return pop(); } void push(int x) { if(top == (MAX-1)) { printf("Stack is full\n"); } else { top++; st[top] = x; } } int pop() { int temp; if(top == -1) { printf("Stack is empty\n"); return 0;

else if(t[k]==')') { t[k]='('; } } infixprefix(t); } void infixprefix(char s[]) { char res[30]; char pre[30]; char temp; int n = strlen(s); int i=0,j=0,l1,y=0; while(i='A' && s[i]<='Z')||(s[i]>='a' && s[i]<='z')||(s[i]>='0' && s[i]<='9')) { res[j] = s[i]; i++; j++; } else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='%') { while(((top!=-1) && (st[top]!='(')) && (priority(st[top])>=priority(s[i]))) { res[j]=pop(); j++; } push(s[i]); i++; } else

printf("Incorrect Expression\n"); exit(1); } } while((top!=-1) && (st[top]!='(')) { res[j]=pop(); j++; } res[j]='\0'; l1=strlen(res); for(i=l1-1;i>=0;i--) { pre[y] = res[i]; y++; } pre[y]='\0'; printf("Prefix expression \n"); puts(pre); } void reversed(char s1[]) { int l = strlen(s1); int j,i=0; for(j=l-1; j>=0;j--) { t[i]=s1[j]; i++; } } int priority(char ch) { if(ch=='%' || ch=='/' || ch=='*') { return 2; } else if(ch=='+' || ch=='-') { return 1; } else { return 0; } } void push(char x)

int preeval(char str[]) { int l,i; int op2,op1,val; l = strlen(str); for(i=l-1; i>=0; i--) { if(isdigit(str[i])) { push(str[i]-'0'); } else { op1 = pop(); op2 = pop(); switch(str[i]) { case '+': val=op1+op2; break; case '-': val=op1-op2; break; case '': val=op1op2; break; case '/': val=op1/op2; break; case '%': val=op1%op2; break; } push(val); } } return pop(); } void push(int x) { if(top == (MAX-1)) { printf("Stack is full\n"); } else { top++; st[top] = x;

int pop() { int temp; if(top == -1) { printf("Stack is empty\n"); return 0; } else { temp = st[top]; top--; return temp; } }

8. Queue Operations (Array Implementation) #include #define MAX 100 int queue[MAX]; int front=-1, rear=-1; void insert(int x); int del(); int peek(); void display(); int main() { int ch,val; do { printf("MENU\n");

int val; if(front==-1 || front>rear) { printf("Queue is empty\n"); return -1; } else { val = queue[front]; front++; if(front>rear) { front=rear=-1; } return val; } } int peek() { if(front==-1 || front>rear) { printf("Queue is empty\n"); return -1; } else { return queue[front]; } } void display() { int i; if(front==-1 || front>rear) { printf("Queue is empty\n"); } else { for(i=front; i<=rear; i++) { printf("%d ",queue[i]); } } printf("\n"); }