1.Stack opertation:i).Assign to a variable name Y the value of the third element from the top of the stack and keep the stack undisturbed.ii) Given an arbitrary integer n pop out the top n elements. A message should be displayed if an unusual condition is encountered.iii) Assign to a variable name Y the value of the third element from the bottom of the stack and keep the stack undisturbed.//pro starts from here////Use two stacks#include<stdio.h>#include<stdlib.h>#define SIZE 10//stack structurestruct stack{int top;int a[SIZE];};//push: inserting element into stackvoid push(struct stack *m,int data){if(m->top== (SIZE-1)) //checking for empty condition{printf("full\n");}else //if stack is not empty{m->top++;m->a[m->top]=data;}}//pop: removing element from stack and it returns int valueint pop(struct stack *b){if(b->top==-1) //if stack is empty{printf("empty\n");}else // if stack contains elements{int data=b->a[b->top];b->top--;return data;}}int main(){struct stack s1,temp;s1.top=temp.top=-1;int z,i,j,x,y,n,t,c,data;int count;while(c!=3){printf("1.insert\t2.pop\t3.break\nenter ur choice\n");scanf("%d",&c);switch(c){case 1: printf("enter data\t");scanf("%d",&data);push(&s1,data);break;case 2: z=pop(&s1);printf("%d is popped out\n",z);break;case 3: break;}}if(s1.top<2){printf("stack insufficient data\n");}else{//3rd element from topfor(i=0;i<=2;i++){x=pop(&s1);push(&temp,x);}printf("\n\a3rd TOP is %d",x);// push(&s1,x);for(i=0;i<2;i++){x=pop(&temp);push(&s1,x);}while(s1.top>2) //3rd element from bottom{t=pop(&s1);push(&temp,t);}y=pop(&s1); \\y is element removed from stack s1push(&s1,y);printf("\n3rd BOTTOM is %d \n",y);while(temp.top!=-1){t=pop(&temp);push(&s1,t);}}count=s1.top+1;printf("enterno of ele deleted :\t");scanf("%d",&n);if(n>count){printf("Invalid ENTRY\n");exit(0);}printf("\n");for(i=0;i<n;i++){x=pop(&s1);printf("\t%d\n",x);}}//END of 1st program2. Write a program to determine if an input character string is of the form XxY. X is x string of arbitrary length using only the characters from A and B. For example, X may be ABBAB. Y is a string which is the reverse of X. Thus for the string X given above, Y is BABBA. A is any arbitrary character which is not A or B. Given ABAAxABAB the program should display a message that this string is invalid. For the string ABBAxABBA the program should write a message that it is valid. Use appropriate data structure.//program starts here#include<stdio.h>#include<stdlib.h>#include<string.h>#define SIZE 10struct stack{int top;char a[SIZE];};void push(struct stack *m,char data){if(m->top==(SIZE-1)){printf("full\n");}else{m->top++;m->a[m->top]=data;}}char pop(struct stack *b){if(b->top==-1)printf("empty\n");else{char data=b->a[b->top];b->top--;return data;}}int main(){struct stack s1;s1.top=-1;int i,j,k,n,len,flag=-1;char a[SIZE],ch;printf("enter string \n");scanf("%s",a);len=strlen(a);printf("length of the strinbg is %d\n",len);if(len%2==0){printf(" FAIL\n");exit(0);}for(i=0;a[i]!='\0';i++){if(a[i]=='A' ||a[i]=='B'){push(&s1,a[i]);}else{if(a[i]=='x'){i++;break;}else{printf("FAIL AT X \n");exit(0);}}}while(a[i]!='\0'){ch=pop(&s1);if(a[i]!=ch){printf("FAIL AT SEC FORM\n");exit(0);}else{i++;}}printf("\tCORRECT\n");}//END OF 2nd PROGRAM3. Write a C program that parses Infix arithmetic expressions to Postfix arithmetic expressions using a Stack.//program starts here#include<stdio.h>#include<stdlib.h>#include<string.h>#define SIZE 20int priority(char symbol){switch(symbol){case '*':case '/':return 2;case '+':case '-':return 1;case '(':return 0;}}struct stack{int top;char a[SIZE];};void push(struct stack *s,char data){if(s->top==(SIZE-1))printf("full\n");else{s->top++;s->a[s->top]=data;}}char pop(struct stack *s){char ele;if(s->top==-1){printf("empty\n");exit(0);}else{ele=s->a[s->top];s->top--;return ele;}}void inf(char infix[],char postfix []){struct stack s;int top;int i,j,n,c1,c2;s.top=-1;char symbol,z;push(&s,'#');j=0;c1=-1;c2=0;for(i=0;i<strlen(infix);i++){if(infix[i]== '('){c1++;break;}}for(i=0;i<strlen(infix);i++){symbol=infix[i];if(isalnum(symbol)){postfix[j++]=symbol;}else if(symbol=='('){push(&s,symbol);if(c1!=-1)c2++;}else if(symbol==')'){if(c1!=-1)c2--;while(s.a[s.top]!='(')postfix[j++]=pop(&s);}else{while(priority(s.a[s.top]) >= priority(symbol))postfix[j++]=pop(&s);push(&s,symbol);}}while(s.a[s.top]!='#'){if(s.a[s.top]!='(')postfix[j++]=pop(&s);elsez=pop(&s);}postfix[j]='\0';if(c1!=-1&&c2!=0){printf("invalid expression\n");exit(0);}}void main (){int y,i;char infix[20],postfix[20];printf("enter exp\n");scanf("%s",infix);inf(infix,postfix);printf("\n postfix exp is :\t%s\n\n",postfix);}//END OF 3rd PROGRAM
stack
Subscribe to:
Posts (Atom)
No comments:
Post a Comment