Sie sind auf Seite 1von 52

INDEX S.No 1. 2. 3. 4. 5. 6. 7. Programs WAP to insert an element at specific position in an array. WAP to delete a specific element from an array.

WAP to perform addition of two matrices. WAP to perform multiplication of two matrices. WAP to perform stack operations using static implementation. WAP to perform stack operations using dynamic implementation. WAP to perform conversion between different notations (Prefix, Postfix, Infix) and evaluation of postfix and prefix expression. Page No.

8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.

WAP to perform queue operations using static implementation. WAP to perform queue operations using dynamic implementation. WAP to implement circular queue. WAP to perform operations on singly linked list. WAP to perform operations on doubly linked list. WAP to perform operations on circular linked list. WAP to implement Binary Search Tree. WAP to implement Linear Search. WAP to implement Binary Search. WAP to perform Selection Sort. WAP to perform Insertion Sort. WAP to perform Bubble Sort. WAP to perform Quick Sort.

// WAP to insert an element at specific position in an array.

#include<stdio.h> #include<conio.h> void main() { int a[20],n,p,k,m; clrscr(); printf("\n enter the size for array"); scanf("%d",&n); printf("\n enter %d element for array...\n",n); for(k=0;k<n;k++) scanf("%d",&a[k]); printf("enter the element which we want to insert"); scanf("%d",&m); printf("\nenter the positon which we want to insert the element "); scanf("%d",&p); --p; for(k=n;k>=p;k--) { a[k+1]=a[k]; } a[p]=m; if(p>n) { printf("\n insertion outside the array"); } n++; printf("\nnew array.."); for(k=0;k<n;k++) { printf("\n%d",a[k]); } getch(); }

// WAP to delete a specific element from an array. #include<stdio.h>

#include<conio.h> void main() { int a[20],n,p,k,m; clrscr(); printf("\n enter the size for array"); scanf("%d",&n); printf("\n enter %d element for array...\n",n); for(k=1;k<=n;k++) scanf("%d",&a[k]); printf("\nenter the positon which we want to delete the element "); scanf("%d",&p); for(k=p;k<n;k++) { a[k]=a[k+1]; } n--; printf("\nnew array.."); for(k=1;k<=n;k++) { printf("\n%d",a[k]); } getch(); }

// WAP to perform addition of two matrices. #include<stdio.h> #include<conio.h>

void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; clrscr(); printf("\nenter the order the matrix a"); scanf("%d%d",&m,&n); printf("\n enter the elements of matrix a"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { printf("\t%d",a[i][j]); } } printf("\nenter the order the matrix b"); scanf("%d%d",&p,&q); printf("\n enter the elements of matrix b"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<p;i++) { printf("\n"); for(j=0;j<q;j++) { printf("\t%d",b[i][j]); } } if(m==p&&n==q) { printf("\nmatrix c the sum of matrix a & b"); for(i=0;i<m;i++) { for(j=0;j<=n;j++) { c[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { printf("\t%d",c[i][j]); } } } else

{ printf("\nThe addition of matrix a&b is not posible."); } getch(); }

// WAP to perform multiplication of two matrices. #include<stdio.h> #include<conio.h> void main() {

int a[10][10],b[10][10],s[10][10],m,n,p,q,i,j,k; clrscr(); printf("enter the order of matrix A "); scanf("%d%d",&m,&n); printf("\n enter the %d elements of matrix a",n*m); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { printf("\t%d",a[i][j]); } } printf("\nenter the order of matrix B "); scanf("%d%d",&p,&q); printf("\n enter the %d elements of matrix b",p*q); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<p;i++) { printf("\n"); for(j=0;j<q;j++) { printf("\t%d",b[i][j]); } } if(n!=p) { printf("the mutiplication is not possible"); } else { for(i=0;i<m;i++) { for(j=0;j<q;j++) { s[i][j]=0; for(k=0;k<n;k++) { s[i][j]=s[i][j]+a[i][k]*b[k][j]; } } } printf("\nmatrix mutiplication of matrix A & matrix B is"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<q;j++)

{ printf("\t%d",s[i][j]); } } } getch(); }

// WAP to perform stack operations using static implementation. #include<stdio.h> #include<conio.h> #include<stdlib.h> # define len 100 struct stack {

int i; char n; }s[100]; int top=-1; void push(); void pop(); void treverse(); void main() { int c; char ch='y'; while(ch!='n') { clrscr(); printf("\nenter the choice"); printf("\n1. push"); printf("\n2.pop"); printf("\n3.treverse"); printf("\n4.exit"); scanf("%d",&c); switch(c) { case 1: push(); break; case 2: pop(); break; case 3: treverse(); break; case 4: exit(0); default: printf("\nyou entered wrong choice"); } printf("\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void push() { stack p; if(top==100) printf("\nstack is over flow"); else { printf("enter the roll no. and a char which we want to insert"); scanf("%d %c",&p.i,&p.n); top=top+1; s[top].i=p.i; s[top].n=p.n; } } void pop() { if(top<0) printf("\n stack is under flow");

else { printf("\ndeleted item is %d",s[top].i); printf("\ndeleted item is %c",s[top].n); top--; } } void treverse() { int j; for(j=top;j>=0;j--) { printf("\nroll no=%d name=%c",s[j].i,s[j].n); } }

// WAP to perform stack operations using dynamic implementation. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct student { int no; char name[20]; student *next;

}*top=NULL; void push(); void pop(); void treverse(); main() { int c; char ch='y'; while(ch!='n') { printf("\nenter the choice"); printf("\n1. push"); printf("\n2.pop"); printf("\n3.treverse"); printf("\n4.exit"); scanf("%d",&c); switch(c) { case 1: push(); break; case 2: pop(); break; case 3: treverse(); break; case 4: exit(0); default: printf("\nyou entered wrong choice"); } printf("\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void push() { student *ptr; ptr=(student*)malloc(sizeof(student)); printf("enter the roll no. & name which we want to insert"); scanf("%d",&ptr->no); scanf("%s",&ptr->name); ptr->next=NULL; if(top==NULL) top=ptr; else { ptr->next=top; top=ptr; } } void pop() { student*ptr; if(top==NULL) printf("\n stack is under flow"); else

10

{ ptr=top; top=top->next; free(ptr); printf("\ndeleted item is %d",ptr->no); printf("\ndeleted item is %s",ptr->name); } } void treverse() { int j; student *ptr; for(j=1,ptr=top;ptr->next!=NULL;ptr=ptr->next,j++) { printf("\nroll no= %d",ptr->no); printf("\nname=%s",ptr->name); } printf("\nroll no= %d",ptr->no); printf("\nname=%s",ptr->name); }

/* WAP to perform conversion between different notations (Prefix, Postfix, Infix) and evaluation of postfix and prefix expression. */ #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void spush(char sym[]); char s_stack[50][50]; char spostfix[50][50],sprefix[50][50],spostfix[50][50]; char pprefix[50][50];

11

char prefix[50],postfix[50],stack[50],prefix1[50],ppostfix[50][50]; char infix[50],infix1[50]; char val[10]; int top=-1,ln=0; static int pos=-1,i=0; void ppop() { if(top==-1) printf("\n the stack is empty"); strcpy(val,s_stack[top--]); } void pre_in(char prefix[][50]) { char op1[50]="",op2[50]=""; char sym[50],ch; while(i<=ln-1) { strcpy(sym,prefix[--ln-1]); if(strcmp(sym,"^")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"^"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"$")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"$"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"*")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"*"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"/")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1);

12

strcat(sym,"/"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"+")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"+"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"-")==0) { ppop(); strcpy(op2,val); pop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"-"); strcat(sym,op2); spush(sym); } else spush(sym); } printf("\n the result is %s",strrev(s_stack[0])); } void post_in(char prefix[][50]) { char op1[50]="",op2[50]=""; char sym[50],ch; while(i<ln-1) { strcpy(sym,prefix[i]); i++; if(strcmp(sym,"^")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"^"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"$")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"$"); strcat(sym,op2);

13

spush(sym); } else if(strcmp(sym,"*")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"*"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"/")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"/"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"+")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"+"); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"-")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,"-"); strcat(sym,op2); spush(sym); } else spush(sym); } printf("\n the result is %s",s_stack[1]); } void pre_eval(char prefix[][50]) { int op1,op2,res=0; char sym[50],ch; while(i<=ln) { strcpy(sym,prefix[ln--]); if(strcmp(sym,"^")==0)

14

{ op2=spop(); op1=spop(); res=op2^op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"$")==0) { op2=spop(); op1=spop(); res=op2^op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"*")==0) { op2=spop(); op1=spop(); res=op2*op1; spush(itoa(res,sym,10)); break; } else if(strcmp(sym,"/")==0) { op2=spop(); op1=spop(); res=op2/op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"+")==0) { op2=spop(); op1=spop(); res=op2+op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"-")==0) { op2=spop(); op1=spop(); res=op2-op1; spush(itoa(res,sym,10)); } else spush(sym); } printf("\n the result is %d",res); } void post_eval(char spostfix[][50]) { int op1,op2,res=0; char sym[50],ch; while(i<ln) { strcpy(sym,spostfix[i]); i++; if(strcmp(sym,"^")==0) { op1=spop(); op2=spop();

15

res=op2^op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"$")==0) { op1=spop(); op2=spop(); res=op2^op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"*")==0) { op1=spop(); op2=spop(); res=op2*op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"/")==0) { op1=spop(); op2=spop(); res=op2/op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"+")==0) { op1=spop(); op2=spop(); res=op2+op1; spush(itoa(res,sym,10)); } else if(strcmp(sym,"-")==0) { op1=spop(); op2=spop(); res=op2-op1; spush(itoa(res,sym,10)); } else spush(sym); } printf("\n the result is %d",res); } void spush(char *sym) { if(top>=49) printf("\n stack is full"); strcpy(s_stack[++top],sym); } int spop() { int val; if(top==-1) printf("\n the stack is empty"); val=atoi(s_stack[top--]); return val; }

16

char pop() { if(top==-1) printf("\n stack is empty"); return (stack[top--]); } void push(char symbal) { if(top>=49) printf("\n the stack is full"); stack[++top]=symbal; } int preced(char symbal) { switch(symbal) { case '^': return 5; case '$': return 5; case '*': return 4; case '/': return 4; case '+': return 3; case '-': return 3; } return 0; } void intoprefix(char infix[]) { char ch; int len ,flag=0,c=-2; len=strlen(infix); while(i<len) { c++; ch=infix[i++]; switch(ch) { case ')': push(ch); flag++; break; case '(': while(stack[top]!=')') { prefix[++pos]=pop(); } top--; flag--; break; case '^': case '$':

17

case '*': case '/': case '+': case '-': if(top==-1) push(ch); else { while(preced(ch)<preced(stack[top])) { if(top==-1) break; else prefix[++pos]=pop(); } } if(c) push(ch); break; default: prefix[++pos]=ch; break; } } while(top>=0) { pos++; prefix[pos]=stack[top]; top--; } prefix[++pos]='\0'; strrev(prefix); puts(prefix); } void intopostfix(char infix[]) { char ch; int arr[10]; int len ,flag=0,c=-2; len=strlen(infix); while(i<len) { c++; ch=infix[i++]; switch(ch) { case '(': push(ch); flag++; break; case ')': while(stack[top]!='(') { postfix[++pos]=pop(); } top--; flag--; break; case '^':

18

case '$': case '*': case '/': case '+': case '-': if(top==-1) push(ch); else { while(preced(ch)<=preced(stack[top])) { if(top==-1) break; else postfix[++pos]=pop(); } } if(c) push(ch); break; default: postfix[++pos]=ch; break; } } while(top>=0) { pos++; postfix[pos]=stack[top]; top--; } postfix[++pos]='\0'; puts(postfix); } void post_pre(char spostfix[][50]) { char op1[50]="",op2[50]=""; char sym[50],ch; while(i<=ln-1) { strcpy(sym,spostfix[i]); i++; if(strcmp(sym,"^")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"$")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val);

19

strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"*")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"/")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"+")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"-")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else spush(sym); } printf("\n the result is %s",s_stack[1]); } void pre_post(char sprefix[][50]) { char op1[50]="",op2[50]=""; char sym[50],ch; while(i<=ln-1) { strcpy(sym,sprefix[--ln-1]); if(strcmp(sym,"^")==0) { ppop();

20

strcpy(op2,val); ppop(); strcpy(op1,val); strcpy(sym,op1); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"$")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"*")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"/")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"+")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else if(strcmp(sym,"-")==0) { ppop(); strcpy(op2,val); ppop(); strcpy(op1,val); strcat(sym,op1); strcat(sym,op2); spush(sym); } else spush(sym);

21

} printf("\n the result is %s",strrev(s_stack[0])); } void main() { int choice; char ch; do { top=-1; clrscr(); printf("\t\n enter 1 for infix to prefix\n"); printf("\t\n enter 2 for infix to postfix\n"); printf("\t\n enter 3 for postfix to prefix\n"); printf("\t\n enter 4 for postfix to infix\n"); printf("\t\n enter 5 for prefix to infix\n"); printf("\t\n enter 6 for prefix to postfix\n"); printf("\t\n enter 7 for prefix evlluation\n"); printf("\t\n enter 8 for postfix evalluation\n"); printf("\t\n enter 9 for exit\n"); printf("\t\n enter any one choice"); scanf("%d",&choice); switch(choice) { case 1 : printf("\n enter the expresasion in infix format\n"); fflush(stdin); gets(infix); pos=-1,i=0; top=-1; intoprefix(strrev(infix)); break; case 2: printf("\n enter the expresasion in infix format\n"); fflush(stdin); gets(infix); pos=-1,i=0; top=-1; intopostfix(infix); break; case 3: top=-1; printf("\nenter postfix expression"); do { gets(spostfix[ln++]); } while(strcmp(spostfix[ln-1],".")!=0); post_pre(spostfix); break; case 4: top=-1; i=0; printf("\nenter prefixfix expression"); do { gets(ppostfix[ln++]); } while(strcmp(ppostfix[ln-1],".")!=0);

22

post_in(ppostfix); break; case 5: top=-1; printf("\nenter prefixfix expression"); do { gets(pprefix[ln++]); } while(strcmp(pprefix[ln-1],".")!=0); pre_in(pprefix); break; case 6: top=-1; printf("\nenter prefix expression"); do { gets(sprefix[ln++]); } while(strcmp(sprefix[ln-1],".")!=0); pre_post(sprefix); break; case 7: top=-1; printf("\nenter prefixfix expression(numeric)"); do { gets(sprefix[ln++]); } while(strcmp(sprefix[ln-1],".")!=0); pre_eval(sprefix); break; case 8: top=-1; printf("\nenter postfix expression(numeric)"); do { gets(spostfix[ln++]); } while(strcmp(spostfix[ln-1],".")!=0); post_eval(spostfix); break; case 9: exit(1); } printf("\nenter any key to continue /n"); scanf("%c",&ch); } while(ch!='n'||ch!='N'); getch(); }

23

// WAP to perform queue operations using static implementation. #include<stdio.h> #include<conio.h> #include<stdlib.h> #define len 5 void qinsert(); void qdelete(); void qdisplay(); int item; int q[len],rear=-1,front=-1; void main() { int c; char ch='y'; while(ch!='n') { clrscr(); printf("\nenter the choice"); printf("\n1. insert"); printf("\n2.delete"); printf("\n3.display"); printf("\n4.exit"); scanf("%d",&c); switch(c) {

24

case 1: qinsert(); break; case 2: qdelete(); break; case 3: qdisplay(); break; case 4: exit(0); default: printf("\nyou entered wrong choice"); } printf("\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void qinsert() { if(rear>=len-1) { printf("\nqueue is full.."); } else { printf("\nenter the no which we want to insert.. "); scanf("%d",&item); rear++; if(front==-1) front=0; q[rear]=item; } } void qdelete() { if(front==-1) { printf("\n queue is empty...."); } else { item=q[front]; if(front==rear) { front=-1; rear=-1; } else front++; printf("deleted item is %d",item); } } void qdisplay() { int i; for(i=front;i<=rear;i++) {

25

printf("\nno=%d",q[i]); } }

// WAP to perform queue operations using dynamic implementation. #include<stdio.h> #include<conio.h> #include<stdlib.h> void qinsert(); void qdelete(); void qdisplay(); struct queue { int no; char name[20]; queue *next; }*rear=NULL,*front=NULL; void main() { int c; char ch='y'; while(ch!='n') { clrscr(); printf("\nenter the choice"); printf("\n1. insert"); printf("\n2.delete"); printf("\n3.display"); printf("\n4.exit"); scanf("%d",&c); switch(c)

26

{ case 1: qinsert(); break; case 2: qdelete(); break; case 3: qdisplay(); break; case 4: exit(0); default: printf("\nyou entered wrong choice"); } printf("\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void qinsert() { queue *ptr; ptr=(queue*)malloc(sizeof(queue)); ptr->next=NULL; printf("\nenter the roll no and name"); scanf("%d %s",&ptr->no,&ptr->name); if(front==rear==NULL) front=rear=ptr; else { rear->next=ptr; rear=ptr; } } void qdelete() { queue *ptr; if(front==NULL) printf("\nQueue is empty.."); else if(front==rear) { ptr=front; front=rear=NULL; printf("deleted item is \nroll no=%d \nname=%s",ptr->no,ptr->name); free(ptr); } else { ptr=front; front=front->next; printf("deleted item is \n%d \n%s",ptr->no,ptr->name); free(ptr); } } void qdisplay() { queue *i,*next; i=front;

27

while(i!=rear) { printf("\nname=%s \nroll no =%d",i->name,i->no); i=i->next; } }

// WAP to implement circular queue. #include<stdio.h> #include<conio.h> #include<stdlib.h> #define len 10 int i,front=-1,rear=-1,q[len]; void qinsert(); void qdelete(); void qdisplay(); void main() { int c; char ch='y'; while(ch!='n') { clrscr(); printf("\nenter the choice"); printf("\n1. insert"); printf("\n2.delete"); printf("\n3.display"); printf("\n4.exit"); scanf("%d",&c); switch(c) { case 1: qinsert(); break; case 2: qdelete();

28

break; case 3: qdisplay(); break; case 4: exit(0); default: printf("\nyou entered wrong choice"); } printf("\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void qinsert() { if(front==(rear+1)%len) { printf("queue is full\n"); } else { printf("enter the element to be insert\n"); scanf("%d",&i); if(front==-1) front=rear=0; else rear=((rear+1)%len); q[rear]=i; } } void qdelete() { if(front==-1) { printf("queue is empty"); } else { i=q[front]; printf("deleted item is %d\n",q[front]); if(front==rear) front=rear=-1; else front=((front+1)%len); } } void qdisplay() { if(front==-1) { printf("\nqueue is empty"); } else if(front>rear) { for(i=front;i<len;i++) printf("\n%d",q[i]); for(i=0;i<=rear;i++) printf("\n%d",q[i]);

29

} else { printf("\n the status of the queue\n"); for(i=front;i<=rear;i++) printf("\n%d",q[i]); } }

// WAP to perform operations on singly linked list. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct linked { int no; linked*link; }*start=NULL; void insbeg(); void insmid(); void inslast(); void delbeg(); void delmid(); void dellast(); void display(); void count(); void reverse(); void main() { int c; char ch='y'; while(ch!='n') { clrscr(); for(int i=0;i<60;i++) printf("\xDB"); printf("\nenter your choice"); printf("\n\n1.insert at begning"); printf("\n2.insert at mid"); printf("\n3.insert at last");

30

printf("\n4.delete at begning"); printf("\n5.delete at mid"); printf("\n6.delete at last"); printf("\n7.display"); printf("\n8.count the node in link list"); printf("\n9.reverse the list..."); printf("\n10.exit\n"); for(i=0;i<60;i++) printf("\xCD"); printf("\n"); scanf("%d",&c); switch(c) { case 1: insbeg(); break; case 2: insmid(); break; case 3: inslast(); break; case 4: delbeg(); break; case 5: delmid(); break; case 6: dellast(); break; case 7: display(); break; case 8: count(); break; case 9: reverse(); break; case 10: exit(0); default: printf("\nyou entered wrong choice"); } printf("\n\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void insbeg() { linked*ptr; ptr=(linked*)malloc(sizeof(linked)); ptr->link=NULL; printf("enter the no"); scanf("%d",&ptr->no); if(start==NULL) {

31

start=ptr; } else { ptr->link=start; start=ptr; } } void insmid() { int i,loc; linked*ptr,*temp; ptr=(linked*)malloc(sizeof(linked)); ptr->link=NULL; printf("enter the no"); scanf("%d",&ptr->no); if(start==NULL) start=ptr; else { printf("enter the position at which we want insert the no"); scanf("%d",&loc); for(i=1,temp=start;i<loc-1 ;i++,temp=temp->link) { } ptr->link=temp->link; temp->link=ptr; } } void inslast() { linked*ptr,*temp; ptr=(linked*)malloc(sizeof(linked)); ptr->link=NULL; printf("enter the no"); scanf("%d",&ptr->no); if(start==NULL) { start=ptr; } else { for(temp=start;temp->link!=NULL;temp=temp->link) { } temp->link=ptr; } } void delbeg() { linked*ptr; if(start==NULL) printf("list is empty"); else { ptr=start; start=start->link; printf("deletd item is %d",ptr->no); free(ptr);

32

} } void delmid() { int i,loc; linked*ptr,*temp; if(start==NULL) printf("list is empty"); else { printf("enter the position at which we want delete the no"); scanf("%d",&loc); for(i=1,temp=start,ptr=start->link;i<loc-1;i++,temp=temp->link,ptr=ptr->link) { } temp->link=ptr->link; printf("deletd item is %d",ptr->no); free(ptr); } } void dellast() { linked*ptr,*temp; if(start==NULL) printf("list is empty"); else if(start->link==NULL) { ptr=start; start=NULL; printf("deletd item is %d",ptr->no); free(ptr); } else { temp=start; ptr=start->link; while(ptr->link!=NULL) { temp=temp->link; ptr=ptr->link; } temp->link=NULL; printf("deletd item is %d",ptr->no); free(ptr); } } void display() { linked*ptr; for(ptr=start;ptr;ptr=ptr->link) { printf("%d\t",ptr->no); } } void count() { linked*ptr; int i=0; for(ptr=start;ptr;ptr=ptr->link) {

33

i++; printf("%d",ptr->no); } printf("no of the nodes=%d",i); } void reverse() { linked *temp,*r,*q; temp=start; r=NULL; while(temp!=NULL) { q=r; r=temp; temp=temp->link; r->link=q; } start=r; display(); }

// WAP to perform operations on doubly linked list. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct linked { int no; linked*next,*prev; }*start=NULL; void insbeg(); void insmid(); void inslast(); void delbeg(); void delmid(); void dellast(); void display(); void reverse(); void count(); void main() { int c; char ch='y'; while(ch!='n') { clrscr(); printf("\nenter your choice"); printf("\n\n1.insert at begning"); printf("\n2.insert at mid"); printf("\n3.insert at last"); printf("\n4.delete at begning"); printf("\n5.delete at mid"); printf("\n6.delete at last"); printf("\n7.display"); printf("\n8.count the node"); printf("\n9.reverse the list...");

34

printf("\n10.exit\n\n"); scanf("%d",&c); switch(c) { case 1: insbeg(); break; case 2: insmid(); break; case 3: inslast(); break; case 4: delbeg(); break; case 5: delmid(); break; case 6: dellast(); break; case 7: display(); break; case 8: count(); break; case 9: reverse(); break; case 10: exit(0); default: printf("\nyou entered wrong choice"); } printf("\n\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void insbeg() { linked*ptr; ptr=(linked*)malloc(sizeof(linked)); printf("enter the no"); scanf("%d",&ptr->no); ptr->next=ptr->prev=NULL; if(start==NULL) { start=ptr; } else { ptr->next=start; start->prev=ptr; start=ptr;

35

} } void insmid() { int i,loc; linked*ptr,*temp; ptr=(linked*)malloc(sizeof(linked)); printf("enter the no"); scanf("%d",&ptr->no); ptr->next=ptr->prev=NULL; if(start==NULL) start=ptr; else if(start->next==NULL) { start->next=ptr; ptr->prev=start; } else { printf("enter the position at which we want insert the no"); scanf("%d",&loc); for(i=1,temp=start;i<loc-1;i++,temp=temp->next) { } ptr->next=temp->next; temp->next=ptr; ptr->prev=temp; } } void inslast() { linked*ptr,*temp; ptr=(linked*)malloc(sizeof(linked)); printf("enter the no"); scanf("%d",&ptr->no); ptr->next=ptr->prev=NULL; if(start==NULL) { start=ptr; } else { for(temp=start;temp->next!=NULL;temp=temp->next) { } temp->next=ptr; ptr->prev=temp; temp=ptr; ptr->next=NULL; } } void delbeg() { linked*ptr; if(start==NULL) printf("list is empty"); else if(start->next==NULL) { ptr=start; start=NULL;

36

printf("deletd item is %d",ptr->no); free(ptr); } else { ptr=start; start=start->next; start->prev=NULL; printf("deleted item is %d",ptr->no); free(ptr); } } void delmid() { int i,loc; linked*ptr,*temp; if(start==NULL) printf("list is empty"); else { printf("enter the position at which we want delete the no"); scanf("%d",&loc); for(i=1,temp=start,ptr=start->next;i<loc-1; i++,temp=temp->next,ptr=ptr->next) { } temp->next=ptr->next; ptr->prev=temp; printf("deletd item is %d",ptr->no); free(ptr); } } void dellast() { linked*ptr,*temp; if(start==NULL) printf("list is empty"); else if(start->next==NULL) { ptr=start; start=NULL; printf("deletd item is %d",ptr->no); free(ptr); } else { temp=start; ptr=start->next; while(ptr->next!=NULL) { temp=ptr; ptr=ptr->next; } ptr->prev=temp; temp->next=NULL; printf("deletd item is %d",ptr->no); free(ptr); } } void display()

37

{ linked*ptr; for(ptr=start;ptr;ptr=ptr->next) { printf("%d\t",ptr->no); } } void count() { linked*ptr; int i=0; for(ptr=start;ptr;ptr=ptr->next) { i++; printf("%d\n",ptr->no); } printf("\nno of the nodes=%d",i); } void reverse() { linked *temp,*r,*q; temp=start; r=NULL; while(temp!=NULL) { q=r; r=temp; temp=temp->next; r->next=q; q->prev=r; } start=r; display(); }

38

// WAP to perform operations on circular linked list. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct linked { int no; linked*next; }*start=NULL,*last; void insbeg(); void insmid(); void inslast(); void delbeg(); void delmid(); void dellast(); void display(); void main() { int c; char ch='y'; while(ch!='n') { clrscr(); printf("\nenter your choice"); printf("\n\n1.insert at begning"); printf("\n2.insert at mid"); printf("\n3.insert at last"); printf("\n4.delete at begning"); printf("\n5.delete at mid"); printf("\n6.delete at last"); printf("\n7.display"); printf("\n8.exit"); scanf("%d",&c); switch(c) { case 1: insbeg(); break; case 2: insmid();

39

break; case 3: inslast(); break; case 4: delbeg(); break; case 5: delmid(); break; case 6: dellast(); break; case 7: display(); break; case 8: exit(0); default: printf("\nyou entered wrong choice"); } printf("\n\ndo you want to continue(y or n)"); fflush(stdin); scanf("%c",&ch); } getch(); } void insbeg() { linked*ptr; ptr=(linked*)malloc(sizeof(linked)); printf("enter the no. "); scanf("%d",&ptr->no); if(start==NULL) { start=last=ptr; ptr->next=NULL; } else { ptr->next=start; start=ptr; last->next=start; } } void insmid() { int i,loc; linked*ptr,*temp,*t2; ptr=(linked*)malloc(sizeof(linked)); printf("enter the no"); scanf("%d",&ptr->no); if(start==NULL) start=last=ptr; else { printf("enter the position at which we want insert the no"); scanf("%d",&loc); for(i=1,temp=start,t2=start->next;i<loc-1;i++,temp=temp->next,t2=t2->next) {

40

} ptr->next=t2->next; temp->next=ptr; } } void inslast() { linked*ptr,*temp; ptr=(linked*)malloc(sizeof(linked)); printf("enter the no"); scanf("%d",&ptr->no); if(start==NULL) { ptr->next=NULL; start=last=ptr; } else for(temp=start;temp->next!=NULL;temp=temp->next) { } temp->next=ptr; ptr->next=start; } void delbeg() { linked*ptr; if(start==NULL) printf("list is empty"); else { ptr=start; start=start->next; last->next=start; printf("deletd item is %d",ptr->no); free(ptr); } } void delmid() { int i,loc; linked*ptr,*temp; if(start==NULL) printf("list is empty"); else { printf("enter the position at which we want delete the no"); scanf("%d",&loc); for(i=1,temp=start,ptr=start->next;i<loc-1;i++,temp=temp->next,ptr=ptr->next) { } temp->next=ptr->next; printf("deletd item is %d",ptr->no); free(ptr); } } void dellast() { linked*ptr,*temp; if(start==NULL) printf("list is empty");

41

else if(start->next==NULL) { ptr=start; start=NULL; printf("deletd item is %d",ptr->no); free(ptr); } else { temp=start; ptr=start->next; while(ptr->next!=NULL) { temp=ptr; ptr=ptr->next; } temp->next=start; printf("deletd item is %d",ptr->no); free(ptr); } } void display() { linked*ptr; for(ptr=start;ptr;ptr=ptr->next) { printf("%d\n",ptr->no); } }

42

// WAP to implement Binary Search Tree. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int num; node *left; node *right; };//typedef struct node node; node *P=NULL; node *insert(node *P,int num); void inorder(node *P); void preorder(node *P); void postorder(node *P); int select() { int s; do { printf("Enter 1:Insert a node in the BST\n"); printf("Enter 2:Display (preorder) the BSt\n"); printf("Enter 3:Display (inorder) the BSt\n"); printf("Enter 4:Display (postorder) the BSt\n"); printf("Enter 5:END\n "); scanf("%d",&s); if(s<1||s>5) { printf("Wrong choice...\n:try again\n"); getch(); } }while((s<1)||(s>5)); return(s); } void main() { clrscr(); int choice; int dig; do

43

{ choice=select(); switch(choice) { case 1: { printf("enter the integer : to quit 0"); scanf("%d",&dig); while(dig!=0) { P=insert(P,dig); scanf("%d",&dig); } }continue; case 2: { printf("\npreorder traversing...\n"); preorder(P); }continue; case 3: { printf("\nInorder traversing...\n"); inorder(P); }continue; case 4: { printf("\npostorder traversing...\n"); postorder(P); }continue; case 5: printf("End");exit(0) ; } } while(choice!=5); } node *insert(node *P,int digit) { if(P==NULL) { P=(node*)malloc(sizeof(node)); P->left=P->right=NULL; P->num=digit; } else { if(digit<P->num) P->left=insert(P->left,digit); else if(digit>=P->num) P->right=insert(P->right,digit); } return(P) ; }

void preorder(node *P) {

44

if(P!=NULL) { printf("%d\n",P->num); preorder(P->left); preorder(P->right); } } void postorder(node *P) { if(P!=NULL) { postorder(P->left); postorder(P->right); printf("%d\n",P->num); } } void inorder(node *P) { if(P!=NULL) { inorder(P->left); printf("%d\n",P->num); inorder(P->right); } }

45

// WAP to implement Linear Search. #include<stdio.h> #include<conio.h> void main() { int a[10],n,num,i,flag=0; clrscr(); start: printf("\n enter the size for array"); scanf("%d",&n); if(n>20) { printf("\n size required is out of array size"); printf("\nenter the size from 1 to 20"); goto start; } printf("\nenter %d element for array...\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n enter the no. to search"); scanf("%d",&num); printf("\nnow searching start...."); for(i=0;i<n;i++) { if(a[i]==num) { printf("\n%d is found at %d position",num,i+1); flag=1; break; } } if(flag==0) { printf("\n%d is not found in entire array",num); } getch(); }

46

// WAP to implement Binary Search. #include<stdio.h> #include<conio.h> void main() { int a[20],n,num,i,beg,mid,end; clrscr(); start: printf("\n enter the size for array"); scanf("%d",&n); if(n>20) { printf("\n size required is out of array size"); printf("\nenter the size from 1 to 20"); goto start; } printf("\nenter %d element for array...(in assending order)\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n enter the no. to search"); scanf("%d",&num); beg=0; end=n-1; mid=(beg+end)/2; while(num!=a[mid]&&beg<=end) { if(num<a[mid]) end=mid-1; else beg=mid+1; mid=(beg+end)/2; } if(num==a[mid]) printf("\n%d is found at %d position",num,mid+1); else printf("\n %d is not found in entire array",num); getch(); }

47

// WAP to perform Selection Sort. #include<stdio.h> #include<conio.h> void main() { int a[20],n,min,i,p,pos,temp; clrscr(); start: printf("\n enter the size for array"); scanf("%d",&n); if(n>20) { printf("\n size required is out of array size"); printf("\n enter the size from 1 to 20"); goto start; } printf("\n enter %d element for array...\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n now sort process start....."); for(i=0;i<=n-1;i++) { pos=i; min=a[i]; for(p=i+1;p<=n-1;p++) { if(a[p]<min) { min=a[p]; pos=p; } } temp=a[i]; a[i]=a[pos]; a[pos]=temp; } printf("\n sorted array in assendimg order:\n"); for(i=0;i<n;i++) { printf("\n%d",a[i]); } getch(); }

48

// WAP to perform Insertion Sort. #include<stdio.h> #include<conio.h> main() { int a[20],n,i,k,y; start: printf("\n enter the size for array"); scanf("%d",&n); if(n>20) { printf("\n size required is out of array size"); printf("\n enter the size from 1 to 20"); goto start; } printf("\n enter %d element for array...\n",n); for(k=0;k<n;k++) scanf("%d",&a[k]); printf("\n now sort process start....."); for(k=0;k<n;k++) { y=a[k]; for(i=k-1;i>=0&&y<a[i];i--) a[i+1]=a[i]; a[i+1]=y; } for(k=0;k<n;k++) { printf("\n%d",a[k]); } getch(); }

49

// WAP to perform Bubble Sort. #include<stdio.h> #include<conio.h> void main() { int n,i,a[20],c,j; clrscr(); printf("enter the size of array..."); scanf("%d",&n); printf("enterthe %d element.... ",n); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("now sort proces starts..."); for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { if(a[j]>a[j+1]) { c=a[j]; a[j]=a[j+1]; a[j+1]=c; } } } for(i=1;i<=n;i++) { printf("\n%d",a[i]); } getch(); }

50

// WAP to perform Quick Sort. #include<stdio.h> #include<conio.h> int quick(int[],int,int); main() { int a[20],n,i,l,h; start: printf("\n enter the size for array"); scanf("%d",&n); if(n>20) { printf("\n size required is out of array size"); printf("\nenter the size from 1 to 20"); goto start; } printf("\nenter %d element for array...\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); l=0; h=n-1; quick(a,l,h); printf("\nsorted array is ::\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); } int quick(int a[],int l,int h) { int temp,low,high,key; low=l; high=h; key=a[(low+high)/2]; do { while(key>a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp; } } while(low<=high); if(l<high)

51

quick(a,l,high); if(low<h) quick(a,low,h); return(0); }

52

Das könnte Ihnen auch gefallen