Sie sind auf Seite 1von 51

/*Recursive program to compute the nth Fibonacci number*/ #include<stdio.h> #include<conio.

h> int fib(int); void main() { int n,f; clrscr(); printf("Enter the term: "); scanf("%d",&n); f=fib(n-1); /*Function call*/ /*Function prototype*/

printf("%dth term of Fibonacci series: %d\n",n,f); getch(); } /*Function definition*/ int fib(int n) { if(n==0||n==1) return n; else return fib(n-1)+fib(n-2); } /*Recursive program to compute Factorial of an integer*/ #include<stdio.h> #include<conio.h> long int factorial(int); void main() { int n; long int f; clrscr(); printf("Enter the number: "); scanf("%d",&n); f=factorial(n); /*Function call*/ /*Function prototype*/ /*Recursive Function call*/ /*Base criteria*/

printf("Factorial of %d: %ld\n",n,f);

getch(); } /*Function definition*/ long int factorial(int n) { if(n==0||n==1) return 1; else return n*factorial(n-1); } /*Recursive program for calculation of GCD(n,m)*/ #include<stdio.h> #include<conio.h> int gcd(int,int); void main() { int n,m,g; clrscr(); printf("Enter two numbers: "); scanf("%d%d",&n,&m); g=gcd(n,m); /*Function call*/ /*Function prototype*/ /*Recursive Function call*/ /*Base criteria*/

printf("GCD of %d & %d: %d\n",n,m,g); getch(); } /*Function definition*/ int gcd(int n,int m) { if(n==0) return m; if(m==0) return n; return gcd(m,n%m); } /*Recursive Function call*/ /*Base criteria*/ /*Base criteria*/

/*Recursive program for Towers of Hanoi: N disks are to be transferred from peg S to peg D with peg A as the intermediate peg*/ #include<stdio.h> #include<conio.h> void transfer(int,int,int,int); void main() { int n; clrscr(); printf("Enter number of disks: "); scanf("%d",&n); transfer(n,1,3,2); getch(); } void transfer(int n,int s,int d,int a) { if(n==0) /*Base criteria*/ /*Function definition*/ /*Function call*/ /*Function prototype*/

printf("Move disk %d from %d to %d\n",n,s,d); else { transfer(n-1,s,a,d); /*Recursive Function call*/

printf("Move disk %d from %d to %d\n",n,s,d); transfer(n-1,a,d,s); } } /* Program to perform linear search using recursive & non-recursive functions*/ #include<stdio.h> #include<conio.h> int rlinear_search(int [],int,int,int); int ilinear_search(int [],int,int); void main() { int n,key,i,pos,ch; int a[100]; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); /*Function prototype*/ /*Function prototype*/ /*Recursive Function call*/

for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("Enter element to be searched: "); scanf("%d",&key); printf("Enter your choice (1-Non-recursive/2-Recursive): "); scanf("%d",&ch); if(ch==1) pos=ilinear_search(a,n,key); /*Non-recursive function call*/ else pos=rlinear_search(a,n,key,0); /*Recursive function call*/ if(pos != -1) printf("Element found successfully at position %d\n",pos+1); else printf("Element not found\n"); getch(); } int rlinear_search(int a[],int n,int key,int i) /*Recursive linear search function definition*/ { if(i>=n) /*Base criteria*/ return -1; if(i<n) { if(a[i]==key) return i; else return rlinear_search(a,n,key,i+1); /*Recursive Function call*/ } } /*Non-Recursive linear search function definition*/ int ilinear_search(int a[],int n,int key) { int i; for(i=0;i<n;i++) { if(a[i]==key) return i; } return -1; }

/* Program to perform binary search using recursive & non_recursive functions*/ #include<stdio.h> #include<conio.h> int rbinary_search(int [],int,int,int); int ibinary_search(int [],int,int); /*Function prototype*/ /*Function prototype*/

void main() { int n,key,i,pos,ch; int a[100]; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements in ascending order\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("Enter element to be searched: "); scanf("%d",&key); printf("Enter your choice (1-Non recursive/2-Recursive): "); scanf("%d",&ch); if(ch==1) pos=ibinary_search(a,n,key); /*Non recursive function call*/ else pos=rbinary_search(a,0,n-1,key); /*Recursive function call*/ if(pos != -1) printf("Element found successfully at position %d\n",pos+1); else printf("Element not found\n"); getch(); } /*Recursive binary search function definition*/ int rbinary_search(int a[],int lb,int ub,int key) { int mid; if(lb>ub) /*Base criteria*/ return -1; else { mid=(lb+ub)/2; if(a[mid]==key) return mid; else if(key<a[mid]) return rbinary_search(a,lb,mid-1,key); /*Recursive Function call*/ else return rbinary_search(a,mid+1,ub,key); /*Recursive Function call*/ } } /*Non-Recursive binary search function definition*/ int ibinary_search(int a[],int n,int key) { int mid,i,j; i=0; j=n-1; while(i<=j)

{ mid=(i+j)/2; if(key==a[mid]) return mid; else if(key<a[mid]) j=mid-1; else i=mid+1; } return -1; }

/*Program to perform Fibonacci Search*/ #include<stdio.h> #include<conio.h> int fibonacci_search(int [],int,int); /*Function prototype*/ int fib(int); /*Function prototype*/ void main() { int a[100],n,key,i,pos; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]",i); scanf("%d",&a[i]); } printf("Enter the element to be searched: "); scanf("%d",&key); pos=fibonacci_search(a,n,key); /*Function call*/ if(pos!=-1) printf("Element %d is found at position %d\n",key,pos+1); getch(); } int fibonacci_search(int a[],int n,int key) /*Function Definition*/ { int p,q,r,t,k,m; k=0; while(fib(k)<n) /*Function call*/ k++; p=fib(k-1); /*Function call*/ q=fib(k-2); /*Function call*/ r=fib(k-3); /*Function call*/ m=(n+1)-(p+q); if(key>a[p]) p=p+m; while(p!=0) { if(key==a[p]) { printf("Key found\n"); return p; } else if(key<a[p]) { if(r==0) p=0; else { p=p-r; t=q; q=r; r=t-r;

} } else { if(q==1) p=0; else { p=p+r; q=q-r; r=r-q; } } } if(key==a[p]) return p; printf("Key not found\n"); return -1; } int fib(int n) { if(n==0 || n==1) return n; else return fib(n-1)+fib(n-2); } /*Function definition*/ /*Base criteria*/

/*Recursive Function call*/

/*Program to implement Bubble Sort in ascending order*/ #include<stdio.h> #include<conio.h> void bubble_sort(int [],int); void swap(int *,int *); /*Function prototype*/ /*Function prototype*/

void main() { int a[100],n,i; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("\nElements before sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); bubble_sort(a,n); /*Function call*/ printf("\nElements after sorting: "); for(i=0;i<n;i++)

printf("%d ",a[i]); getch(); } void bubble_sort(int a[],int n) { int i,j; for(i=0;i<n-1;i++) { for(j=0;j<n-1-i;j++) if(a[j]>a[j+1]) swap(&a[j],&a[j+1]); } } void swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; } /*Function definition*/

/*No. of passes*/ /*No. of comparisons*/ /*Comparing adjacent elements*/ /*Function call*/

/*Function definition*/

/*Program to implement Quick Sort in ascending order*/ #include<stdio.h> #include<conio.h> void quick_sort(int [],int,int); void swap(int *,int *); /*Function prototype*/ /*Function prototype*/

void main() { int a[100],n,i; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("\nElements before sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); quick_sort(a,0,n-1); /*Function call*/ printf("\nElements after sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); getch(); }

void quick_sort(int a[],int lb,int ub) { int i,j,pivot; if(lb<ub) { i=lb; j=ub; pivot=lb; while(i<=j) { while(a[i]<=a[pivot] && i<ub) i++; while(a[j]>a[pivot]) j--; if(i<j) swap(&a[i],&a[j]); } swap(&a[pivot],&a[j]); quick_sort(a,lb,j-1); quick_sort(a,j+1,ub); } } void swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; }

/*Function definition*/

/*Function call*/ /*Function call*/ /*Recursive Function call*/ /*Recursive Function call*/

/*Function definition*/

/*Program to implement Insertion Sort in ascending order*/ #include<stdio.h> void insertion_sort(int [],int); /*Function prototype*/ void main() { int a[100],n,i; printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("\nElements before sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); insertion_sort(a,n); /*Function call*/ printf("\nElements after sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); }

void insertion_sort(int a[],int n) { int i,temp,pos; for(i=1;i<n;i++) { temp=a[i]; pos=i; while(pos>0&&a[pos-1]>temp) { a[pos]=a[pos-1]; --pos; } a[pos]=temp; } }

/*Function definition*/

/*Program to implement Heap Sort in ascending order*/ #include<stdio.h> #include<conio.h> void build_heap(int [],int); void heap_sort(int [],int);

/*Function prototype*/ /*Function prototype*/

void main() { int a[100],n,i; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("\nElements before sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); build_heap(a,n); /*Function call*/ heap_sort(a,n); /*Function call*/ printf("\nElements after sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); getch(); } void build_heap(int a[],int n) { int i,s,f,temp; for(i=1;i<n;i++) { temp=a[i]; s=i; f=(s-1)/2; /*Function definition*/

while(s>0 && a[f]<temp) { a[s]=a[f]; s=f; f=(s-1)/2; } a[s]=temp; } } void heap_sort(int a[],int n) /*Function definition*/ { int i,temp,f,s; for(i=n-1;i>0;i--) { temp=a[i]; a[i]=a[0]; f=0; if(i==1) s=-1; else s=1; if(i>2 && a[2]>a[1]) s=2; while(s>0 && a[s]>temp) { a[f]=a[s]; f=s; s=2*f+1; if(s+1<=i-1 && a[s+1]>a[s]) s++; if(s>i-1) s=-1; } a[f]=temp; } } /*Program to implement Radix Sort in ascending order*/ #include<stdio.h> #include<conio.h> void radix_sort(int [],int); /*Function prototype*/

void main() { int a[100],n,i; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) {

printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("\nElements before sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); radix_sort(a,n); /*Function call*/ printf("\nElements after sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); getch(); } void radix_sort(int a[],int n) /*Function definition*/ { int i,j,k,div,large,e,nod,pass; int bucket[10][4],index[10]; large=a[0]; for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; } nod=0; while(large>0) { nod++; large /=10; } div=1; for(pass=0;pass<nod;pass++) { for(k=0;k<10;k++) index[k]=0; for(i=0;i<n;i++) { e=(a[i]/div)%10; bucket[e][index[e]++]=a[i]; } i=0; for(k=0;k<10;k++) { for(j=0;j<index[k];j++) a[i++]=bucket[k][j]; } div=div*10; } }

/*Program to implement Merge Sort in ascending order*/ #include<stdio.h> #include<conio.h> void merge_sort(int [],int,int); void merge(int[],int,int,int); /*Function prototype*/ /*Function prototype*/

void main() { int a[100],n,i; clrscr(); printf("Enter size of the list: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { printf("Enter a[%d]: ",i); scanf("%d",&a[i]); } printf("\nElements before sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); merge_sort(a,0,n-1); /*Function call*/ printf("\nElements after sorting: "); for(i=0;i<n;i++) printf("%d ",a[i]); getch(); } void merge_sort(int a[],int p,int r) { int q; if(p<r) { q=(p+r)/2; merge_sort(a,p,q); merge_sort(a,q+1,r); merge(a,p,q,r); } } void merge(int a[],int p,int q,int r) { int i,j,k,b[100]; i=p; j=q+1; k=p; while(i<=q && j<=r) { if(a[i]<a[j]) { /*Function definition*/

/*Recursive Function call*/ /*Recursive Function call*/ /*Function call*/

/*Function definition*/

b[k++]=a[i]; i++; } else { b[k++]=a[j]; j++; } } while(i<=q) { b[k++]=a[i++]; } while(j<=r) b[k++]=a[j++]; for(i=p;i<=r;i++) a[i]=b[i]; } /*Program to implement stack using arrays*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAXSTK 100 int top=-1; int stack[MAXSTK]; void push(int); int pop(void); void display(void); int isempty(void); int isfull(void); /*Function prototype*/ /*Function prototype*/ /*Function prototype*/ /*Function prototype*/ /*Function prototype*/

void main() { int ch,x; clrscr(); do { printf("1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter an element: "); scanf("%d",&x); push(x); /*Function call*/ break;

case 2: x=pop(); /*Function call*/ printf("\nPopped element: %d\n",x); break; case 3: display(); break; case 4: exit(0); } printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void push(int x) { if(isfull()) { printf("Stack is full\n"); return; } top++; stack[top]=x; } int pop(void) { int x; if(isempty()) { printf("Stack is empty\n"); exit(0); } x=stack[top]; top--; return x; } int isempty(void) { if(top==-1) return 1; else return 0; } int isfull(void) { if(top==MAXSTK-1) return 1; /*Function definition*/ /*Function call*/ /*Function call*/

/*Function definition*/

/*Function call*/

/*Function definition*/

/*Function definition*/

else return 0; } void display() { int i; if(isempty()) { printf("Stack is empty\n"); return; } printf("Elements of stack: "); for(i=top;i>=0;i--) printf("%d ",stack[i]); printf("\n"); } /*Function definition*/

/*Function call*/

OUTPUT 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 1 Enter an element: 10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 1 Enter an element: 20 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 3 Elements of stack: 20 10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 2 Popped element: 20 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY

4.QUIT Enter your choice: 3 Elements of stack: 10 Do you want to continue (1-YES/2-NO): 2

6) aii) Write a program that implements stack (its operations) using arrays. /*Program to implement stack using arrays*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAXSTK 100 typedef struct { int top; int items[MAXSTK]; }stack; void push(stack *); int pop(stack *); void display(stack *); int isempty(stack *); int isfull(stack *); /*Function prototype*/ /*Function prototype*/ /*Function prototype*/ /*Function prototype*/ /*Function prototype*/

void main() { stack s; int ch,x; clrscr(); s.top=-1; do { printf("1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: push(&s); /*Function call*/ break; case 2: x=pop(&s); /*Function call*/ printf("\nPopped element: %d\n",x); break; case 3: display(&s); /*Function call*/ break; case 4: exit(0); } printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void push(stack *s) /*Function definition*/ { int x;

if(isfull(s)) { printf("Stack is full\n"); return; } printf("Enter an element: "); scanf("%d",&x); s->top++; s->items[s->top]=x; } int pop(stack *s) { int x; if(isempty(s)) { printf("Stack is empty\n"); exit(0); } x=s->items[s->top]; s->top--; return x; } int isempty(stack *s) { if(s->top==-1) return 1; else return 0; } int isfull(stack *s) { if(s->top==MAXSTK-1) return 1; else return 0; } void display(stack *s) { int i; if(isempty(s)) { printf("Stack is empty\n"); return; } printf("Elements of stack: "); for(i=s->top;i>=0;i--) printf("%d ",s->items[i]); printf("\n"); }

/*Function call*/

/*Function definition*/

/*Function call*/

/*Function definition*/

/*Function definition*/

/*Function definition*/

/*Function call*/

OUTPUT 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 1 Enter an element: 10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 1 Enter an element: 20 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 3 Elements of stack: 20 10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 2 Popped element: 20 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 2 Popped element: 10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 3 Stack is empty Do you want to continue (1-YES/2-NO): 2

6) b) Write a program that implements stack (its operations) using linked list. /*Program to implement stack using Linked List*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct link { int info; struct link *next; }; typedef struct link node; node *start=NULL; void push(); int pop(); void display(); int isempty(); /*Function prototype*/ /*Function prototype*/ /*Function prototype*/ /*Function prototype*/

void main() { int ch,x; clrscr(); do { printf("1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: push(); /*Function call*/ break; case 2: x=pop(); /*Function call*/ printf("\nPopped element: %d\n",x); break; case 3: display(); /*Function call*/ break; case 4: exit(0); } printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void push() /*Function definition*/ { node *p;

p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory space"); exit(0); } printf("Enter an element: "); scanf("%d",&p->info); p->next=start; start=p; } int pop() { int x; node *temp; temp=start; if(isempty()) { printf("Stack is empty\n"); exit(0); } x=start->info; start=start->next; free(temp); return x; } int isempty() { if(start==NULL) return 1; else return 0; } void display() { node *temp; if(isempty()) { printf("Stack is empty\n"); return; } temp=start; printf("Elements of stack: "); while(temp->next!=NULL) { printf("%d->",temp->info); temp=temp->next; } printf("%d\n",temp->info); } /*Function definition*/

/*Function definition*/

/*Function definition*/

OUTPUT 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 3 Stack is empty Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 1 Enter an element: 10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 1 Enter an element: 20 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 3 Elements of stack: 20->10 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 2 Popped element: 20 Do you want to continue (1-YES/2-NO): 1 1.PUSH 2.POP 3.DISPLAY 4.QUIT Enter your choice: 3 Elements of stack: 10 Do you want to continue (1-YES/2-NO): 2 7) a) Write a program that uses Stack operations to convert infix expression into postfix expression. /*Program to convert infix to postfix expression*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> #define MAXSTK 100 int top=-1; char stack[MAXSTK];

void push(char); char pop(void); int isp(char); int icp(char); void main() { char infix[100],postfix[100],ch,ch1; int i,j; clrscr(); printf("Enter infix expression: "); gets(infix); push('('); for(i=0,j=0;infix[i]!='\0';i++) { ch=infix[i]; if(isalpha(ch)) postfix[j++]=ch; else { if(ch==')') { while(stack[top]!='(') { ch1=pop(); postfix[j++]=ch1; } top--; } else { while(isp(stack[top])>icp(ch)) { ch1=pop(); postfix[j++]=ch1; } push(ch); } } } postfix[j++]='\0'; printf("\nPostfix Expression: %s\n",postfix); getch(); } void push(char x) { top++; stack[top]=x; } char pop(void) {

char x; x=stack[top]; top--; return x; } int isp(char c) { switch(c) { case '(': return 0; case '*': case '/': return 2; case '+': case '-': return 1; } } int icp(char c) { switch(c) { case '(': return 4; case '*': case '/': return 2; case '+': case '-': return 1; } } 7) b) Write a program that implements Queue (its operations) using arrays. /*Program to implement Queue using arrays*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAXQUE 100 int front=0,rear=-1; int que[MAXQUE]; void insert(int); int delet(void); void display(void); int isempty(void); int isfull(void); void main() { int ch,x; clrscr(); do { printf("1.INSERT\n2.DELETE\n3.DISPLAY\n4.QUIT\n");

printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter an element: "); scanf("%d",&x); insert(x); break; case 2: x=delet(); printf("\nDeleted element: %d\n",x); break; case 3: display(); break; case 4: exit(0); } printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void insert(int x) { if(isfull()) { printf("Queue is full\n"); return; } rear++; que[rear]=x; } int delet(void) { int x; if(isempty()) { printf("Queue is empty\n"); exit(0); } x=que[front]; front++; return x; } int isempty(void) { if(front>rear)

return 1; else return 0; } int isfull(void) { if(rear==MAXQUE-1) return 1; else return 0; } void display() { int i; if(isempty()) { printf("Queue is empty\n"); return; } printf("Elements of Queue: "); for(i=front;i<=rear;i++) printf("%d ",que[i]); printf("\n"); } 7) c) Write a program that implements Queue (its operations) using linked lists. /*Program to implement Queue using Linked List*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct link { int info; struct link *next; }; typedef struct link node; node *front=NULL,*rear=NULL; void insert(); int delet(); void display(); int isempty(); void main() { int ch,x; clrscr(); do {

printf("1.INSERT\n2.DELETE\n3.DISPLAY\n4.QUIT\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: insert(); break; case 2: x=delet(); printf("\nDeleted element: %d\n",x); break; case 3: display(); break; case 4: exit(0); } printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void insert() { node *p; p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory space"); exit(0); } printf("Enter an element: "); scanf("%d",&p->info); p->next=NULL; if(rear==NULL) { front=rear=p; } else { rear->next=p; rear=p; } } int delet() { int x; node *temp; if(isempty()) {

printf("QUEUE is empty\n"); exit(0); } temp=front; x=temp->info; if(front==rear) front=rear=NULL; else front=front->next; free(temp); return x; } int isempty() { if(front==NULL&&rear==NULL) return 1; else return 0; } void display() { node *temp; if(isempty()) { printf("QUEUE is empty\n"); return; } temp=front; printf("Elements of Queue: "); while(temp->next!=NULL) { printf("%d->",temp->info); temp=temp->next; } printf("%d\n",temp->info); }

8) a) /*Program to create singly linked list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct list { int info; struct list *next; }; typedef struct list node; node *start=NULL; void createlist(void); void display(void); void main() { clrscr(); createlist(); display(); getch(); } void createlist() { node *p,*ptr; int ch; p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; start=ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); while(ch==1) { p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; ptr->next=p;

ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); } } void display() { node *ptr; if(start==NULL) { printf("Linked List is empty\n"); return; } ptr=start; printf("Elements of Linked List: "); while(ptr->next!=NULL) { printf("%d->",ptr->info); ptr=ptr->next; } printf("%d\n",ptr->info); } 8) b) /*Program to perform insertion operation on a singly linked list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct list { int info; struct list *next; }; typedef struct list node; node *start=NULL; void createlist(void); void display(void); void insert(int); void main() { int x,ch; clrscr(); createlist(); display(); do { printf("Enter element to be inserted: ");

scanf("%d",&x); insert(x); display(); printf("Do you want to continue(1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void createlist() { node *p,*ptr; int ch; p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; start=ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); while(ch==1) { p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; ptr->next=p; ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); } } void display() { node *ptr; if(start==NULL) { printf("Linked List is empty\n"); return; } ptr=start;

printf("Elements of Linked List: "); while(ptr->next!=NULL) { printf("%d->",ptr->info); ptr=ptr->next; } printf("%d\n",ptr->info); } void insert(int x) { node *p,*curr,*prev; int ch,pos,i; p=(node *)malloc(sizeof(node)); p->info=x; p->next=NULL; printf("Enter your choice 1.Beginning 2.End 3.Middle: "); scanf("%d",&ch); if(ch==1) { p->next=start; start=p; } else if(ch==2) { curr=start; while(curr->next!=NULL) curr=curr->next; curr->next=p; } else if(ch==3) { printf("Enter the position: "); scanf("%d",&pos); curr=start; for(i=1;i<pos;i++) { prev=curr; curr=curr->next; } prev->next=p; p->next=curr; } else printf("Entered wrong option\n"); } 8) c) /*Program to perform deletion operation on a singly linked list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h>

struct list { int info; struct list *next; }; typedef struct list node; node *start=NULL; void createlist(void); void display(void); int delet(void); void main() { int x,ch; clrscr(); createlist(); display(); do { x=delet(); printf("Deleted element: %d\n",x); display(); printf("Do you want to continue(1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); } void createlist() { node *p,*ptr; int ch; p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; start=ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); while(ch==1) { p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); }

printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; ptr->next=p; ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); } } void display() { node *ptr; if(start==NULL) { printf("Linked List is empty\n"); return; } ptr=start; printf("Elements of Linked List: "); while(ptr->next!=NULL) { printf("%d->",ptr->info); ptr=ptr->next; } printf("%d\n",ptr->info); } int delet() { node *p,*curr,*prev; int ch,pos,i,x; printf("Enter your choice to delete 1.Beginning 2.End 3.Middle: "); scanf("%d",&ch); if(ch==1) { p=start; x=start->info; start=start->next; free(p); } else if(ch==2) { curr=start; while(curr->next!=NULL) { prev=curr; curr=curr->next; } prev->next=NULL; x=curr->info; free(curr);

} else if(ch==3) { printf("Enter the position: "); scanf("%d",&pos); curr=start; for(i=1;i<pos;i++) { prev=curr; curr=curr->next; } prev->next=curr->next; x=curr->info; free(curr); } else printf("Entered wrong option\n"); return x; } 9) b) /*Program to reverse elements of a singly linked list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct list { int info; struct list *next; }; typedef struct list node; node *start=NULL; void createlist(void); void display(void); void reverselist(void); void main() { clrscr(); createlist(); display(); reverselist(); display(); getch(); } void createlist() { node *p,*ptr;

int ch; p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; start=ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); while(ch==1) { p=(node *)malloc(sizeof(node)); if(p==NULL) { printf("Out of memory"); exit(0); } printf("Enter the element: "); scanf("%d",&p->info); p->next=NULL; ptr->next=p; ptr=p; printf("Do you want to continue (1-YES/2-NO): "); scanf("%d",&ch); } } 9) c) /*Program to store a Polynomial expression using linked list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct poly { int exp; int coe; struct poly *next; }; typedef struct poly node; node *start=NULL; void createpolynomial(); void display(); void main() { clrscr();

createpolynomial(); display(); getch(); } void createpolynomial() { node *p,*ptr; int ch,count=0; do { p=(node *)malloc(sizeof(node)); printf("Enter coefficient & exponent: "); scanf("%d%d",&p->coe,&p->exp); p->next=NULL; count++; if(count==1) { start=p; ptr=p; } else { ptr->next=p; ptr=p; } printf("Do you want to enter more terms(1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); } void display() { node *ptr; ptr=start; if(ptr==NULL) printf("No Polynomial expression\n"); else { while(ptr->next!=NULL) { printf("%dX^%d+",ptr->coe,ptr->exp); ptr=ptr->next; } printf("%d\n",ptr->coe); } } 9) d) /*Program to represent the given Sparse matrix using arrays*/ #include<stdio.h> #include<conio.h>

#include<stdlib.h> void createsparse(int [][3]); void displaysparse(int [][3]); void main() { int smat[100][3]; clrscr(); createsparse(smat); displaysparse(smat); getch(); } void createsparse(int smat[][3]) { int i; printf("Enter number of rows of sparse matrix: "); scanf("%d",&smat[0][0]); printf("Enter number of columns of sparse matrix: "); scanf("%d",&smat[0][1]); printf("Enter number of nonzero elements of sparse matrix: "); scanf("%d",&smat[0][2]); for(i=1;i<=smat[0][2];i++) { printf("Enter the row number: "); scanf("%d",&smat[i][0]); printf("Enter the column number: "); scanf("%d",&smat[i][1]); printf("Enter the value: "); scanf("%d",&smat[i][2]); } } void displaysparse(int smat[][3]) { int i,t; printf("SPARSE MATRIX\n"); for(i=0;i<=smat[0][2];i++) printf("%d %d %d\n",smat[i][0],smat[i][1],smat[i][2]); printf("\n"); } 9) e) /*Program to represent the given Sparse matrix using linked list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct sparse { int row;

int col; int value; struct sparse *next; }; typedef struct sparse node; node *start=NULL; void createsparse(); void displaysparse(); void main() { clrscr(); createsparse(); displaysparse(); getch(); } void createsparse() { node *p,*curr; int r,c,v,i; printf("Enter number of rows of sparse matrix: "); scanf("%d",&r); printf("Enter number of columns of sparse matrix: "); scanf("%d",&c); printf("Enter number of nonzero elements of sparse matrix: "); scanf("%d",&v); p=(node *)malloc(sizeof(node)); p->next=NULL; p->row=r; p->col=c; p->value=v; start=p; curr=p; for(i=1;i<=v;i++) { p=(node *)malloc(sizeof(node)); printf("Enter the row number: "); scanf("%d",&p->row); printf("Enter the column number: "); scanf("%d",&p->col); printf("Enter the value: "); scanf("%d",&p->value); p->next=NULL; curr->next=p; curr=p; } } void displaysparse() {

node *curr,*prev; if(start==NULL) printf("Sparse matrix is empty\n"); else { curr=start; printf("Sparse matrix\n"); while(curr!=NULL) { printf("%d %d %d\n",curr->row,curr->col,curr->value); curr=curr->next; } } } 11) a) /*Program to create Binary Search Tree*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct tree { int info; struct tree *left; struct tree *right; }; typedef struct tree node; node *root=NULL; void create_tree(int); void display_tree(); void main() { int n; clrscr(); printf("Enter number of elements in BST: "); scanf("%d",&n); create_tree(n); display_tree(node *); getch(); } void create_tree(int n) { int i,flag; node *p,*curr,*prev; for(i=1;i<=n;i++) { p=(node *)malloc(sizeof(node)); printf("Enter element %d: ",i);

scanf("%d",&p->info); p->left=p->right=NULL; if(i==1) root=p; else { curr=root; flag=1; while(curr!=NULL) { prev=curr; if(p->info<curr->info) curr=curr->left; else if(p->info>curr->info) curr=curr->right; else { flag=0; break; } } if(flag) { if(p->info<prev->info) prev->left=p; else prev->right=p; } } } } void display_tree(node *t) { if(t==NULL) return; display_tree(t->left); printf("%d ",t->info); display_tree(t->right); } 11) b) /*Program to insert a node into Binary Search Tree*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct tree { int info; struct tree *left; struct tree *right;

}; typedef struct tree node; node *root=NULL; void insert(int); void display_tree(node *); void main() { int n,ch; clrscr(); do { printf("Enter element to be inserted into BST: "); scanf("%d",&n); insert(n); display_tree(root); printf("\nDo you want to continue(1-YES/2-NO): "); scanf("%d",&ch); }while(ch==1); getch(); } void insert(int n) { int flag; node *p,*curr,*prev; p=(node *)malloc(sizeof(node)); p->info=n; p->left=p->right=NULL; if(root==NULL) root=p; else { curr=root; flag=1; while(curr!=NULL) { prev=curr; if(p->info<curr->info) curr=curr->left; else if(p->info>curr->info) curr=curr->right; else { flag=0; free(p); break; } } if(flag) {

if(p->info<prev->info) prev->left=p; else prev->right=p; } } } void display_tree(node *t) { if(t==NULL) return; display_tree(t->left); printf("%d ",t->info); display_tree(t->right); } 11) c) /*Program to delete a node from Binary Search Tree*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> struct tree { int info; struct tree *left; struct tree *right; }; typedef struct tree node; node *root=NULL; void create_tree(int); node * delet_tree(node *,int); void display_tree(); void main() { int n,f; node *t; clrscr(); printf("Enter number of elements in BST: "); scanf("%d",&n); create_tree(n); display_tree(node *); printf("Enter element to be deleted: "); scanf("%d",&ele); t=delet_tree(ele); if(t!=NULL) display_tree(root); /* else printf("Element is not present in the BST\n");*/

getch(); } void create_tree(int n) { int i,flag; node *p,*curr,*prev; for(i=1;i<=n;i++) { p=(node *)malloc(sizeof(node)); printf("Enter element %d: ",i); scanf("%d",&p->info); p->left=p->right=NULL; if(i==1) root=p; else { curr=root; flag=1; while(curr!=NULL) { prev=curr; if(p->info<curr->info) curr=curr->left; else if(p->info>curr->info) curr=curr->right; else { flag=0; break; } } if(flag) { if(p->info<prev->info) prev->left=p; else prev->right=p; } } } } node * delet_tree(node *t,int x) { node *temp; if(t==NULL) printf("\nElement is not found in the BST\n"); else { if(x<t->info) t->left=delet_tree(t->left,x);

else if(x>t->info) t->right=delet_tree(t->right,x); } } void display_tree(node *t) { if(t==NULL) return; display_tree(t->left); printf("%d ",t->info); display_tree(t->right); } 12)a) /*Dijkstras Algorithm*/ #include<stdio.h> #include<conio.h> #define SIZE 20 int adj[SIZE][SIZE]; int length[SIZE],set[SIZE]; int path[SIZE]; int i,j,k,n; void dijkstra(int); void displaypath(int); int searchmin(); void main() { int s; clrscr(); printf("Enter the no. of vertices: "); scanf("%d",&n); printf("Enter the values of Adjacency matrix\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { // printf("From v%d to v%d: ",i,j); scanf("%d",&adj[i][j]); } printf("Adjacency Matrix\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%4d",adj[i][j]); printf("\n"); } printf("Enter source vertex: "); scanf("%d",&s);

dijkstra(s); printf("Distance matrix\n"); displaypath(s); getch(); } void displaypath(int s) { for(i=1;i<=n;i++) { if(i!=s) printf("From vertex%d to vertex%d: %d\n",s,i,length[i]); } } int searchmin() { int min=999,v; for(i=1;i<=n;i++) { if(set[i]!=1) { if(length[i]<min) { min=length[i]; k=i; } } } return k; } void dijkstra(int s) { int flag,j; /*INITIALIZATION*/ for(i=1;i<=n;i++) //Initialization of SET array set[i]=0; for(i=1;i<=n;i++) { if(adj[s][i]==0) //There is no direct path from source s to vertex i { length[i]=999; path[i]=NULL; //Empty path } else { length[i]=adj[s][i]; path[i]=s; //Source vertex is the immediate predecessor to vertex i } } set[s]=1;

length[s]=0; //Source vertex is implicitly enumerated with length as 0 /*ITERATION*/ flag=0; //flag is for controlling the iteration while(!flag) { j=searchmin(); //Find a vertex j which has min. distance among those //vetices not yet enumerated for the shortest path set[j]=1; //Vertex j is enumerated for(i=1;i<=n;i++) //For each i not yet enumerated { if(set[i]==1) //If i is enumerated already i++; //Then go to next vertex else //If i is connected to j by an edge { if(adj[i][j]!=0) { if((length[j]+adj[i][j])<length[i]) { length[i]=length[j]+adj[i][j]; path[i]=j; //Vertex j becomes the immediate predecessor of i } } } } /*To test whether all vertices are enumerated or not*/ flag=1; for(i=1;i<=n;i++) { if(set[i]==0) { flag=0; break; } else i=i+1; } } } 12)b /*Warshalls Algorithm*/ #include<stdio.h> #include<conio.h> #define SIZE 20

int adj[SIZE][SIZE]; int path[SIZE][SIZE]; int i,j,k,n;

//Adjacency Matrix //Path Matrix

void warshall(); //Function Prototype

void displaypath();

//Function Prototype

void main() { clrscr(); printf("Enter the no. of vertices: "); scanf("%d",&n); printf("Enter the values of Adjacency matrix\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) { printf("Enter from v%d to v%d: ",i+1,j+1); scanf("%d",&adj[i][j]); } printf("Adjacency Matrix\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%4d",adj[i][j]); printf("\n"); } warshall(); //Function call printf("Path matrix\n"); displaypath(); //Function call getch(); } void displaypath() //Function definition { for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%4d",path[i][j]); printf("\n"); } } void warshall() { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(adj[i][j]==0) path[i][j]=0; else path[i][j]=1; } } for(k=0;k<n;k++) { for(i=0;i<n;i++) //Function definition

{ for(j=0;j<n;j++) path[i][j]=path[i][j]|(path[i][k]&path[k][j]); } } }

Das könnte Ihnen auch gefallen