Common questions :MCSL 209

IT & Software

| views

1. Stack Implementation Using Two Queues

#include #define N 50 int q1[N], q2[N], f1=0,r1=-1,f2=0,r2=-1; void push(int x){ q2[++r2]=x; while(f1<=r1) q2[++r2]=q1[f1++]; f1=0; r1=-1; for(int i=f2;i<=r2;i++) q1[++r1]=q2[i]; f2=0; r2=-1; } void pop(){ if(r1<0) printf("Stack Empty\n"); else printf("Popped: %d\n", q1[f1++]); } int main(){ push(10); push(20); push(30); pop(); pop(); }

2. Binary Tree Traversals (Inorder, Preorder, Postorder)

#include #include struct node{ int data; struct node *l,*r; }; struct node* new(int x){ struct node* n=malloc(sizeof(*n)); n->data=x; n->l=n->r=NULL; return n; } void inorder(struct node* t){ if(t){ inorder(t->l); printf("%d ",t->data); inorder(t->r); } } void preorder(struct node* t){ if(t){ printf("%d ",t->data); preorder(t->l); preorder(t->r); } } void postorder(struct node* t){ if(t){ postorder(t->l); postorder(t->r); printf("%d ",t->data); } } int main(){ struct node* root=new(1); root->l=new(2); root->r=new(3); inorder(root); printf("\n"); preorder(root); printf("\n"); postorder(root); }

3. Prefix to Postfix Conversion

#include #include char stack[50][50]; int top=-1; void push(char *s){ strcpy(stack[++top],s); } char* pop(){ return stack[top--]; } int isOp(char c){ return c=='+'||c=='-'||c=='*'||c=='/'; } int main(){ char pre[50],op1[50],op2[50],res[50]; scanf("%s",pre); for(int i=strlen(pre)-1;i>=0;i--){ if(isOp(pre[i])){ strcpy(op1,pop()); strcpy(op2,pop()); sprintf(res,"%s%s%c",op1,op2,pre[i]); push(res); }else{ res[0]=pre[i]; res[1]='\0'; push(res); } } printf("%s",stack[top]); }

4. Singly Linked List Sort

#include #include struct node{ int data; struct node* next; }; struct node* add(struct node* h,int x){ struct node* n=malloc(sizeof(*n)); n->data=x; n->next=h; return n; } void sort(struct node* h){ for(;h;h=h->next) for(struct node* t=h->next;t;t=t->next) if(h->data>t->data){ int x=h->data; h->data=t->data; t->data=x; } } void display(struct node* h){ while(h){ printf("%d ",h->data); h=h->next; } } int main(){ struct node* h=NULL; int x; while(scanf("%d",&x),x!=-1) h=add(h,x); sort(h); display(h); }

5. Elements NOT Common in Two Linked Lists

#include #include struct node{ int data; struct node* next; }; struct node* add(struct node* h,int x){ struct node* n=malloc(sizeof(*n)); n->data=x; n->next=h; return n; } int found(struct node* h,int x){ while(h){ if(h->data==x) return 1; h=h->next; } return 0; } int main(){ struct node *A=NULL,*B=NULL; int x; while(scanf("%d",&x),x!=-1) A=add(A,x); while(scanf("%d",&x),x!=-1) B=add(B,x); printf("Not common: "); for(struct node* p=A;p;p=p->next) if(!found(B,p->data)) printf("%d ",p->data); }

6. Reverse Input String

#include #include int main(){ char s[100]; gets(s); for(int i=strlen(s)-1;i>=0;i--) printf("%c",s[i]); }

7. Concatenate Two Strings

#include #include int main(){ char a[100],b[50]; gets(a); gets(b); strcat(a,b); printf("%s",a); }

Share this Post
About the Author

✍️ Satyendra Singh is a dedicated software educator and creator behind Quizer.in. With a passion for coding, learning, and teaching, he simplifies complex programming topics and builds engaging tools that make learning fun for everyone.

Comments

No comments yet — be the first to comment! 💬
Leave a Comment

Your email address will not be published. Required fields are marked *

Popular Competitive Exam Quizzes