0% found this document useful (0 votes)
106 views60 pages

Dhamu - All Progromes Batch-1

The document describes a C program to add two polynomials represented as singly linked lists. It defines a polynomial node structure with coefficient and exponent fields. Functions are included to create, display, add and get nodes for the polynomial linked lists. The main function gets the two polynomials as linked lists, displays them, and calls the add function to sum the polynomials term by term and return the resulting linked list.

Uploaded by

Dhamo Daran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views60 pages

Dhamu - All Progromes Batch-1

The document describes a C program to add two polynomials represented as singly linked lists. It defines a polynomial node structure with coefficient and exponent fields. Functions are included to create, display, add and get nodes for the polynomial linked lists. The main function gets the two polynomials as linked lists, displays them, and calls the add function to sum the polynomials term by term and return the resulting linked list.

Uploaded by

Dhamo Daran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

Ex.No: 1a DATE: 15.07.11 SINGLY LINKED LIST PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.

h> #define TRUE 1 #define FALSE 0 typedef struct SLL { int data; struct SLL*next; } node; node *create(); void main() { int choice, val; char ans; node *head; void display(node *); node *search(node*,int); void insert(node*); void delete(node**); head=NULL; do { clrscr(); printf("\n Program to perform various operationS on linked list"); printf("\n 1.Create"); printf("\n 2.Display"); printf("\n 3.Search for an item:"); printf("\n 4. Insert an element in a list"); printf("\n 5. Delete an element from list"); printf("\n 6. Quit"); printf("\n Enter the choice (1-6):\n"); scanf("%d",&choice); switch(choice) { case 1: head=create(); break; case 2:

display(head); break; case 3: printf("\n Enter the element you want to search "); scanf("%d",&val); search(head,val); break; case 4: insert(head); break; case 5: delete(&head); break; case 6: exit(0); default: clrscr(); printf("\n Invalid choice, try again"); getch(); } } while(choice!=6); } node *create() { node *temp,*New,*head; int val,flag; char ans='y'; node*get_node(); temp=NULL; flag=TRUE; do { printf("\n Enter the element:"); scanf("%d",&val); New=get_node(); if(New==NULL) printf("\n Memory is not allocated"); New->data=val; if(flag) { head=New; temp=head; flag=FALSE; } else

{ temp->next=New; temp=New; } printf("\n Do you want to enter more elements? (y/n)"); ans=getch(); } while(ans=='y'); printf("\n The singly linked list is expected \n"); getch(); clrscr(); return head; } node *get_node() { node *temp; temp=(node*)malloc(sizeof(node)); temp->next=NULL; return temp; } void display(node *head) { node *temp; temp=head; if(temp==NULL) { printf("\n The list is empty\n"); getch(); clrscr(); return; } while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("NULL"); getch(); clrscr(); } node *search(node*head,int key) { node*temp; int found; temp=head; if(temp==NULL)

{ printf("\n The linkage list is empty\n"); getch(); clrscr(); return NULL; } else found=FALSE; while(temp!=NULL&&!found) { if(temp->data!=key) temp=temp->next; else found=TRUE; } if(found) { printf("\n The element is present in the list\n"); getch(); return temp; } else { printf("The element is not present in the list\n"); getch(); return NULL; } } void insert(node *head) { node *temp,*New; int val; temp=head; if(temp==NULL) { printf("\n Insertion is not possible"); getch(); return; } clrscr(); printf("\n Enter the element after which you want to insert"); scanf("%d",&val); temp=search(head,val); if(temp!=NULL) { printf("\n Enter the element which you want to insert:");

scanf("%d",&val); New=(node*)malloc(sizeof(node)); if(New==NULL) printf("\n Memory is not allocated \n"); New->data=val; New->next=NULL; New->next=temp->next; temp->next=New; printf("\n The element is inserted\n"); getch(); clrscr(); }} node *get_prev(node *head,int val) { node *temp,*prev; int flag; temp=head; if(temp==NULL) return 0; flag=FALSE; prev=NULL; while(temp!=NULL&&!flag) { if(temp->data!=val) { prev=temp; temp=temp->next; } else flag=TRUE; } if(flag) return prev; else return NULL; } void delete(node**head) { node*temp,*prev; int key; temp=*head; if(temp==NULL) { printf("\n The list is empty"); getch(); clrscr();

return; } clrscr(); printf("\n Enter the element you wnat to delete"); scanf("%d",&key); temp=search(*head,key); if(temp!=NULL) { prev=get_prev(*head,key); if(prev!=NULL) { prev->next=temp->next; free(temp); } else { *head=temp->next; free(temp); } printf("\n The element is deleted\n"); getch(); clrscr(); } } OUTPUT:Program to perform various operations on linked list 1. Create 2. Display 3. Search for an item: 4. Insert an element in a list 5. Delete an element from list 6. Quit Enter the choice (1-6): 1 Enter the element: 12 Do you want to enter more elements?(y/n) y Enter the element: 15 Do you want to enter more elements? (y/n) y

Enter the element: 32 Do you want to enter more elements? (y/n) n The singly linked list is expected Enter the choice (1-6): 2 12->15->32->NULL Enter the choice (1-6): 3 Enter the element you want to search 12 The element is present in the list Enter the choice (1-6): 4 Enter the element after which you want to insert 32 The element is present in the list Enter the element which you want to insert: 44 The element is inserted Enter the choice (1-6): 2 12->15->32->44->NULL Enter the choice (1-6): 5 Enter the element you want to delete: 32 The element is present in the list The element is deleted Enter the choice (1-6): 2 12->15->44->NULL

EX.NO:1b DATE: 15.7.11 DOUBLY LINKED LIST PROGRAM: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node *next,*prev; }*New,*New1,*temp,*start,*dummy; void add(void); struct node *get_node(); void display(void); void delet(void); int find(int); int first=1; void main() { char ans; int choice,num,found=0; start=NULL; do{ clrscr(); printf("\n\t Program for doubly linked list\n"); printf("\n\n 1.Insertion of element \n\n"); printf("\n\n 2. Deletion of element from the list\n\n"); printf("\n\n3. Display of doubly linked list\n\n"); printf("\n\n 4. Searching of a particular node in the list\n\n"); printf("\n\n5. Exit\n"); printf("\n Enter the choice"); scanf("%d",&choice); switch(choice) { case 1: add(); break; case 2: delet(); break; case 3: display(); break;

case 4: printf("\n Enter the number which is to be searched"); scanf("%d",&num); temp=start; while((temp!=NULL)&&(found==0)) found=find(num); if(found) printf("\n The number is present in the list"); else printf("\n The number is NOT present in the list"); break; case 5: exit(0); } printf("\n Do you want to continue? \n"); ans=getch(); } while(ans=='y'||ans=='Y'); getch(); } void add(void) { clrscr(); New=get_node(); printf("\n\n Enter the element \n"); scanf("%d",&New->data); if(first==1) { start=New; first=0; } else { dummy=start; while(dummy->next!=NULL) dummy=dummy->next; dummy->next=New; New->prev=dummy; }} struct node*get_node() { New1=malloc(sizeof(struct node)); New1->next=NULL; New1->prev=NULL; return(New1); }

void display(void) { clrscr(); temp=start; if(temp==NULL) printf("\n The doubly linked list is empty"); else { while(temp!=NULL) { printf("\n\n%d->",temp->data); temp=temp->next; } printf("NULL"); } getch(); } int find(int num) { if(temp->data==num) return(1); else temp=temp->next; return 0; } void delet(void) { int num,flag=0; int found; int last=0; clrscr(); temp=start; if(temp==NULL) printf("\n\nSorry:DLL is not created\n"); else{ printf("Enter the number to be deleted"); scanf("%d",&num); while((flag==0)&&(temp!=NULL)) { found=find(num); flag=found; } if(found==0) printf("\n Number not found"); else {

if(temp==start) { start=start->next; temp->next=NULL; start->prev=NULL; free(temp); getch(); printf("\n The starting node is deleted"); } else { if(temp->next==NULL) last=1; else last=0; (temp->next)->prev=temp->prev; (temp->prev)->next=temp->next; temp->prev=NULL; temp->next=NULL; free(temp); if(last) printf("\n The last nod is deleted"); else printf("\n The intermediate nod is deleted"); } } } }

OUTPUT:Program for doubly linked list:1. Insertion of element 2. Deletion of element from the list 3. Display of doubly linked list 4. Searching of a particular node in the list 5. Exist Enter the choice: 1 Enter the element 2 Do you want to continue? Y Enter the choice 1 Enter the element 5 Do you want to continue? Y Enter the choice 3 5->NULL 2->NULL Do you want to continue? Y Enter the choice 4 Enter the number which is to be searched 2 The number is present in the list Do you want to continue? Y Enter the choice 2 Enter the number to be deleted 2 The starting node is deleted Do you want to continue? Y Enter the choice 5 Quit

EX.NO:2 DATE: 22.7.11 ADDITION OF POLYNOMIAL PROGRAM: #include<stdio.h> #include<conio.h> #include<process.h> #include<alloc.h> #include<stdlib.h> #define TRUE 1 #define FLASE 0 typedef struct pnode { float coef; int exp; struct pnode *next; }p; void main() { p*p1,*p2,*p3; p*get_poly(),*add(p*,p*); void display(p*); clrscr(); printf("\n enter the first polynomial\n\n"); p1=get_poly(); clrscr(); printf("\n enter the second polynomial\n\n"); p2=get_poly(); clrscr(); printf("\n the first polynomial is:\n"); display(p1); printf("\n the second polynomial is :\n"); display(p2); p3=add(p1,p2); printf("\n the addition of the polynomial is :\n"); display(p3); exit(0); } p*get_poly() { p*temp,*New,*last; int exp,flag; float coef; p*get_node(); char ans='y';

temp=NULL; flag=TRUE; printf("\n enter the polynomial is desending order of exponent \n"); do { printf("\n enter the co-efficient and exponent of atom:"); scanf("%f%d",&coef,&exp); New=get_node(); if(New==NULL) printf("\n memory cannot be allocated"); New->coef=coef; New->exp=exp; if(flag==1) { temp=New; last=temp; flag=FLASE; } else { last->next=New; last=New; } printf("\n do you want to add more terms?(y/n)"); ans=getch(); } while(ans=='y'); return(temp); } p*get_node() { p*temp; temp=(p*)malloc(sizeof(p)); temp->next=NULL; return(temp); } void display(p*head) { p*temp; temp=head; if(temp==NULL) { printf("\n the polynomial is empty \n"); getch(); return; }

printf("\n"); while(temp->next!=NULL) { printf("%0.1fx^%d+",temp->coef,temp->exp); temp=temp->next; } printf("%0.1fx^%d",temp->coef,temp->exp); getch(); } p*add(p*first,p*second) { p*p1,*p2,*temp,*dummy; char ch; float coef; p*append(int,float,p*); p1=first; p2=second; temp=(p*)malloc(sizeof(p)); if(temp==NULL) printf("\n memory cannot be allocated"); dummy=temp; while(p1!=NULL&&p2!=NULL) { if(p1->exp==p2->exp) { coef=p1->coef+p2->coef; temp=append(p1->exp,coef,temp); p1=p1->next; p2=p2->next; } else if (p1->exp<p2->exp) { coef=p2->coef; temp=append(p2->exp,coef,temp); p2=p2->next; } else if(p1->exp>p2->exp) { coef=p1->coef; temp=append(p1->exp,coef,temp); p1=p1->next; } } while(p1!=NULL) { temp=append(p1->exp,p2->coef,temp);

p1=p1->next; } while(p2!=NULL) { temp=append(p2->exp,p2->coef,temp); p2=p2->next; } temp->next=NULL; temp=dummy->next; free(dummy); return(temp); } p * append(int exp,float coef,p*temp) { p*New,*dummy; New=(p*)malloc(sizeof(p)); if(New==NULL) printf("\n memory cannot be allocated\n"); New->exp=exp; New->coef=coef; New->next=NULL; dummy=temp; dummy->next=New; dummy=New; return(dummy); }

OUTPUT:Enter the first polynomial Enter the polynomial in descending order of exponent Enter the coefficient and exponent of a term :5 2 Do you want to add more terms?????(y/n)y Enter the coefficient and exponent of a term :2 1 Do you want to add more terms?????(y/n)y Enter the coefficient and exponent of a term :7 0 Do you want to add more terms?????(y/n)n

Enter the Second polynomial Enter the polynomial in descending order of exponent Enter the coefficient and exponent of a term :11 2 Do you want to add more terms?????(y/n)y Enter the coefficient and exponent of a term :6 1 Do you want to add more terms?????(y/n)y Enter the coefficient and exponent of a term :3 0 Do you want to add more terms?????(y/n)n

The first polynomial is: 5.0x^2+2.0x^1+7.0x^0 The second polynomial is: 11.0x^2+6.0x^1+3.0x^0 The addition of the polynomial is: 16.0x^2+8.0x^1+10.0x^0

EX NO:3 IMPLEMENTATION OF STACK AND USE IT TO CONVERT DATE:29.7.2011 INFIX TO POSTFIX EXPRESSION

PROGRAM: #include<stdio.h> #include<conio.h> #include<alloc.h> char inf[40],post[40]; int top=0,st[20]; void postfix(); void push (int); char pop(); void main (void) { clrscr(); printf("\n\t Enter the infix expression :\n\n\t\t"); scanf("%s",inf); postfix(); getch(); } void postfix() { int i,j=0; for(i=0;inf[i]!='\0';i++) { switch (inf[i]) { case '+': while (st[top]>=1) post[j++]=pop(); push(1); break; case '-': while(st[top]>=1) post[j++]=pop(); push(2); break; case'*': while(st[top]>=3) post[j++]=pop(); push(3); break; case'/': while(st[top]>=3)

post[j++]=pop(); push(4); break; case'^': while(st[top]>=4) post[j++]=pop(); push(5); break; case '(': push(0); break; case ')': while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=inf[i]; } } while(top>0) post[j++]=pop(); printf("\n\tpostfix expression is :\n\n\t\t%s",post); } void push(int ele) { top++; st[top]=ele; } char pop() { int el; char e; el=st[top]; top--; switch(el) { case 1: e='+'; break; case 2: e='-'; break; case 3:

e='*'; break; case 4: e='/'; break; case 5: e='^'; break; } return (e); }

OUTPUT:

Enter the infix expression : a+b*c/d postfix expression is : abc*d/+

EX.NO.: 4 DATE: 29.07.11 CIRCUL QUEUE IMPLEMENTATION PROGRAM: #include<stdio.h> #define qsize 50 int qu[qsize]; int front,rear; void insert() { int item; if(rear==qsize) rear=1; else rear++; if(front!=rear) { printf("Enter items to be inserted\n"); scanf("%d",&item); qu[rear]=item; if(front==0) front++; } else printf("ovear flow\n"); return; } int delete() { int item; if(front!=0) { item=qu[front]; if(front==rear) { front=rear=0; return(item); } if(front==qsize) front=1; else front++; return(item); } printf("Under Flow\n");

return(0); } void display() { int i; for(i=front;i<=rear;i++) printf("%d",qu[i]); printf("\n"); return; } void main() { int i,j,k,item,choice; void insert(); int delete(); void display(); front=rear=0; do { printf("1.Insert an Element into shop by Producer\n"); printf("2.get an element from the shop by consumer\n"); printf("3.quit"); scanf("%d",&choice); switch(choice) { case 1: insert(); printf("cqueue after insertion is:\n"); display(); printf("\n"); break; case 2: item=delete(); printf("item deleted id :%d",item); printf("cqueue after deletion is:\n"); display(); printf("\n"); break; default:break; }} while(choice!=3); }

OUTPUT: 1.Insert an Element into shop by Producer 2.get an element from the shop by consumer 3.quit 1 Enter items to be inserted 25 cqueue after insertion is: 25 1 Enter items to be inserted 50 cqueue after insertion is: 2550 2 item deleted id :25cqueue after deletion is: 50 3

EX.NO.: 5 DATE: 05.08.11. EXPRESSION TREE PROGRAM: #include<stdio.h> #include<alloc.h> #include<conio.h> typedef struct bin { int data; struct bin*left; struct bin*right; }node; void insert(node*,node*); void inorder(node*); void preorder(node*); void postorder(node*); node* get_node(); void main() { int choice; char ans='n'; node*New,*root; root=NULL; clrscr(); do { printf("\n Program for implementing simple binary tree"); printf("\n 1.Create"); printf("\n 2.Inorder"); printf("\n 3.Preorder"); printf("\n 4.Postorder"); printf("\n 5. exit"); printf("\n\t Enter the choice:"); scanf("%d",&choice); switch(choice) { case 1:root=NULL; do { New=get_node(); printf("\n Enter the element:"); scanf("%d", &New->data); if(root==NULL) root=New;

else insert(root,New); printf("\n Do you want to enter more element: (y/n)"); ans=getch();} while(ans=='y'||ans=='Y'); clrscr(); break; case 2: if(root==NULL) printf("tree is not created"); else inorder(root); break; case 3: if(root==NULL) printf("Tree is not created"); else preorder(root); break; case 4:if(root==NULL) printf("\n Tree is not created!"); else postorder(root); break; } } while(choice!=5); } node*get_node() { node*temp; temp=(node*)malloc(sizeof(node)); temp->left=NULL; temp->right=NULL; return temp; } void insert(node*root,node*New) { char ch; printf("\n Where to insert left or right of %d",root->data); ch=getch(); if((ch=='r')||(ch=='R')) { if(root->right==NULL) { root->right=New;} else {

if(root->left==NULL) { root->left=New;} else insert(root->left,New); } } void inorder(node*temp) { if(temp!=NULL) { inorder(temp->left); printf("\n \t %d",temp->data); inorder(temp->right);}} void preorder(node*temp) { if(temp!=NULL) { printf("\t %d",temp->data); preorder(temp->left); preorder(temp->right);}} void postorder(node*temp) { if(temp!=NULL) { postorder(temp->left); postorder(temp->right); printf("\n\t%d",temp->data); } getch(); }

OUTPUT:

Program for Implementing simple Binary Tree 1.Create 2.Inorder 3.Preorder 4.postorder 5.exit Enter the choice1 Enter the element 50 Do you want to Enter more element?(y/n) y Enter the element 20 Where to insert left/right of 50 L Do you want to Enter more element?(y/n) y Enter the element 70 Where to insert left/right of 50 R Do you want to Enter more element?(y/n)n

Enter the choice 2 20 50 70

Enter the choice 3 50 20 70 Enter the choice 4 20 70 50

Enter the choice 5

EX.NO:6 DATE: 12.8.11 BINARY SEARCH TREE PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define null 0 typedef struct bst { int data; struct bst*left,*right; } node; void insert(node*,node*); void inorder(node*); node*search(node*,int,node**); void del(node*, int); void main() { int choice; char ans='n'; int key; node*New,*root,*tmp,*parent; node*get_node(); root=NULL; clrscr(); printf("\n Program for binary search tree"); do { printf("\n 1.Create \n2.Search\n3.Delete\n4.Display\n5.Exit"); printf("\nEnter the choice"); scanf("%d",&choice); switch(choice) { case 1: do { New=get_node(); printf("\n Enter the element"); scanf("%d",&New->data); if(root==NULL) root=New; else

insert(root,New); printf("\n Do you want to enter more elements :( y/n)"); ans=getch(); } while(ans=='y'); break; case 2: printf("\n Enter the element which you want to search"); scanf("%d",&key); tmp=search(root,key,&parent); printf("\n Parent of node %d is %d",tmp->data,parent->data); break; case 3: printf("\n Enter the element u wish to deletr"); scanf("%d",&key); del(root,key); break; case 4: if(root==NULL) printf("\n Tree is not created"); else { printf("\n The tree is:"); inorder(root); } break; case 5: exit(0); break; }} while(choice!=5); } node *get_node() { node*temp; temp=(node*)malloc(sizeof(node)); temp->left=NULL; temp->right=NULL; return temp; } void insert(node*root,node*New) { if(New->data<root->data) { if(root->left==NULL) root->left=New;

else insert(root->left,New); } if(New->data>root->data) { if(root->right==NULL) root->right=New; else insert(root->right,New); }} node *search(node*root,int key,node**parent) { node *temp; temp=root; while(temp!=NULL) { if(temp->data==key) { printf("\n The %d element is present ",temp->data); return temp; } *parent=temp; if(temp->data>key) temp=temp->left; else temp=temp->right; } return NULL; } void del(node*root,int key) { node*temp,*parent,*temp_succ; temp=search(root,key,&parent); if(temp->left!=NULL&&temp->right!=NULL) { parent=temp; temp_succ=temp->right; while(temp_succ->left!=NULL) { parent=temp_succ; temp_succ=temp_succ->left; } temp->data=temp_succ->data; parent->left=NULL; printf("\n Now deleted it!"); return;

} if(temp->left==NULL&&temp->right==NULL) { if(parent->left==temp) parent->left=temp->left; else parent->right=temp->left; temp=NULL; free(temp); printf("\n Now deleted it!"); return; } if(temp->left==NULL&&temp->right!=NULL) { if(parent->left==temp) parent->left=temp->right; else parent->right=temp->right; temp=NULL; free(temp); printf("\n Now deleted it!"); return; } if(temp->left==NULL&&temp->right==NULL) { if(parent->left==temp) parent->left==NULL; else parent->right=NULL; printf("\n Now deleted it!"); return; }} void inorder(node*temp) { if(temp!=NULL) { inorder(temp->left); printf("%d\t\t",temp->data); inorder(temp->right); }}

OUTPUT:-

Program for binary search tree 1. Create 2. Search 3. Delete 4. Display 5. Exit Enter the choice 1 Enter the element12 Do you want to enter more elements :( y/n) Enter the element 23 Do you want to enter more elements :( y/n) Enter the element 45 Do you want to enter more elements :( y/n) Enter the element 67 Do you want to enter more elements :( y/n) Enter the choice 2 Enter the element which you want to search12 The 12 element is present Parent of node 12 is 28263 Enter the choice 3 Enter the element u wish to deletr67 The 67 element is present Now deleted it! Enter the choice 4 The tree is: 12 23 45

Ex.No: 7 DATE: 19.08.11 AVL TREE PROGRAM: #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)); clrscr(); root=NULL; printf("\n 1. Insert\n2. Display\n3. Exit"); while(1) { printf("\n Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter the value to be inserted : "); scanf("%d",&info); if(search(root,info)==NULL) root=insert(info,root,&ht_inc); else printf("\nDuplicate value ignored\n"); break; case 2: if(root==NULL) { printf("\nTree is empty"); continue;

} printf("\n tree is:\n"); display(root, 1); printf("\n \n"); printf("In order Traversal is: "); inorder(root); printf("\n"); break; case 3: exit(1); default: printf("\n Wrong choice\n"); }}} 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); } struct node*insert(int info,struct node*pptr,int *ht_inc) { struct node*aptr; 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: pptr->balance=0; *ht_inc=FALSE;

break; case 0: pptr->balance=1; break; case 1: aptr=pptr->lchild; if(aptr->balance==1) { printf("\n Left to Left Rotation\n"); pptr->lchild=aptr->rchild; aptr->rchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("\n Left to Right Rotation\n"); bptr=aptr->rchild; aptr->rchild=bptr->lchild; bptr->lchild=aptr; pptr->lchild=bptr->rchild; bptr->rchild=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; }}}} if(info>pptr->info) { pptr->rchild=insert(info,pptr->rchild,ht_inc); if(*ht_inc==TRUE) { switch(pptr->balance) { case 1: pptr->balance=0; *ht_inc=FALSE; break; case 0:

pptr->balance=-1; break; case -1: aptr=pptr->rchild; if(aptr->balance==-1) { printf("\n Right to Right Rotation"); pptr->rchild=aptr->lchild; aptr->lchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("\n Right to Left Rotation\n"); bptr=aptr->lchild; aptr->lchild=bptr->rchild; bptr->rchild=aptr; pptr->rchild=aptr; pptr->rchild=bptr->lchild; bptr->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; }}}} return(pptr); } display(struct node*ptr,int level) { int i; if(ptr!=NULL) { display(ptr->rchild,level+1); printf("\n\n"); for(i=0;i<level;i++) printf(" "); printf("%d ",ptr->info); display(ptr->lchild,level+1);

}} inorder(struct node*ptr) { if(ptr!=NULL) { inorder(ptr->lchild); printf("\t%d",ptr->info); inorder(ptr->rchild); }}

OUTPUT:1. Insert 2. Display 3. Exit Enter your choice: 1 Enter the value to be inserted: 10 Enter your choice: 1 Enter the value to be inserted: 20 Enter your choice: 1 Enter the value to be inserted: 30 Right to Right Rotation Enter your choice: 2

Tree is: 30 20 10 In order Traversal is: 10 20 30

Ex.No: 8 DATE: 26.08.11 PRIORITY QUEUE PROGRAM: #include<stdio.h> #include<conio.h> #define SIZE 5 void main(void) { int rear,front,que[SIZE],choice; int Qfull(int rear),Qempty(int rear,int front); int insert(int que[SIZE],int rear, int front); int delet(int que[SIZE],int front); void display(int que[SIZE],int rear,int front); char ans; clrscr(); front=0; rear=-1; do{ clrscr(); printf("\n\t\t Priority Queue\n"); printf("\n Main Menu"); printf("\n 1.Insert\n2.Delete\n3.Display"); printf("\n Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: if(Qfull(rear)) printf("\n Queue is full"); else rear=insert(que,rear,front); break; case 2: if(Qempty(rear,front)) printf("\n cannot delete element"); else front=delet(que,front); break; case 3: if(Qempty(rear,front)) printf("\n Queue is empty");

else display(que,rear,front); break; default: printf("\n wrong choice"); break; } printf("\n Do you want to continue?"); ans=getche(); } while(ans=='Y'||ans=='y'); getch(); } int insert(int que[SIZE],int rear,int front) { int item,j; printf("\n Enter the element:"); scanf("%d",&item); if(front==-1) front++; j=rear; while(j>=0&&item<que[j]) { que[j+1]=que[j]; j--; } que[j+1]=item; rear=rear+1; return rear; } int Qfull(int rear) { if(rear==SIZE-1) return 1; else return 0; } int delet(int que[SIZE],int front) {int item; item=que[front]; printf("\n The item deleted is %d",item); front++; return front; } Qempty(int rear,int front) {if((front==-1)||(front>rear))

return 1; else return 0; } void display(int que[SIZE],int rear,int front) {int i; printf("\n The queue is:"); for(i=front;i<=rear;i++) printf("%d",que[i]); } OUTPUT:Main Menu 1. Insert 2. Delete 3. Display Enter your choice: 1 Enter the element: 10 Do you want to continue? Y Enter your choice: 1 Enter the element: 20 Do you want to continue? Y Enter your choice: 1 Enter the element: 30 Do you want to continue? Y Enter your choice: 3 The queue is: 30 20 10 Do you want to continue? Y Enter your choice: 2 The item delete is 30 Do you want to continue? Y Enter your choice: 3 The queue is: 20 10 Do you want to continue? N

EX.NO.: 9 DATE: 26.08.11. IMPLEMENTATION OF HASHING TECHNIQUES USING LINEAR PROBING PROGRAM: #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 linear_prob(int[],int,int),display(int[]); clrscr(); printf("\n collision handling by Linear Probling"); for(i=0;i<MAX;i++) a[i]=-1; do { printf("\n Enter the Number\t"); scanf("%d",&num); key=creat(num); linear_prob(a,key,num); printf("\n Do U Wish To Continue?(y/N)"); ans=getche(); } while(ans=='y'); display(a); getch(); } int create(int num) { int key; key=num%10; return key; } void linear_prob(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 HASH TABLE 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; }}} void display(int a[MAX]) { int i; printf("\n The Hash Table is....\n"); for(i=0;i<MAX;i++) printf("\n %d%d",i,a[i]); }

OUTPUT:Collision handling by Linear Probing Enter the Number 12 Do You Wish To Continue? (y/N) Enter the Number 34 Do You Wish To Continue? (y/N) Enter the Number 79 Do You Wish To Continue? (y/N) The Hash Table is.... 0-1 1-1 212 3-1 434 5-1 6-1 7-1 8-1 979 y

EX.NO.: 10 DATE: 02.09.11. TOPOLOGICAL SORT IMPLEMENTATION PROGRAM: #include<conio.h> #include<stdio.h> #define MAX 10 void main() {int G[MAX][MAX],n,i,j,k,visited[MAX],indegree[MAX],edges,u,v; printf("\nEnter no.of vertices :"); scanf("%d",&n); //Initialize the adjacency matrix for(i=0;i<n;i++) for (j=0;j<n;j++) G[i][j]=0; //read the graph as a list of direted edges printf("\Enter No.of edges :"); scanf("%d",&edges); printf("\Enter edges as a pair of vertices :"); for(i=0;i<edges;i++) { scanf("%d%d",&u,&v); G[u][v]=1; } //initialize for(i=0;i<n;i++) { visited[i]=0; indegree[i]=0; for(j=0;j<n;j++) if(G[j][i]!=0) indegree[i]++; } printf("\nTopological ordering sequence :\n"); for(i=0;i<n;i++) { //locate a node with degree 0 j=0; while(j<n) { if(visited[j]==0 && indegree[j]==0) { printf("%d ",j); visited[j]=1;

//decrement the indegree of nodes adjacent to j for(k=0;k<n;k++) if(!visited[k]&& G[j][k]!=0) indegree[k]--; break; } j++; } if(j==n) { printf("\n Graph has a cycle"); break; } } getch(); }

OUTPUT: Enter no.of vertices :3 Enter No.of edges :3 Enter edges as a pair of vertices :1 2 3 4 3 2 Topological ordering sequence : 012

EX.NO.: 11 DATE: 09.09.11. DIJKSTRAS ALGORITHM IMPLEMENTATION PROGRAM: #define INFINITY 9999 #include<stdio.h> #define MAX 10 typedef struct node { struct node *next; int vertex,weight; }node; node *G[10]; int n; void readgraph(); void insert(int vi,int vj,int w); void djikstra(int startnode); void main() { int u; clrscr(); readgraph(); printf("\n enter the starting node"); scanf("%d",&u); djikstra(u); getch(); } void djikstra(int startnode) { int distance[MAX],pred[MAX]; int visited[MAX],count,mindistance,nextnode,i,j; node *p; for(i=0;i<n;i++) { distance[i]=INFINITY; pred[i]=startnode; visited[i]=0; } distance[startnode]=0; count=0; while(count<n-1) { mindistance=INFINITY; for(i=0;i<n;i++)

if(distance[i]<mindistance&&!visited[i]) { mindistance=distance[i]; nextnode=i; } visited[nextnode]=1; for(p=G[nextnode];p!=NULL;p=p->next) if(!visited[p->vertex]) if(mindistance+p->weight<distance[p->vertex]) { distance[p->vertex]=mindistance+p->weight; pred[p->vertex]=nextnode; } count++; } for(i=0;i<n;i++) if(i!=startnode) { printf("\n distance of %d=%d",distance[i]); printf("path=%d",i); j=i; do { j=pred[j]; printf("<-%d",j); } while(j!=startnode); } } void readgraph() { int i,j; int adj[10][10]; printf("\n enter no of vertices"); scanf("%d",&n);printf("\nenter adjacency matrix:"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&adj[i][j]); for(i=0;i<n;i++) G[i]=NULL; for(i=0;i<n;i++) for(j=0;j<n;j++) if(adj[i][j]); insert(i,j,adj[i][j]); } void insert(int vi,int vj,int w)

{ node *p,*q; q=(node*)malloc(sizeof(node)); q->vertex=vj; q->next=NULL; q->weight=w; if(G[vi]==NULL) G[vi]=q; else { p=G[vi]; while(p->next!=NULL) p=p->next; p->next=q; } }

OUTPUT: enter no of vertices 3 enter adjacency matrix: 4 5 76 9 0 9 9 8 7 enter the starting node4 distance of 9999=1352 path=0<-4 distance of 9999=1352 path=1<-4 distance of 9999=1352 path=2<-4

Ex.No:12 DATE: 16.09.2011 PRIMS ALGORITHM PROGRAM: #include<stdio.h> #include<conio.h> #define SIZE 20 #define INFINITY 32767 void prim(int G[][SIZE],int nodes) { int select[SIZE],i,j,k; int min_dist,v1,v2,total=0; for(i=0;i<nodes;i++) select[i]=0; printf("\n the minimal spanning tree is:\n"); select[0]=1; for(k=1;k<nodes;k++) { min_dist=INFINITY; for(i=0;i<nodes;i++) { for(j=0;j<nodes;j++) { if(G[i][j]&&((select[i]&&!select[j])||(!select[i]&&select[j]))) { if(G[i][j]<min_dist) { min_dist=G[i][j]; v1=i; v2=j; }}}} printf("\n Edge (%d%d)andweight=%d",v1,v2,min_dist); select[v1]=select[v2]=1; total=total+min_dist; } printf("\n\n\t total path length is=%d",total);

} void main() { int G[SIZE][SIZE],nodes; int v1,v2,length,i,j,n; clrscr(); printf("\n Prim's algorithm is\n");

printf("\n Enter the number of nodes in the graph"); scanf("%d",&nodes); printf("\n Enter the number edges in the graph"); scanf("%d",&n); for(i=0;i<nodes;i++) for(j=0;j<nodes;j++) G[i][j]=0; printf("\n Enter the edges and weights\n"); for(i=0;i<n;i++) { printf("\n Enter the edge by v1 and v2:"); scanf("%d%d",&v1,&v2); printf("\n Enteer the corresponding weight:"); scanf("%d",&length); G[v1][v2]=G[v2][v1]=length; } getch(); printf("\n\t"); clrscr(); prim(G,nodes); getch(); }

OUTPUT:Prim's algorithm is Enter the number of nodes in the graph 5 Enter the number edges in the graph 5 Enter the edges and weights Enter the edge by v1 and v2: 01 Enter the corresponding weight: 1 Enter the edge by v1 and v2: 12 Enter the corresponding weight: 2 Enter the edge by v1 and v2: 23 Enter the corresponding weight: 3 Enter the edge by v1 and v2: 34 Enter the corresponding weight: 4 Enter the edge by v1 and v2: 45 Enter the corresponding weight: 5 The minimal spanning tree is: Edge (01) and weight=1 Edge (12) and weight=2 Edge (23) and weight=3 Edge (34) and weight=4 Total path length is=10

Ex.No:12 DATE: 16.09.2011 KRUSKALS ALGORITHM PROGRAM: #include<stdio.h> #include<conio.h> typedef struct edgel { int cost; int x,y; }edgel; edgel e[20]; int ne,nv,visited[15]; int sum=0; void getdata(); void kruskal(int,int,int); void getdata() { int i,j,temp1; printf("\n Enter the no.of vertex"); scanf("%d",&nv); printf("\n Enter the no.of edges"); scanf("%d",&ne); for(i=1;i<=ne;i++) { printf("\n Enter the edges in (V1,V2 )form and its cost :\n"); scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].cost); visited[i]=0; } for(i=1;i<=ne;i++) { for(j=i+1;j<=ne;j++) { if(e[i].cost>e[j].cost) { temp1=e[i].cost; e[i].cost=e[j].cost; e[j].cost=temp1; temp1=e[i].x; e[i].x=e[j].x; e[j].x=temp1; temp1=e[i].y; e[i].y=e[j].y; e[j].y=temp1; }}}} void kruskal(int x,int y,int i)

{ if(visited[x]==0||visited[y]==0) { visited[x]=visited[y]==1; printf("%d\t%d\t%d\n\n",x,y,e[i].cost); sum+=e[i].cost; }} void main() { int i; clrscr(); printf("\n\n program for kruskal algotrithm"); getdata(); printf("\n \t the mininum spanning tree is \n\n....."); printf("vertex1\t vertex2\tcost\n\n"); for(i=1;i<=ne;i++) kruskal(e[i].x,e[i].y,i); printf("total cost of mst is =%d",sum); getch(); } OUTPUT: program for kruskal algotrithm Enter the no.of vertex3 Enter the no.of edges3 Enter the edges in (V1,V2 )form and its cost : 1 2 3 Enter the edges in (V1,V2 )form and its cost : 4 5 6 Enter the edges in (V1,V2 )form and its cost : 7 8 9 the mininum spanning tree is .....vertex1 vertex2 cost 1 4 7 total cost of mst is =18 2 5 8 3 6 9

EX.NO.: 13 DATE: 23.09.11.


BACKTRACKIN ALGORITHM FOR KNAPSACK PROBLEM

PROGRAM: #include<stdio.h> #include<conio.h> float final_profit=-1.0; int p[9]={0,11,21,31,33,43,53,55,65}; int w[]={0,1,11,21,23,33,43,53,55}; int m=110; int n=8; int temp[9],x[9]; float final_wt=-1.0; float bound_calculation(int cp,int cw,int k) { int ub,c,i; ub=cp; c=cw; for(i=k+1;i<=n;i++) { c=c+w[i]; if(c<m) ub=ub+p[i]; else return(ub+(1-(c-m)/w[i]*p[i])); } return ub; } void bk(int k,int cp, int cw) { int new_k,new_cp,new_cw,j; if(cw+w[k]<=m) { temp[k]=1; if(k<n) { new_k=k+1; new_cp=cp+p[k]; new_cw=cw+w[k]; bk(new_k,new_cp,new_cw); } if((new_cp>final_profit)&&(k==n)) { final_profit=new_cp; final_wt=new_cw; for(j=1;j<=k;j++)

x[j]=temp[j]; } } if(bound_calculation(cp,cw,k)>=final_profit) { temp[k]=0; if(k<n) BK(k+1,cp,cw); if((cp>final_profit)&&(k==n)) { final_profit=cp; final_wt=cw; for(j=1;j<=n;j++) x[j]=temp[j]; } } } void main() { int i; clrscr(); printf("\n\tknapsack problem using backtracking algorithum\n"); printf("\ncapacity of knapstack=%d",m); printf("\n------------------\n"); for(i=1;i<=n;i++) printf("\n%d\t%d",p[i],w[i]); printf("\n----------------\n"); BK(1,0,0); printf("\n following items are selected from knapsasck......"); for(i=1;i<=n;i++) { if(x[i]==1) { printf("\n\titem%d",i); } printf("\nfinal weight =%0.2f",final_wt); printf("\n finalprofit=%0.2f",final_profit); getch(); } }

OUTPUT:

knapsack problem using backtracking algorithum capacity of knapstack=110 -----------------11 1 21 11 31 21 33 23 43 33 53 43 55 53 65 55 ---------------following items are selected from knapsasck...... item1 final weight =109.00 finalprofit=159.00 item2 final weight =109.00 finalprofit=159.00 item3 final weight =109.00 finalprofit=159.00 item4 final weight =109.00 finalprofit=159.00 item5 final weight =109.00 finalprofit=159.00

EX.NO.: 14 DATE: 23.09.11.


BRANCH & BOUND ALGORITHM IMPLEMENTATION

PROGRAM: #include<stdio.h> #include<conio.h> #define MAX 10 typedef struct { int nodes[MAX]; int vertex; int min; } path_node; path_node TSP(int source,path_node list,int element[][MAX],int max_no_cities) { int i,j; path_node new_list,new_path,new_min; if(list.vertex==0) { new_min.min=element[source][1]; new_min.nodes[max_no_cities-1]=source; new_min.vertex=max_no_cities; return new_min; } for(i=0;i<list.vertex;i++) { new_list.vertex=0; for(j=0;j<list.vertex;j++) if(i!=j) new_list.nodes[new_list.vertex++]=list.nodes[j]; new_path=TSP(list.nodes[i],new_list,element,max_no_cities); new_path.min=element[source][list.nodes[i]]+new_path.min; new_path.nodes[max_no_cities-list.vertex-1]=source; if(i==0) { new_min=new_path; } else { if(new_path.min<new_min.min) new_min=new_path; }} return new_min;} void display(path_node path) {

int i; printf("\n\n the minimum cost is %d\n",path.min); printf("\n the path is...\n"); for(i=0;i<path.vertex;i++) printf("%d--",path.nodes[i]); printf("%d",path.nodes[0]);} main() { int i,j,element[MAX][MAX],max_no_cities; path_node graph,path; clrscr(); printf("\n how many number of cities are there?"); scanf("%d",&max_no_cities); if(max_no_cities==0) { printf("error:there is no city for processing the TSP"); } else { for(i=1;i<=max_no_cities;i++) { for(j=1;j<=max_no_cities;j++) if(i==j) element[i][j]=0; else { printf("enter distance from city %d to %d?",i,j); scanf("%d",&element[i][j]); } if(i>1) graph.nodes[i-2]=i; } graph.vertex=max_no_cities-1; path=TSP(1,graph,element,max_no_cities); display(path); } getch(); return 1; }

OUTPUT: how many number of cities are there?5 enter distance from city 1 to 2?3 enter distance from city 1 to 3?2 enter distance from city 1 to 4?4 enter distance from city 1 to 5?5 enter distance from city 2 to 1?3 enter distance from city 2 to 3?4 enter distance from city 2 to 4?2 enter distance from city 2 to 5?4 enter distance from city 3 to 1?5 enter distance from city 3 to 2?2 enter distance from city 3 to 4?5 enter distance from city 3 to 5?3 enter distance from city 4 to 1?5 enter distance from city 4 to 2?2 enter distance from city 4 to 3?4 enter distance from city 4 to 5?5 enter distance from city 5 to 1?3 enter distance from city 5 to 2?2 enter distance from city 5 to 3?4 enter distance from city 5 to 4?2 the minimum cost is 12 the path is... 1--3--5--4--2--1

EX.NO.: 15 DATE: 23.09.11.


RANSDOMIZED ALGORITHM IMPLEMENTATION

PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> int main(void) { int a[10]={10,20,30,40,11,12,13,10,30,30}; int n=10; void repetition(int a[],int n); clrscr(); printf("\n\t program finds the repeated element\n"); repetition(a,n); getch(); return 0;} void repetition(int a[],int n) { int i,j,count; time_t t; srand((unsigned)time(&t)); count=1; while(count<=100) { i=rand()%(n+1); j=rand()%(n+1); if((i!=j)&&(a[i]==a[j])) printf("\n The repeated element is present at index %d",i); count++;}} OUTPUT: program finds the repeated element The repeated element is present at index 7 The repeated element is present at index 0 The repeated element is present at index 10 The repeated element is present at index 8 The repeated element is present at index 7 The repeated element is present at index 9 The repeated element is present at index 8 The repeated element is present at index 2 The repeated element is present at index 0

You might also like