Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Program no. Aim :- To check whether the entered string is a keyword or not #include<stdio.h> #include<conio.h> #include<string.

h> void main() { char c[5][5]={"auto","if","else","int","float"}; char s[5]; int a,i,j=0; //clrscr(); printf("enter a string"); gets(s); for(i=0;i<5;i++) { a=strcmp(s,c[i]); if(a==0) { j=1; } } if(j==1) { printf("it is a keyword"); } else { printf("it is not a keyword"); } getch(); }

OUTPUT enter a stringauto it is a keyword

Program no. Aim:- To take an infix expression and convert it into postfix #include<stdio.h> #include<conio.h> void infix_to_postfix(char inf[]); char pop(); void push(char); char stack[50]; int top=-1; void main() { char inf[35]; printf("enter a string"); gets(inf); infix_to_postfix(inf); getch(); } void push(char symbol) { if(top>=49) { printf("stack overflow"); getch(); return; } else { top=top+1; stack[top]=symbol; } } char pop() { char item; if(top==-1) { printf("stack empty"); getch(); return(0); } else { item=stack[top]; top--; } return(item);

} int preced(char ch) { if(ch==47) { return(5); } else if(ch==42) { return(4); } else if(ch==43) { return(3); } else { return(2); } } void infix_to_postfix(char inf[]) { int length; static int index=0,pos=0; char symbol,temp; char postfix[25]; length=strlen(inf); push('#'); while(index<length) { symbol=inf[index]; switch(symbol) { case'(' : push(symbol); break; case')' : temp=pop(); while(temp!='(') { postfix[pos]=temp; pos++; temp=pop(); } break; case '+': case'-': case '*':

case '/': case '^': while(preced(stack[top])>=preced(symbol)) { temp=pop(); postfix[pos]=temp; pos++; } push(symbol); break; default:postfix[pos++]=symbol; break; } //switch close index++; } //while close while(top>0) { temp=pop(); postfix[pos++]=temp; } postfix[pos++]='\0'; puts(postfix); return; }

OUTPUT enter a stringA+B*C ABC*+

Program no. Aim:- To implement stack using linked list #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node *link; }; struct node *top=NULL,*temp; void main() { int choice,data; clrscr(); while(1)//infinite loop is used to insert/delete infinite number of nodes { printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n"); printf("\nEnter ur choice:"); scanf("%d",&choice); switch(choice) { case 1: temp=(struct node *)malloc(sizeof(struct node)); printf("Enter a node data :"); scanf("%d",&data); temp->data=data; temp->link=top; top=temp; break; case 2: if(top!=NULL) { printf("The poped element is %d",top->data); top=top->link; } else { printf("\nStack Underflow"); } break; case 3:

temp=top; if(temp==NULL) { printf("\nStack is empty\n"); } while(temp!=NULL) { printf("->%d->",temp->data); temp=temp->link; } break; case 4: exit(0); } } }

OUTPUT 1.Push 2.Pop 3.Display 4.Exit Enter ur choice:1 Enter a node data :45 1.Push 2.Pop 3.Display 4.Exit Enter ur choice:1 Enter a node data :67 1.Push 2.Pop 3.Display 4.Exit Enter ur choice:1 Enter a node data :89 1.Push

2.Pop 3.Display 4.Exit Enter ur choice:2 The poped element is 89 1.Push 2.Pop 3.Display 4.Exit Enter ur choice:3 ->67->->45-> 1.Push 2.Pop 3.Display 4.Exit Enter ur choice:4

Program no.

Aim:-Static implementation of stack #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<process.h> void push(); void pop(); void display(); int top=-1,a[20],item; void push() { printf("enter the data in stack"); scanf("%d",&item); if(top==20) { printf("overflow"); return ; } else { top=top+1; a[top]=item; } } void display() { int i; printf("------------stack is----------"); for(i=top;i>=0;i--) { printf("\n%d",a[i]); } } void pop() { if (top==-1) { printf("underflow"); return; } else { item=a[top]; top=top-1; } }

void main() { int n; clrscr(); printf("1:-push"); printf("\n2:-pop"); printf("\n3:-display"); printf("\n4:-exit"); do { printf("\nenter your choice"); scanf("%d",&n); switch(n) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); break; } }while(n!=4); //getch(); }

OUTPUT 1:-push 2:-pop 3:-display 4:-exit enter your choice1 enter the data in stack12 enter your choice1 enter the data in stack23 enter your choice1 enter the data in stack34 enter your choice1 enter the data in stack56 enter your choice2

enter your choice3 ------------stack is---------34 23 12 enter your choice4

You might also like