Vous êtes sur la page 1sur 49

Experiment No: Regd no:

Aim: Write a C program to implement singly linked list operations. #include <stdlib.h> #include <string.h> typedef struct node { int sid; char sname[25]; int ssem; struct node *link; }NODE; NODE *InsFront(NODE *, int, char *, int); NODE *InsBack(NODE *, int, char *, int); NODE *InsPos(NODE *, int, char *, int,int); NODE *DelNode(NODE *, int); NODE *SrchUpdate(NODE *, int); void Display(NODE *); void main() { NODE *start=NULL; /* Main Program */ int opn,id,sem,p,insopn; char name[25]; do { clrscr(); printf("\n ### Linked List Operations ### \n\n"); printf("\n Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exit\n"); printf("\n Your option ? "); scanf("%d",&opn); switch(opn) { case 1: printf("Insertion at: Press 1->Front 2->Back 3->Pos ? "); scanf("%d",&insopn); printf("\n\nRead the Sid,Name, and Sem details ?"); scanf("%d%s%d",&id,name,&sem); if( insopn == 1) start=InsFront(start,id,name,sem); else if(insopn == 2) start=InsBack(start,id,name,sem); else if(insopn == 3) { printf(" At What Position ? "); scanf("%d",&p);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

start=InsPos(start,id,name,sem,p); } break; case 2: printf(" Read the Student Id of the Node to be deleted ? "); scanf("%d",&id); start=DelNode(start,id); break; case 3: printf(" Read the Student Id of the Node to be Searched ? "); scanf("%d",&id); start=SrchUpdate(start,id); break; case 4: printf(" Linked List is \n"); Display(start); break; case 5: printf("\n\n Terminating \n\n"); break; default: printf("\n\nInvalid Option !!! Try Again !! \n\n"); break; } printf("\n\n\n\n Press a Key to Continue . . . "); getch(); }while(opn != 5); } NODE *InsFront(NODE *st, int id, char *name, int sem) { NODE *temp; temp=(NODE *)malloc(sizeof(NODE)); if( temp == NULL) { printf(" Out of Memory !! Overflow !!!"); return(st); } else { temp->sid=id; strcpy(temp->sname,name); temp->ssem=sem; temp->link=st; printf(" Node has been inserted at Front Successfully !!"); return(temp); } } NODE *InsBack(NODE *st, int id, char *name, int sem) {

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: NODE *temp,*t; temp=(NODE *)malloc(sizeof(NODE)); if( temp == NULL) { printf(" Out of Memory !! Overflow !!!"); return(st); } else { temp->sid=id; strcpy(temp->sname,name); temp->ssem=sem; temp->link=NULL; if(st == NULL) return(temp); else { t=st; while( t->link != NULL) t=t->link; t->link=temp; printf(" Node has been inserted at Back Successfully !!"); return(st); } } } NODE *InsPos(NODE *st, int id, char *name, int sem, int pos) { NODE *temp,*t,*prev; int cnt; temp=(NODE *)malloc(sizeof(NODE)); if( temp == NULL) { printf(" Out of Memory !! Overflow !!!"); return(st); } else { temp->sid=id; strcpy(temp->sname,name); temp->ssem=sem; temp->link=NULL; if(pos == 1) /* Front Insertion */ { temp->link=st; return(temp); } else { t=st; cnt=1;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: while( t != NULL && cnt != pos) { prev=t; t=t->link; cnt++; } if(t) /* valid Position Insert new node*/ { prev->link=temp; temp->link=t; } else printf(" Invalid Position !!!"); printf(" Node has been inserted at given Position Successfully !!"); return(st); } } } NODE *DelNode(NODE *st, int id) { NODE *t,*prev; if( st == NULL) { printf(" Underflow!!!"); return(st); } else { t=st; if(st->sid == id) /* Front Deletion */ { st=st->link; t->link=NULL; free(t); return(st); } else { while( t != NULL && t->sid != id) { prev=t; t=t->link; } if(t) /* node to be deleted found*/ { prev->link=t->link; t->link=NULL; free(t); } else printf(" Invalid Student Id !!!"); return(st);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } } } NODE *SrchUpdate(NODE *st, int id) { NODE *t; if( st == NULL) { printf(" Empty List !!"); return(st); } else { t=st; while( t != NULL && t->sid != id) { t=t->link; } if(t) /* node to be Updated found*/ { printf(" Node with Student Id %d found inthe List !\n",id); printf(" Read the New Id,Name and Sem forthe Student\n"); scanf("%d%s%d",t->sid,t->sname,t->ssem); } else printf(" Invalid Student Id !!!"); return(st); } } void Display(NODE *st) { NODE *t; if( st == NULL) printf("Empty List\n"); else { t=st; printf("Start->"); while(t) { printf("[%d,%s,%d]->",t->sid,t->sname,t->ssem); t=t->link; } printf("Null\n"); } } OUTPUT:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: ### Linked List Operations ###

Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exit Your option ? 1 Insertion at: Press 1->Front 2->Back 3->Pos ? 2 Read the Sid,Name, and Sem details ?5804 Balu 1 Node has been inserted at Back Successfully !! Press a Key to Continue . . . ### Linked List Operations ### Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exit Your option ? 4 Linked List is Start->[5801,anand,1]->[5802,anusha,1]->[5803,anusha,1]>[5804,Balu,1]->Null Press a Key to Continue . . . ### Linked List Operations ### Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exit Your option ? 5 Terminating Press a Key to Continue . . .

Aim: Write a C program to implement Stack using Arrays.

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: #include<stdio.h> #include<conio.h> #define MAXSIZE 10 void push(); int pop(); void traverse(); int stack[MAXSIZE]; int Top=-1; void main() { int choice; char ch; do { clrscr(); printf("\n1. PUSH "); printf("\n2. POP "); printf("\n3. TRAVERSE "); printf("\nEnter your choice"); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: printf("\nThe deleted element is %d",pop()); break; case 3: traverse(); break; default: printf("\nYou Entered Wrong Choice"); } printf("\nDo You Wish To Continue (Y/N)"); fflush(stdin); scanf("%c",&ch); } while(ch=='Y' || ch=='y'); } void push() { int item; if(Top == MAXSIZE - 1) { printf("\nThe Stack Is Full"); getch(); exit(0); } else

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { printf("Enter the inserted"); scanf("%d",&item); Top= Top+1; stack[Top] = item; } } int pop() { int item; if(Top == -1) { printf("The stack is Empty"); getch(); exit(0); } else { item = stack[Top]; Top = Top-1; } return(item); } void traverse() { int i; if(Top == -1) { printf("The Stack is Empty"); getch(); exit(0); } else { for(i=Top;i>=0;i--) { printf("Traverse the element"); printf("\n%d",stack[i]); } } } element to be

OUTPUT: 1. PUSH

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 2. POP 3. TRAVERSE Enter your choice1 Enter the element to be inserted 63 Do You Wish To Continue (Y/N)y 1. PUSH 2. POP 3. TRAVERSE Enter your choice1 Enter the element to be inserted 78 Do You Wish To Continue (Y/N)y 1. PUSH 2. POP 3. TRAVERSE Enter your choice1 Enter the element to be inserted 52 Do You Wish To Continue (Y/N)y 1. PUSH 2. POP 3. TRAVERSE Enter your choice2 The deleted element is 52 Do You Wish To Continue (Y/N)y 1. PUSH 2. POP 3. TRAVERSE Enter your choice3 Traverse the element 78Traverse the element 63 Do You Wish To Continue (Y/N)n

AIM: Write a C program to implement Stack using Linked List. #include<stdio.h>

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: #include<conio.h> struct stack { int no; struct stack *next; } *start=NULL; typedef struct stack st; void push(); int pop(); void display(); void main() { char ch; int choice,item; do { clrscr(); printf("\n 1: push"); printf("\n 2: pop"); printf("\n 3: display"); printf("\n Enter your choice"); scanf("%d",&choice); switch (choice) { case 1: push(); break; case 2: item=pop(); printf("The delete element in %d",item); break; case 3: display(); break; default : printf("\n Wrong choice"); }; printf("\n do you want to continue(Y/N)"); fflush(stdin); scanf("%c",&ch); } while (ch=='Y'||ch=='y'); } void push() { st *node; node=(st *)malloc(sizeof(st)); printf("\n Enter the number to be insert"); scanf("%d",&node->no); node->next=start;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: start=node; } int pop() { st *temp; temp=start; if(start==NULL) { printf("stack is already empty"); getch(); exit(); } else { start=start->next; free(temp); } return(temp->no); } void display() { st *temp; temp=start; while(temp->next!=NULL) { printf("\nno=%d",temp->no); temp=temp->next; } printf("\nno=%d",temp->no); }

OUTPUT: 1: push

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 2: pop 3: display Enter your choice1 Enter the number to be insert45 do you want to continue(Y/N)y 1: push 2: pop 3: display Enter your choice1 Enter the number to be insert13 do you want to continue(Y/N)y 1: push 2: pop 3: display Enter your choice2 The delete element in 13 do you want to continue(Y/N)y 1: push 2: pop 3: display Enter your choice3 no=45 do you want to continue(Y/N)n

AIM: Write a C Program to implement Queue using Arrays. #include<stdio.h> #include<conio.h> void main()

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { int q[10]={0},i,front=1,rear=1,max=10,n,item; clrscr(); printf("\n\tMENU\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n"); do { printf("\nEnter your choice\n"); scanf("%d",&n); switch(n) { case 1: if(rear<max-1) { printf("Enter the element\n"); scanf("%d",&item); if(rear==-1) { front=0; rear=0; q[rear]=item; } else q[++rear]=item; } else printf("Overflow\n"); break; case 2: if(front>=0) { printf("The deleted item =%d",q[front]); if(front==rear) { front=-1; rear=-1; } else front++; } else printf("Underflow\n"); break; case 3: if((front==-1)&&(rear==-1))

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: printf("The queue is else { printf("The elements of the queue are :"); for(i=front;i<=rear;i++) printf("%d\t",q[i]); } break; case 4: break; default: printf("Invalid choice\n"); break; } } while(n!=4); getch(); } empty\n");

OUTPUT: MENU 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: Enter your choice 1 Enter the element 25 Enter your choice 1 Enter the element 46 Enter your choice 1 Enter the element 78 Enter your choice 3 The elements of the queue are :25 Enter your choice 2 The deleted item =25 Enter your choice 4

46

78

AIM: Write a C program to implement Queue Using Linked List. #include<stdio.h> #include<conio.h> struct queue { int no; struct queue *next;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } *start=NULL; void add(); int del(); void traverse(); void main() { int ch; char choice; do { clrscr(); printf("----1. add\n"); printf("----2. delete\n"); printf("----3. traverse\n"); printf("----4. exit\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: add(); break; case 2: printf("the delete element is\n%d",del()); break; case 3: traverse(); break; case 4: return; default : printf("wrong choice\n"); }; fflush(stdin); scanf("%c",&choice); } while(choice!=4); } void add() { struct queue *p,*temp; temp=start; p=(struct queue*)malloc(sizeof(struct queue)); printf("Enter the data"); scanf("%d",&p->no); p->next=NULL; if(start==NULL) { start=p; } else

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { while(temp->next! { temp=temp->next; } temp->next=p; } } int del() { struct queue *temp; int value; if(start==NULL) { printf("queue is empty"); getch(); return(0); } else { temp=start; value=temp->no; start=start->next; free(temp); } return(value); } void traverse() { struct queue *temp; temp=start; =NULL)

while(temp->next!=NULL) { printf("no=%d",temp->no); temp=temp->next; } printf("no=%d",temp->no); getch(); }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

OUTPUT: ----1. add ----2. delete ----3. traverse ----4. exit Enter your choice 1 Enter the data56 ----1. add ----2. delete

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: ----3. traverse ----4. exit Enter your choice 1 Enter the data78 ----1. add ----2. delete ----3. traverse ----4. exit Enter your choice 3 no=56no=78 ----1. add ----2. delete ----3. traverse ----4. exit Enter your choice 2 the delete element is 56 ----1. add ----2. delete ----3. traverse ----4. exit Enter your choice 4

Aim: Write a C program to implement Bubble Sort. #include<stdio.h> #include<conio.h> void Bubblesort(int a[], int n) { int i,j,temp; i=0; while(i<n) { j=n-1; Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

while(j>i) { if(a[j]<a[j-1]) { temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } j--; } i++; } printf("Sorted elements in the array are:\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); } int main() { int n,i,a[50]; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); Bubblesort(a,n); getch(); return 0; }

OUTPUT:
How many elements 5 Enter the element of array 36 45 2 33 12 Element of array after the sorting are: 2 12 33 36

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 45

Aim: Write a C program to implement Selection Sort.


#include<stdio.h> #include<conio.h> void straightselectionsort(int a[], int n) { int i,j,temp; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i];

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: a[i]=a[j]; a[j]=temp; } } } printf("Sorted elements in the list are:\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); } int main() { int a[10],n,i; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); straightselectionsort(a,n); getch(); return 0; }

OUTPUT: Enter the size of the array 5 Enter the elements into the array 45 30 15 89 20 Sorted elements in the list are: 15 20 30

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 45 89

Aim: Write a C program to implement Quick Sort.


#include<stdio.h> #include<conio.h> #define max 100 int a[max],n,i,l,h; void main() { void input(void); input(); getch(); } void input(void) { void output(int a[],int n); void quick_sort(int a[],int l,int h);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: printf("How many "); scanf("%d",&n); printf("\n"); printf("Enter the elemennts : \n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } l=0; h=n-1; quick_sort(a,l,h); printf("Sorted Array :\n "); output(a,n); } void quick_sort(int a[],int l, int h) { int temp,key,low,high; 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) quick_sort(a,l,high); if(low<h) quick_sort(a,low,h); } void output(int a[],int n) { for(i=0;i<=n-1;i++) { elements in the array :

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: printf("%d\n",a[i]); } }

OUTPUT: How many elements in the array : 5 Enter the elemennts : 45 96 33 21 47 Sorted Array : 21 33 45 47 96 How many elements in the array :

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Aim: Write a C program to implement Merge Sort.


#include<stdio.h> #include<conio.h> void main( ) { int n,m,i,j,k,c[40],a[20],b[20]; clrscr (); printf("Enter limit for A:"); scanf("%d",&n); printf ("\nEnter limit for B:"); scanf("%d",&m); printf("Enter elements for A:-\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter elements for B:-\n"); for(j=0;j<m;j++) scanf("%d",&b[j]); i=j=k=0;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: while(i<n&&j<m) { if(a[i]<b[j]) c[k++]=a[i++]; else if(a[i]>b[j]) c[k++]=b[j++]; else { c[k++]=b[j++]; i++; j++; } } if(i<n) { int t; for(t=0;t<n;t++) c[k++]=a[i++]; } if(j<m) { int t; for(t=0;t<m;t++) { c[k++]=b[j++]; } } printf("\n"); for(k=0;k<(m+n);k++) printf("\t \n %d ",c[k]); getch(); }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

OUTPUT: Enter limit for A:5 Enter limit for B:5 Enter elements for A:45 63 89 44 12 Enter elements for B:24 36 58 69 74 24

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 36 45 58 63 69 74 89

Aim: Write a C program to implement Insertion Sort.


#include<stdio.h> int main(){ int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=1;i<s;i++){ temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)){ a[j+1]=a[j];

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; }

OUTPUT: Enter total elements: 5 Enter 5 elements: 45 32 15 89 78 After sorting: 15 32 45 78 89

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Aim: Write a C program to implement Binary Search Technique. #include<stdio.h> #include<conio.h> void main() { int list[10],key,found,num,i; int low,high,mid; clrscr(); printf("\n enter the max no.of elements u want to insert \n"); scanf("%d",&num); printf("\n enter the elements u want to search \n"); for(i=0;i<num;i++) { scanf("%d",&list[i]); } printf("enter the value to be searched"); scanf("%d",&key); low=0; high=num-1; while(low<=high) { mid=(low+high)/2;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: if(key==list[mid]) { printf("search is printf("\n the element %d is found ",list[mid]); found=1; break;} if(key<list[mid]) high=mid-1; else if(key>list[mid]) low=mid+1; } if(found!=1) printf("seach is unsuccessful"); getch(); }

successful");

Output:

enter the max no.of elements u want to insert 5 enter the elements u want to search 45 32 10 22 63 enter the value to be searched63 search is successful the elemnt is 63 is found

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Aim: Write a C Program to implement Heap Sort. #include<stdio.h> #include<conio.h> int hsort[25],n,i; void adjust(int,int); void heapify(); void main() { int temp; clrscr(); printf("\n\t\t\t\tHEAP SORT"); printf("\n\t\t\t\t**** ****\n\n\n"); printf("\nenter no of elements:"); scanf("%d",&n); printf("\nenter elements to be sorted\n\n"); or(i=1;i<=n;i++) scanf("%d",&hsort[i]); heapify(); for(i=n;i>=2;i--) { temp=hsort[1]; hsort[1]=hsort[i]; hsort[i]=temp; adjust(1,i-1);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } printf("\nSORTED for(i=1;i<=n;i++) printf("%d\n",hsort[i]); getch(); } void heapify() { int i; for(i=n/2;i>=1;i--) adjust(i,n); } void adjust(int i,int n) { int j,element; j=2*i; element=hsort[i]; while(j<=n) { if((j<n)&&(hsort[j]<hsort[j+1])) j=j++; if(element>=hsort[j]) break; hsort[j/2]=hsort[j]; j=2*j; } hsort[j/2]=element; } ELEMENT\n\n");

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

OUTPUT: HEAP SORT **** ****

enter no of elements:5 enter elements to be sorted 45 89 63 12 30 SORTED ELEMENT 12 30 45 63 89

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Aim: write a C program to implement hashing with open


addressing (Linear Probing). #include<stdio.h> #include<conio.h> #include<stdlib.h> #define max 10 void main() { int a[max],num,key,i; char ans; int create(int); void hash(int [],int,int); void display(int[]); clrscr(); printf("\nhash table\n"); for(i=0;i<max;i++) a[i]=-1; do { printf("\n enter the number:"); scanf("%d",&num); key=create(num); hash(a,key,num); printf("\ndo u want to continue"); ans=getche(); }while(ans=='y');

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: display(a); getch(); } int create(int num) { int key; key=num % 10; return key; } void hash(int a[max],int key,int num) { int flag,i,count=0; void display(int a[]); flag=0; if(a[key]==-1) a[key]=num; else { i=0; while(i<max) { if(a[i]!= -1) count++; i++; } if(count==max) { printf("\n hashtable is full"); display(a); getch(); exit(1); } for(i=key+1;i<max;i++) if(a[i]==-1) { a[i]=num; flag=1; break; } for(i=0;i<key && flag==0;i++) if(a[i]==-1) { a[i]=num; flag=1; break;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } } } void display(int a[max]) { int i; printf("\nThe hash table is..........\n"); for(i=0;i<max;i++) printf("\n%d %d",i,a[i]); }

OUTPUT: hash table enter the number:131 do u want to continue y enter the number:21 do u want to continue y enter the number:3 do u want to continue y enter the number:4 do u want to continue y enter the number:5 do u want to continue n The hash table is.......... 0 1 2 3 4 5 6 7 -1 131 21 3 4 5 -1 -1

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 8 9 -1 -1

Aim: Write a C Program to Perform Operations on AVL tree. #include<stdio.h> #include<conio.h> typedef enum { FALSE ,TRUE } bool; struct node { int info; int balance; struct node *lchild; struct node *rchild; }; struct node *insert (int , struct node *, int *); struct node* search(struct node *,int); main() { bool ht_inc; int info ; int choice; struct node *root = (struct node *)malloc(sizeof(struct node)); root = NULL; clrscr(); while(1) { printf("1.Insert\n"); printf("2.Display\n");

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: printf("3.Quit\n"); printf("Enter your choice scanf("%d",&choice); switch(choice) { case 1: printf("Enter the value to be inserted : "); scanf("%d", &info); if( search(root,info) == NULL ) root = insert(info, root, &ht_inc); else printf("Duplicate value ignored\n"); break; case 2: if(root==NULL) { printf("Tree is empty\n"); continue; } printf("Tree is :\n"); display(root, 1); printf("\n\n"); printf("Inorder Traversal is: "); inorder(root); printf("\n"); break; case 3: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ struct node* search(struct node *ptr,int info) { if(ptr!=NULL) if(info < ptr->info) ptr=search(ptr->lchild,info); else if( info > ptr->info) ptr=search(ptr->rchild,info); return(ptr); }/*End of search()*/ struct node *insert (int info, struct node *pptr, int *ht_inc) { struct node *aptr; : ");

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: struct node *bptr; if(pptr==NULL) { pptr = (struct node *) malloc(sizeof(struct node)); pptr->info = info; pptr->lchild = NULL; pptr->rchild = NULL; pptr->balance = 0; *ht_inc = TRUE; return (pptr); } if(info < pptr->info) { pptr->lchild = insert(info, pptr->lchild, ht_inc); if(*ht_inc==TRUE) { switch(pptr->balance) { case -1: /* Right heavy */ pptr->balance = 0; *ht_inc = FALSE; break; case 0: /* Balanced */ pptr->balance = 1; break; case 1: /* Left heavy */ aptr = pptr->lchild; if(aptr->balance == 1) { printf("Left to Left Rotation\n"); pptr->lchild= aptr->rchild; aptr->rchild = pptr; pptr->balance = 0; aptr->balance=0; pptr = aptr; } else { printf("Left to right rotation\n"); bptr = aptr->rchild; aptr->rchild = bptr->lchild; bptr->lchild = aptr; pptr->lchild = bptr->rchild; bptr->rchild = pptr;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: if(bptr->balance == 1 ) pptr->balance = -1; else pptr->balance = 0; if(bptr->balance == -1) aptr->balance = 1; else aptr->balance = 0; bptr->balance=0; pptr=bptr; } *ht_inc = FALSE; }/*End of switch */ }/*End of if */ }/*End of if*/ if(info > pptr->info) { pptr->rchild = insert(info, pptr->rchild, ht_inc); if(*ht_inc==TRUE) { switch(pptr->balance) { case 1: /* Left heavy */ pptr->balance = 0; *ht_inc = FALSE; break; case 0: /* Balanced */ pptr->balance = -1; break; case -1: /* Right heavy */ aptr = pptr->rchild; if(aptr->balance == -1) { printf("Right to Right Rotation\n"); pptr->rchild= aptr->lchild; aptr->lchild = pptr; pptr->balance = 0; aptr->balance=0; pptr = aptr; } else { printf("Right to Left Rotation\n"); bptr = aptr->lchild; aptr->lchild = bptr->rchild; bptr->rchild = aptr;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: pptr->rchild = bptrbptr->lchild = pptr; if(bptr->balance == -1) pptr->balance = 1; else pptr->balance = 0; if(bptr->balance == 1) aptr->balance = -1; else aptr->balance = 0; bptr->balance=0; pptr = bptr; }/*End of else*/ *ht_inc = FALSE; }/*End of switch */ }/*End of if*/ }/*End of if*/ return(pptr); }/*End of insert()*/ display(struct node *ptr,int level) { int i; if ( ptr!=NULL ) { display(ptr->rchild, level+1); printf("\n"); for (i = 0; i < level; i++) printf(" "); printf("%d", ptr->info); display(ptr->lchild, level+1); }/*End of if*/ }/*End of display()*/ inorder(struct node *ptr) { if(ptr!=NULL) { inorder(ptr->lchild); printf("%d ",ptr->info); inorder(ptr->rchild); } }/*End of inorder()*/ >lchild;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

OUTPUT: 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted Right to Right Rotation 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted 1.Insert 2.Display 3.Quit Enter your choice : 2 Tree is : 45 36 33 25 Inorder Traversal is: 25 33 36 45

: 25

: 36

: 45

: 33

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 1.Insert 2.Display 3.Quit Enter your choice :3

Aim: Write a C program to Perform Operations On Binary Search Tree. #include <stdlib.h> typedef struct tnode { int data; struct tnode *right, *left; } TNODE; TNODE *CreateBST(TNODE *, int); void Inorder(TNODE *); void Preorder(TNODE *); void Postorder(TNODE *); void main() { TNODE *root = NULL; /* Main Program */ int opn, elem, n, i; do { clrscr(); printf("\n ### Binary Search Tree Operations ### \n\n"); printf("\n Press 1-Creation of BST"); printf("\n 2-Traverse in Inorder"); printf("\n 3-Traverse in Preorder"); printf("\n 4-Traverse in Postorder"); printf("\n 5-Exit\n"); printf("\n Your option ? "); scanf("%d", &opn); switch (opn) { case 1: root = NULL; printf("\n\nBST for How Many Nodes ?"); scanf("%d", &n); for (i = 1; i <= n; i++) { printf("\nRead the Data for Node %d ?", i); scanf("%d", &elem); root = CreateBST(root, elem);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } printf("\nBST with %d Use!!\n", n); break; case 2: printf("\n BST Traversal in INORDER \n"); Inorder(root); break; case 3: printf("\n BST Traversal in PREORDER \n"); Preorder(root); break; case 4: printf("\n BST Traversal in POSTORDER \n"); Postorder(root); break; case 5: printf("\n\n Terminating \n\n"); break; default: printf("\n\nInvalid Option !!! Try Again !! \n\n"); break; } printf("\n\n\n\n Press a Key to Continue . . . "); getch(); } while (opn != 5); } TNODE *CreateBST(TNODE *root, int elem) { if (root == NULL) { root = (TNODE *) malloc(sizeof(TNODE)); root->left = root->right = NULL; root->data = elem; return root; } else { if (elem < root->data) root->left = CreateBST(root->left, elem); else if (elem > root->data) root->right = CreateBST(root->right, elem); else printf(" Duplicate Element !! Not Allowed !!!"); return (root); } } void Inorder(TNODE *root) { if (root != NULL) { nodes is ready to

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: Inorder(root->left); printf(" %d ", rootInorder(root->right); } } >data);

void Preorder(TNODE *root) { if (root != NULL) { printf(" %d ", root->data); Preorder(root->left); Preorder(root->right); } } void Postorder(TNODE *root) { if (root != NULL) { Postorder(root->left); Postorder(root->right); printf(" %d ", root->data); } }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

OUTPUT:

### Binary Search Tree Operations ### Press 1-Creation of BST 2-Traverse in Inorder 3-Traverse in Preorder 4-Traverse in Postorder 5-Exit Your option ? 1 BST for How Many Nodes ?3 Read the Data for Node 1 ?25 Read the Data for Node 2 ?36 Read the Data for Node 3 ?44 BST with 3 nodes is ready to Use!! ### Binary Search Tree Operations ### Press 1-Creation of BST 2-Traverse in Inorder 3-Traverse in Preorder 4-Traverse in Postorder 5-Exit Your option ? 2 BST Traversal in INORDER

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 25 36 44 Press a Key to ### Binary Search Tree Operations ### Press 1-Creation of BST 2-Traverse in Inorder 3-Traverse in Preorder 4-Traverse in Postorder 5-Exit Your option ? 3 BST Traversal in PREORDER 25 36 44 Press a Key to Continue . . . ### Binary Search Tree Operations ### Press 1-Creation of BST 2-Traverse in Inorder 3-Traverse in Preorder 4-Traverse in Postorder 5-Exit Your option ? 4 BST Traversal in POSTORDER 44 36 25 Press a Key to Continue . . . ### Binary Search Tree Operations ### Press 1-Creation of BST 2-Traverse in Inorder 3-Traverse in Preorder 4-Traverse in Postorder 5-Exit Your option ? 5 Terminating Press a Key to Continue . . . Continue . . .

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology