****************************************************program of singly link list
****************************************************** #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }; struct node * start=NULL; struct node *create_ll(struct node *); struct node *display(struct node *); struct node *insert_beg(struct node *); struct node *insert_end(struct node *); struct node *delete_beg(struct node *); struct node *delete_end(struct node *); main() { int option; clrscr(); do { printf("\n\n*******MAIN MENU******"); printf("\n 1: Create a list"); printf("\n 2: Display the list"); printf("\n 3: Insert at the beginning"); printf("\n 4: Insert at the end"); printf("\n 5: Delete at the begnining"); printf("\n 6: Delete at the end"); printf("\n\n Enter your option"); scanf("%d", &option); switch(option) { case 1: start =create_ll(start); printf("\n LINK LIST IS CREATED"); break; case 2: start= display(start); break; case 3: start= insert_beg(start); break; case 4: start= insert_end(start); break; case 5: start= delete_beg(start); break; case 6: start= delete_end(start); break; } } while(option!=12); getch(); return 0; } struct node *create_ll(struct node *start) { struct node *new_node ,*ptr; int num; printf("\n Enter -1 to end"); printf("\n Enter the data"); scanf("%d" , num); while(num!=-1) { new_node=(struct node*)malloc(sizeof (struct node*)); new_node->data=num; if(start==NULL) { new_node->next=new_node; start=new_node; } else ptr=start; { while(ptr->next!=start) ptr=ptr->next; new_node->next=start; ptr->next=new_node; } printf("\n Enter the data"); scanf("%d" , num); } return start; } struct node *display(struct node *start) { struct node *ptr; ptr=start; printf("\n"); while(ptr->next!=start) { printf("\t %d", ptr->data); ptr=ptr->next; } printf("\t %d", ptr->data); return start; } struct node *insert_beg(struct node *start) { struct node *new_node , *ptr; int num; printf("\n Enter the data"); scanf("%d" , &num); new_node=(struct node*)malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=start) ptr=ptr->next; ptr->next=new_node; new_node->next=start; start=new_node; return start; } struct node *insert_end(struct node *start) { struct node *ptr , *new_node; int num; printf("\n Enter the data"); scanf("%d" , num); new_node=(struct node*) malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=start) ptr=ptr->next; ptr->next=new_node; new_node->next=start; return start; } struct node *delete_beg(struct node *start) { struct node *ptr; while(ptr->next!=start) ptr=ptr->next; ptr->next=start->next; free(start); start=ptr->next; return start; } struct node *delete_end(struct node *start) { struct node *ptr , *preptr; ptr= start; while(ptr->next!=start) { preptr=ptr; ptr=ptr->next; } preptr->next=ptr->next; free(ptr); return start; } /**************************output****************************** *******MAIN MENU****** 1: Create a list 2: Display the list 3: Insert at the beginning 4: Insert at the end 5: Delete at the begnining 6: Delete at the end Enter your option1 Enter -1 to end *******MAIN MENU****** 1: Create a list 2: Display the list 3: Insert at the beginning 4: Insert at the end 5: Delete at the begnining 6: Delete at the end Enter your option2 0 *********************************/ ********************************************** progrram of dOUbly linklist ************************************************** #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; struct node *prev; }; struct node * start=NULL; struct node *create_ll(struct node *); struct node *display(struct node *); struct node *insert_beg(struct node *); struct node *insert_end(struct node *); struct node *insert_before(struct node *start); struct node *insert_after(struct node *start); struct node *insert_sorted(struct node *start); struct node *delete_beg(struct node *); struct node *delete_end(struct node *); struct node *delete_before(struct node *sstart); struct node *delete_after(struct node *start); struct node *delete_sorted(struct node *start); main() { int option; clrscr(); do { printf("\n\n*******MAIN MENU******"); printf("\n 1. Create a list"); printf("\n 2. Display the list"); printf("\n 3. Insert at the beginning"); printf("\n 4. Insert at the end"); printf("\n 5. Insert before a given node"); printf("\n 6. Insert after a given node"); printf("\n 7. Insert in a sorted list"); printf("\n 8. Delete at the begnining"); printf("\n 9. Delete at the end"); printf("\n 10. Delete before a given node"); printf("\n 11, Delete after a given node"); printf("\n 12. Delete from a sorted link list"); printf("\n\n Enter your option"); scanf("%d", &option); switch(option) { case 1: start =create_ll(start); printf("\n LINK LIST IS CREATED"); break; case 2 : start =display(start); break; case 3 : start =insert_beg(start); break; case 4 : start =insert_end(start); break; case 5 : start =insert_before(start); break; case 6 : start =insert_after(start); break; case 7 : start =insert_sorted(start); break; case 8 : start =delete_beg(start); break; case 9 : start =delete_end(start); break; case 10 : start =insert_before(start); break; case 11 : start =delete_after(start); break; case 12 : start =insert_sorted(start); break; } } while(option!=13); getch(); return 0; } struct node *create_ll(struct node *start) { struct node *new_node; int num; printf("\n Enter the data"); scanf("%d" , &num); while(num!=-1) { if(start==NULL) { start=(struct node *)malloc(sizeof(struct node *)); start->prev=NULL; start->next=NULL; start->data=num; } else { new_node=(struct node *)malloc(sizeof(struct node*)); new_node->prev=NULL; new_node->data=num; new_node->next=start; start->prev=new_node; start=new_node; } printf("\Enter the data again"); scanf("%d" ,&num); } return start; } struct node *display(struct node *start) { struct node *ptr; ptr=start; printf("\n"); while(ptr!=NULL) { printf("\t %d" , ptr->data); ptr=ptr->next; } return start; } struct node *insert_beg(struct node *start) { struct node *new_node; int num; printf("\n Enter the data"); scanf("%d" , &num); new_node=(struct node *)malloc(sizeof(struct node *)); new_node->data=num; start->prev=new_node; new_node->next=start; new_node->prev=NULL; start=new_node; return start; } struct node *insert_end(struct node *start) { struct node *ptr , *new_node; int num; printf("\n Enter the data"); scanf("%d" , &num); new_node=(struct node *)malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=NULL) ptr=ptr->next; ptr->next=new_node; new_node->prev=ptr; new_node->next=NULL; return start; } struct node *insert_before(struct node *start) { struct node *new_node , *ptr; int num , val; printf("\n Enter the data"); scanf("%d" ,&num); printf("\n Enter the value before which node is to added"); scanf("%d", &val); new_node=(struct node*)malloc(sizeof(struct node*)); new_node->data=num; ptr=start; while(ptr->data!=val) ptr=ptr->next; new_node->next=ptr; ptr->prev->next=new_node; ptr->prev=new_node; return start; } struct node *insert_after(struct node *start) { struct node *new_node , *ptr; int val , num; printf("\n Enter the data"); scanf("%d" , &num); printf("\n Enter the value after which node is to be added"); scanf("%d" ,&val); new_node=(struct node *)malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->data != val) ptr=ptr->next; new_node->prev=ptr; new_node->next=ptr->next; ptr->next->prev=new_node; ptr->next=new_node; return start; } struct node *insert_sorted(struct node *start) { struct node *ptr, *new_node; int num; printf("\n Enter the data"); scanf("%d" , &num); new_node=(struct node *)malloc(sizeof(struct node*)); new_node->data=num; ptr=start; while(ptr->data<num) { ptr=ptr->next; if(ptr->next==NULL) break; } if(ptr->next==NULL) { ptr->next=new_node; new_node->prev=ptr; new_node->next=NULL; } else { new_node->next=ptr; new_node->prev=ptr->prev; ptr->prev->next=new_node; ptr->prev=new_node; } return start; } struct node *delete_beg(struct node *start) { struct node *ptr; ptr=start; start=start->next; start->prev=NULL; free(ptr); return start; } struct node *delete_end(struct node *start) { struct node *ptr; ptr=start; while(ptr->next!=NULL) ptr=ptr->next; ptr->prev->next=NULL; free(ptr); return start; } struct node *delete_after(struct node *start) { struct node *ptr , *temp; int val; printf("Enter the value after which node has to be deleted"); scanf("%d" , val); ptr=start; while(ptr->data!=val) ptr=ptr->next; temp=ptr->next; ptr->next=temp->next; temp->next->prev=ptr; free(temp); return start; } struct node *delete_before(struct node *start) { struct node *ptr , *temp; int val; printf("\n Enter the data before which the node has to be deleted"); scanf("%d" , &val); ptr=start; while(ptr->data!=val) ptr=ptr->next; temp=ptr->prev; if(temp==start) start=delete_beg(start); else { ptr->prev=temp->prev; temp->prev->next=ptr; } free(temp); return start; } struct node *delete_sorted(struct node *start) { struct node *ptr; int val; printf("\n Enter the value to deleted"); scanf("%d" , &val); ptr=start; while(ptr->data!=val) ptr=ptr->next; ptr->prev->next=ptr->next; free(ptr); return start; } /*******************output*************************** *******MAIN MENU****** 1. Create a list 2. Display the list 3. Insert at the beginning 4. Insert at the end 5. Insert before a given node 6. Insert after a given node 7. Insert in a sorted list 8. Delete at the begnining 9. Delete at the end 10. Delete before a given node 11, Delete after a given node 12. Delete from a sorted link list Enter your option Enter the data1 Enter the data again2 Enter the data again3 Enter the data again4 Enter the data again-1 LINK LIST IS CREATED *******MAIN MENU****** 1. Create a list 2. Display the list 3. Insert at the beginning 4. Insert at the end 5. Insert before a given node 6. Insert after a given node 7. Insert in a sorted list 8. Delete at the begnining 9. Delete at the end 10. Delete before a given node 11, Delete after a given node 12. Delete from a sorted link list Enter your option2 1 2 2 5 3 2 *******MAIN MENU****** 1. Create a list 2. Display the list 3. Insert at the beginning 4. Insert at the end 5. Insert before a given node 6. Insert after a given node 7. Insert in a sorted list 8. Delete at the begnining 9. Delete at the end 10. Delete before a given node 11, Delete after a given node 12. Delete from a sorted link list Enter your option **********************************************************/ ******************************************** programs of circuler link list *********************************************** #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }; struct node * start=NULL; struct node *create_ll(struct node *); struct node *display(struct node *); struct node *insert_beg(struct node *); struct node *insert_end(struct node *); struct node *delete_beg(struct node *); struct node *delete_end(struct node *); main() { int option; clrscr(); do { printf("\n\n*******MAIN MENU******"); printf("\n 1: Create a list"); printf("\n 2: Display the list"); printf("\n 3: Insert at the beginning"); printf("\n 4: Insert at the end"); printf("\n 5: Delete at the begnining"); printf("\n 6: Delete at the end"); printf("\n\n Enter your option"); scanf("%d", &option); switch(option) { case 1: start =create_ll(start); printf("\n LINK LIST IS CREATED"); break; case 2: start= display(start); break; case 3: start= insert_beg(start); break; case 4: start= insert_end(start); break; case 5: start= delete_beg(start); break; case 6: start= delete_end(start); break; } } while(option!=12); getch(); return 0; } struct node *create_ll(struct node *start) { struct node *new_node ,*ptr; int num; printf("\n Enter -1 to end"); printf("\n Enter the data"); scanf("%d" , num); while(num!=-1) { new_node=(struct node*)malloc(sizeof (struct node*)); new_node->data=num; if(start==NULL) { new_node->next=new_node; start=new_node; } else ptr=start; { while(ptr->next!=start) ptr=ptr->next; new_node->next=start; ptr->next=new_node; } printf("\n Enter the data"); scanf("%d" , num); } return start; } struct node *display(struct node *start) { struct node *ptr; ptr=start; printf("\n"); while(ptr->next!=start) { printf("\t %d", ptr->data); ptr=ptr->next; } printf("\t %d", ptr->data); return start; } struct node *insert_beg(struct node *start) { struct node *new_node , *ptr; int num; printf("\n Enter the data"); scanf("%d" , &num); new_node=(struct node*)malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=start) ptr=ptr->next; ptr->next=new_node; new_node->next=start; start=new_node; return start; } struct node *insert_end(struct node *start) { struct node *ptr , *new_node; int num; printf("\n Enter the data"); scanf("%d" , num); new_node=(struct node*) malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=start) ptr=ptr->next; ptr->next=new_node; new_node->next=start; return start; } struct node *delete_beg(struct node *start) { struct node *ptr; while(ptr->next!=start) ptr=ptr->next; ptr->next=start->next; free(start); start=ptr->next; return start; } struct node *delete_end(struct node *start) { struct node *ptr , *preptr; ptr= start; while(ptr->next!=start) { preptr=ptr; ptr=ptr->next; } preptr->next=ptr->next; free(ptr); return start; } /*****************************output*********************** *******MAIN MENU****** 1: Create a list 2: Display the list 3: Insert at the beginning 4: Insert at the end 5: Delete at the begnining 6: Delete at the end Enter your option **************************************/ ******************************************************** programs of doubly circuler link list ******************************************************* #include<stdio.h> #include<conio.h> struct node { struct node *next; int data; struct node *prev; }; struct node *start=NULL; struct node *create(struct node *); struct node *display(struct node *); struct node *insert_beg(struct node *); struct node *insert_end(struct node *); struct node *delete_beg(struct node *); struct node *delete_end(struct node *); main() { int option; clrscr(); do { printf("\n\n********MAIN MENU********"); printf("\n 1. Create link list"); printf("\n 2. Create link list"); printf("\n 3. Create link list"); printf("\n 4. Create link list"); printf("\n 5. Create link list"); printf("\n 6. Create link list"); printf("\n\n Enter your option"); scanf("%d" ,&option); switch(option) { case 1 : start = create(start); printf("\n Link list created"); break; case 2 : start =display(start); break; case 3 : start=insert_beg(start); break; case 4 : start=insert_end(start); break; case 5 : start=delete_beg(start); break; } } while(option!=7); getch(); return 0; } struct node *create(struct node *start) { struct node *new_node , *ptr; int num; printf("\n Enter -1 to end"); printf("Enter the data"); scanf("%d" , &num); while(num!=-1) { if(start==NULL) { start=(struct node*) malloc(sizeof(struct node*)); start->prev=NULL; start->data=num; start->next=start; } else { new_node=(struct node*)malloc(sizeof(struct node*)); new_node->prev=NULL; new_node->data=num; ptr=start; while(ptr->next!=start) ptr=ptr->next; new_node->prev=ptr; ptr->next=new_node; new_node->next=start; start->prev=new_node; start=new_node; } printf("\n Enter the data again"); scanf("%d" , &num); } return start; } struct node *display(struct node *start) { struct node *ptr; ptr=start; printf("\n"); while(ptr->next!=start) { printf("\t %d",ptr->data); ptr=ptr->next; } printf("\t %d",ptr->data); return start; } struct node *insert_beg(struct node *start) { struct node *ptr , *new_node; int num; printf("Enter the data"); scanf("%d" ,&num); new_node=(struct node *)malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=start) { ptr=ptr->next; } new_node->prev=ptr; ptr->next=new_node; new_node->next=start; start->prev=new_node; start=new_node; return start; } struct node *insert_end(struct node *start) { struct node *ptr , *new_node; int num; printf("/n Enter the data"); scanf("%d" , &num); new_node=(struct node *)malloc(sizeof(struct node *)); new_node->data=num; ptr=start; while(ptr->next!=start) ptr=ptr->next; ptr->next=new_node; new_node->prev=ptr; new_node->next=start; return start; } struct node *delete_beg(struct node *start) { struct node *ptr; ptr=start; while(ptr->next!=start) ptr=ptr->next; ptr->next=start->next; start->next->prev=ptr; free(start); start=start->next; return start; } /***********output************** º********MAIN MENU******** º 1. Create link list º 2. Create link list º 3. Create link list º 4. Create link list º 5. Create link list º 6. Create link list º º Enter your option1 Enter -1 to endEnter the data2 Enter the data again2 Enter the data again2 Enter the data again-1 Link list create ********MAIN MENU******** 1. Create link list 2. Create link list 3. Create link list 4. Create link list 5. Create link list 6. Create link list Enter your option2 2 2 2 2 ********MAIN MENU******** 1. Create link list 2. Create link list 3. Create link list 4. Create link list 5. Create link list 6. Create link list Enter your option *************/
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
PROGTAMS OF QUEUE **************************************************************************************************************** // Wreite a program to impliment a circular queue// #include<stdio.h> #include<conio.h> #define max 10 int queue[max]; int front= -1,rear=-1; void insert(); int delete_element(); int peek(); void display(); main() { int option,val; clrscr(); do { printf("\n\n*****MAIN MENU****"); printf("\n 1.Insert an element"); printf("\n 2.Delete an element"); printf("\n 3.Peek"); printf("\n 4.Display the queue"); printf("\n 5. Exit"); printf("\n *****************"); printf("\n\n Enter Your option:"); scanf("%d",&option); switch(option) { case 1: insert(); break; case 2: val=delete_element(); printf("\n The number that was deleted is :%d",val); break; case 3: val=peek(); printf("\n The first value in the queue is :%d",val); break; case 4: display(); break; } }while(option!=5); getch(); return 0; } void insert() { int num; printf("\n Enter the number to be inserted in the queue:"); scanf("%d",&num); if(front ==1 && rear==-1) printf("\n OVERFLOW"); else if (front==-1 && rear==-1) { front=rear=0; queue[rear]=num; } else if (rear==max-1 && front!=0) { rear=0; queue[rear]=num; } else { rear++; queue[rear]=num; } } int delete_element() { int val; if(front==-1) { printf("\nUNDERFLOW"); return -1; } val=queue[front]; if(front==rear) front=rear=-1; else { if(front==max-1) front=0; else front++; } return val; } int peek() { return queue[front]; } void display() { int i; printf("\n"); if(front!=-1 && rear !=-1) { if(front<rear) { for(i=front;i<=rear;i++) printf("\t %d",queue[i]); } else { for(i=front;i<max;i++) printf("\t %d",queue [i]); for(i=0;i<rear;i++) printf("\t %d",queue[i]); } } } /*================output=============== Enter the number to be inserted in the queue:3 *****MAIN MENU**** 1.Insert an element 2.Delete an element 3.Peek 4.Display the queue 5. Exit ***************** Enter Your option:4 2 3 *****MAIN MENU**** 1.Insert an element 2.Delete an element 3.Peek 4.Display the queue 5. Exit ***************** Enter Your option:3 The first value in the queue is :9 *****MAIN MENU**** 1.Insert an element 2.Delete an element 3.Peek 4.Display the queue 5. Exit ***************** Enter Your option:4 9 9 *****MAIN MENU**** 1.Insert an element 2.Delete an element 3.Peek 4.Display the queue 5. Exit ***************** Enter Your option:5 *********************************/ **************************************************************************************************** //write a program to impliment only insertion and display in inner queue// #include<stdio.h> #include<conio.h> #define max 10 int queue[max]; int front=-1,rear=-1; void insert(); void display(); int main() { int option,val; clrscr(); do { printf("\n*****MAIN MENU******"); printf("\n 1. Insert the value of queue"); printf("\n 2. Display the queue"); printf("\n 3. Exit"); printf("\n\n ENTER YOUR OPTION:"); scanf("%d", &option); switch(option) { case 1: insert(); // printf("\n Enter the number to be inserted:"); // break(); case 2 : display(); // break(); } }while(option!=3); getch(); return 0; } void insert() { int num; printf("\n Enter the number to be inserted in the queue"); scanf("%d", &num); if(rear==max-1) printf("\n OVERFLOW"); if(front==-1 && rear==-1) front=rear=0; else rear++; queue[rear]=num; } void display() { int i; printf("\n"); for(i=front;i<=rear;i++) printf("\n %d",queue[i]); } /**************output*********** *****MAIN MENU****** 1. Insert the value of queue 2. Display the queue 3. Exit ENTER YOUR OPTION:1 Enter the number to be inserted in the queue2 2 *****MAIN MENU****** 1. Insert the value of queue 2. Display the queue 3. Exit ENTER YOUR OPTION:1 Enter the number to be inserted in the queue3 2 3 *****MAIN MENU****** 1. Insert the value of queue 2. Display the queue 3. Exit ENTER YOUR OPTION:2 2 3 *****MAIN MENU****** 1. Insert the value of queue 2. Display the queue 3. Exit ENTER YOUR OPTION:3 */ *********************************************************************************************************** //WRITE A PROGRAM TO IMPLIMENT A LINEAR QUEUE.// #include<stdio.h> #include<conio.h> #define max 10 int queue[max]; int front= -1,rear= -1; void insert(); int delete_element(); int peek(); void display(); main() { int option,val; clrscr(); do { printf("\n\n****MAIN MENU*****"); printf("\n 1.INSERT AN ELEMENT"); printf("\n 2.DELETE AN ELEMENT"); printf("\n 3.PEEK"); printf("\n 4.DISPLAY THE QUEUE"); printf("\n EXIT"); printf("\n\n********************"); printf("\n\n Enter your option"); scanf("%d",&option); switch(option) { case 1: insert(); break; case 2: val=delete_element(); printf("\n The number that was deleted is :%d",val); break; case 3: val=peek(); printf("\n The first value in the queue is =%d",val); break; case 4: display(); break; } }while(option !=5); getch(); return 0; } void insert() { int num; printf("\n Enter the number to be inserted in the queue:"); scanf("%d",&num); if(rear==max-1) printf("\n OVERFLOW"); if(front == -1 && rear == -1) front=rear=0; else rear++; queue[rear]=num; } int delete_element() { int val; if(front ==-1 || front>rear) { printf("\n UNDERFLOW"); return -1; } else { front++; val=queue[front]; return val; } } int peek() { return queue[front]; } void display() { int i; printf("\n"); for(i=front; i<=rear;i++) printf("\t %d",queue[i]); } /*==========output============== ****MAIN MENU***** 1.INSERT AN ELEMENT 2.DELETE AN ELEMENT 3.PEEK 4.DISPLAY THE QUEUE EXIT ******************** Enter your option2 The number that was deleted is :3 ****MAIN MENU***** 1.INSERT AN ELEMENT 2.DELETE AN ELEMENT 3.PEEK 4.DISPLAY THE QUEUE EXIT ******************** Enter your option3 The first value in the queue is =3 ****MAIN MENU***** 1.INSERT AN ELEMENT 2.DELETE AN ELEMENT 3.PEEK 4.DISPLAY THE QUEUE EXIT ******************** Enter your option4 3 ****MAIN MENU***** 1.INSERT AN ELEMENT 2.DELETE AN ELEMENT 3.PEEK 4.DISPLAY THE QUEUE EXIT ******************** Enter your option5 *********************************/ **************************************************************************************************------------------------------------------------------PROGRAMS OF SHORTING ************************************************************ Q1. insertion short **************************************** #include<stdio.h> #include<conio.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]; j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); getch(); return 0; } **********************output****************************** Enter total elements: 5 Enter 5 elements: 26 32 36 54 12 After sorting: 12 26 32 36 54 ***************************************************************************************** Que.2.selection short #include<stdio.h> #include<conio.h> int main() { int s,i,j,temp,a[20]; clrscr(); printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s;i++){ for(j=i+1;j<s;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("After sorting is: "); for(i=0;i<s;i++) printf(" %d",a[i]); getch(); return 0; } *******************output***************************** Enter total elements: 5 Enter 5 elements: 22 33 16 45 25 After sorting is: 16 22 25 33 45 ******************************************************************** Que3.mergeSort #include<stdio.h> #include<conio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main(){ int merge[MAX],i,n; clrscr(); printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++){ scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++){ printf("%d ",merge[i]); } getch(); return 0; } void partition(int arr[],int low,int high){ int mid; if(low<high){ mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); } } void mergeSort(int arr[],int low,int mid,int high){ int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)){ if(arr[l]<=arr[m]){ temp[i]=arr[l]; l++; } else{ temp[i]=arr[m]; m++; } i++; } if(l>mid){ for(k=m;k<=high;k++){ temp[i]=arr[k]; i++; } } else{ for(k=l;k<=mid;k++){ temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++){ arr[k]=temp[k]; } } *********************************outout******************************* Enter the total number of elements: 5 Enter the elements which to be sort: 3 1 2 3 4 After merge sorting elements are: 1 2 3 3 4 ************************************************************************************** Que4.queick short #include<stdio.h> #include<conio.h> void quicksort(int [10],int,int); int main(){ int x[20],size,i; clrscr(); printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); getch(); return 0; } void quicksort(int x[10],int first,int last){ int pivot,j,temp,i; if(first<last){ pivot=first; i=first; j=last; while(i<j){ while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j){ temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } *************************output************************************ Enter size of the array: 5 Enter 5 elements: 22 11 33 44 56 Sorted elements: 11 22 33 44 56 ******************************************************************************************* Que.Bubble sort #include<stdio.h> #include<conio.h> int main() { int s,temp,i,j,a[20]; clrscr(); printf("Enter total numbers of elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble sorting algorithm for(i=s-2;i>=0;i--){ for(j=0;j<=i;j++){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); getch(); return 0; } ***************************output********************************** Enter total numbers of elements: 5 Enter 5 elements: 9 6 8 7 3 After sorting: 3 6 7 8 9 ******************************************************************************************************************************************Fibonacci (8) = 21 Fibonacci (9) = 34 *********************************/ ******* ********************************************************* Q )recursion function #include<stdio.h> #include<conio.h> int exp_rec(int,int); main() { int num1,num2,res; clrscr(); printf("Enter the first num="); scanf("%d",&num1); printf("Enter the second num="); scanf("%d",&num2); res = exp_rec(num1,num2); getch(); return 0; } int exp_rec(int x,int y) { if(y==0) return 1; else return (x * exp_rec(x,y-1)); } ********************************************************************************** ************************************************* Q )pop push page=89 book #include<stdio.h> #include<conio.h> # define MAX 10 int st[MAX], top=-1; void push(int st[], int val); int pop(int st[]); int peep(int st[]); void display (int st[]); main () { int val,option; clrscr(); do { printf("**** MAIN MENU ***** "); printf("\n 1. PUSH "); printf("\n 2. POP "); printf("\n 3. PEEP "); printf("\n 4. DISPLAY "); printf("\n 5. EXIT"); printf("\n **************"); printf("\n\n Enter your option : "); scanf("%d",&option); switch (option) { case 1: printf("\nEnter the number to be pushed on to the stack :"); scanf("%d",&val); push(st, val); break; case 2: val=pop(st); printf("\n The value deleted from the stack is : %d",val); break; case 3: val=peep(st); printf("\n The value stored at the top of the stack is :%d",val); break; case 4: display(st); break; } } while(option !=5); getch(); return 0; } void push(int st[],int val) { if(top==MAX-1) { printf("\n STACK OVETFLOW"); } else { top++; st[top]=val; } } int pop(int st[]) { int val; if(top==-1) { printf("STACK UNDERFLOW"); return-1; } else { val=st[top]; top--; return val; } } void display(int st[]) { int i; if(top==-1) { printf("\n STACK IS EMPTY "); } else { for(i=top;i>=0;i--) printf("\n %d",st[i]); } } int peep(int st[]) { if(top==NULL) { printf("\n STACK IS EMPTY "); return-1; } else { return (st[top]); } } /* OUT PUT 2. POP 3. PEEP 4. DISPLAY 5. EXIT ************** Enter your option : 1 Enter the number to be pushed on to the stack :3 **** MAIN MENU ***** 1. PUSH 2. POP 3. PEEP 4. DISPLAY 5. EXIT ************** Enter your option : 4 3 2 1**** MAIN MENU ***** 1. PUSH 2. POP 3. PEEP 4. DISPLAY 5. EXIT ************** Enter your option : 2 The value deleted from the stack is : 3**** MAIN MENU ***** 1. PUSH 2. POP 3. PEEP 4. DISPLAY 5. EXIT ************** Enter your option : 3 The value stored at the top of the stack is :2**** MAIN MENU ***** 1. PUSH 2. POP 3. PEEP 4. DISPLAY 5. EXIT ************** Enter your option :5 */ *********************************************************************************** Q ) 1) //write a program to evalueat a post expression #include<stdio.h> #include<conio.h> #define MAX 100 float st[MAX]; int top = -1; void push(float st[], float val); float pop(float st[]); float evalueatpostfixexp(char exp[]); main() { float val; char exp[100]; clrscr(); printf("\n enter any postfix expression:"); fflush(stdin); gets(exp); val=evalueatpostfixexp(exp); printf("\n value of postfix expression:=%.2f",val); getch(); return 0; } float evalueatpostfixexp(char exp[]) { int i=0; float op1,op2,value; while(exp[i]!='\0') { if(isdigit(exp[i])) push(st,(float) (exp[i]-'0')); else { op2=pop(st); op1=pop(st); switch(exp[i]) { case'+': value=op1+op2; break; case'-': value=op1-op2; break; case'*': value=op1*op2; break; case'/': value=op1/op2; break; case'%': value=(int)op1%(int)op2; break; } push(st,value); } i++; } return(pop(st)); } void push(float st[],float val) { if(top==MAX-1) printf("\n stack overflow"); else { top++; st[top]=val; } } float pop (float st[]) { float val=-1; if(top==-1) printf("\n enter the underflow"); else { val=st[top]; top--; } return val; } /* enter any postfix expression:1 value of postfix expression:=1.00 */ ********************************************************************************************** Q )write a program to implimentatio multiple stack (page 92) //0P 2.program to impliment multiple stack. #include<stdio.h> #include<conio.h> #define MAX 10 int stack[MAX], topA = -1, topB = MAX; //insert element in the first stack void pushA(int val) { if(topA==topB) printf("\n OVERFLOW"); else { topA += 1; stack[topA]= val; } } //delete elemr=em=nt of stack int popA() { int val; if (topA==-1) { printf("\n UNDERFLOW"); val= -999; } else { val = stack[topA]; topA--; } return val; } //printf element of first stack void display_stack() { int i; if(topA == -1) printf("\n stack empty "); else { for(i=topA;i>=0;i--) printf("\n %d",stack[i]); } } //insert element of second stack void pushB(int val) { if(topB- 1 == topA) printf("\n OVERFLOW "); else { topB=-1; stack[topB] = val; } } //delete elemr=ent from to second stack\ int popB() { int val; if(topB == MAX) { printf("\nUNDERFLOW "); val = -999; } else { val = stack[topB]; topB--; } return val; } //printf elemr=ent of second stack void display_stackB() { int i; if(topB == MAX) printf("\n stack empty"); else { for(i=topB;i<MAX;i++) printf("\n %d",stack[i]); } } int main() { int option, val; clrscr(); do { printf("\n***MENU**************"); printf("\n 1.PUSH IN STACKA "); printf("\n 2.PUSH IN STACKB "); printf("\n 3.POP IN STACKA "); printf("\n 4.POP IN STACKB "); printf("\n 5.DISPLAY STACKA "); printf("\n 6.DISPLAY STACKB "); printf("|n 7.EXIT"); printf("\n ENTER YOUR CHOICE "); scanf("%d",&option); switch(option) { case1: printf("Enter the value of pushed on stackA"); scanf("%d",&val); pushA(val); break; case2: printf("Enter the value of pushed on stackB "); scanf("%d",&val); pushB(val); break; case3: val=popA(); if (val!=-999) printf(" The value popped from on stackA = %d",val); break; case4: val=popB(); if (val!=-999) printf("Ther value popped from on stackB = %d",val); break; case5: printf("\n The content of StackA are :\n"); display_stackA(); break; case6: printf("\n The content of StackB are :\n"); display_stackB(); break; } } while(option !=7); getch(); return 0; } ******************************************************************************************************// insertion at begning in doubly link list. #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *prev; struct node *next; }; void main() { struct node *start=NULL,*new_node,*ptr; int value; clrscr(); printf("enter the data="); scanf("%d",&value); while(value!=10) { new_node=(struct node *)malloc(sizeof (struct node )); new_node->data=value; if(start==NULL) { new_node->prev=NULL; new_node->next=NULL; start=new_node; } else { ptr=start; start->prev=new_node; new_node->next=ptr; new_node->prev=start; start=new_node; } printf("enter the node="); scanf("%d",&value); } //while.... ptr=start; while(ptr!=NULL) { printf("\n new_node->value=%d",ptr->data); ptr=ptr->next; } getch(); } /*==output============ enter the data=1 enter the node=2 enter the node=3 enter the node=4 enter the node=5 enter the node=6 enter the node=7 enter the node=8 enter the node=9 enter the node=10 new_node->value=9 new_node->value=8 new_node->value=7 new_node->value=6 new_node->value=5 new_node->value=4 new_node->value=3 new_node->value=2 new_node->value=1 */ ***************************************************************** // creation link list by insertion in begning in single link list// #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *next; }; void main() { struct node *start=NULL,*new_node,*ptr; int value; // start= NULL; clrscr(); printf("enter the node ="); scanf("%d",&value); while(value!=10) { new_node=(struct node *)malloc(sizeof(struct node )); new_node->data=value; if(start==NULL) { new_node->next=NULL; start= new_node; } else { new_node->next=start ; start=new_node; } printf("enter another node="); scanf("%d",&value); } ptr=start; while(ptr!=NULL) { printf("\n %d",ptr->data); ptr=ptr->next; } getch(); } /*=========output========= enter the node =2 enter another node=3 enter another node=4 enter another node=5 enter another node=10 5 4 3 2 */ ****************************************************************** // insertion at end in doubly link list. #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *prev; struct node *next; }; struct node *start,*new_node, *ptr; int value; void main() { clrscr(); printf("enter the first node="); scanf("%d",&value); while(value!=10) { new_node=(struct node *)malloc(sizeof(struct node )); new_node->data=value; if(start==NULL) { new_node->next=NULL; new_node->prev=NULL; start=new_node; } else { ptr=start; while(ptr->next!=NULL) { ptr=ptr->next; } ptr->next=new_node; new_node->next = NULL; new_node->prev=ptr; } printf("enter the second node="); scanf("%d",&value); } //while.. //while.... ptr=start; while(ptr!=NULL) { printf("second node %d\n",ptr->data); ptr=ptr->next; } getch(); } /*==========output====== enter the first node=1 enter the second node=2 enter the second node=3 enter the second node=4 enter the second node=6 enter the second node=10 second node 1 second node 2 second node 3 second node 4 second node 6 */ *********************************************************************************************** //insrtion at beggning in circular linklist.// #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *next; }; void main() { struct node *start,*new_node,*ptr; int value; clrscr(); printf("enter the value of node="); scanf("%d",&value); start=NULL; while (value!=10) { new_node=(struct node *)malloc(sizeof(struct node *)); new_node->data=value; if(start==NULL) { new_node->next=start; start=new_node; } else if(start->next==NULL) { new_node->next=start; start->next=new_node; start=new_node; } else { ptr=start; while(ptr->next!=start) { ptr=ptr->next; } ptr->next=new_node; new_node->next=start; start=new_node; } printf("enter the value of node:"); scanf("%d",&value);//else... } //while... ptr=start; while(ptr->next!=start) { printf("\n \a ptr data=%d",ptr->data); ptr=ptr->next; } printf("\n \a ptr data=%d",ptr->data); getch(); } /*============output====== enter the value of node=2 enter the value of node:3 enter the value of node:5 enter the value of node:6 enter the value of node:8 enter the value of node:4 enter the value of node:9 enter the value of node:10 ptr data=9 ptr data=4 ptr data=8 ptr data=6 ptr data=5 ptr data=3 ptr data=2 */ ******************************************************************* //insertion at begnning in doubly link list.// #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *prev; struct node *next; }; void main() { struct node *start=NULL,*new_node,*ptr; int value; clrscr(); printf("enter the data="); scanf("%d",&value); while(value!=10) { new_node=(struct node *)malloc(sizeof (struct node )); new_node->data=value; if(start==NULL) { new_node->prev=NULL; new_node->next=NULL; start=new_node; } else { ptr=start; start->prev=new_node; new_node->next=ptr; new_node->prev=start; start=new_node; } printf("enter the node="); scanf("%d",&value); } //while.... ptr=start; while(ptr!=NULL) { printf("\n new_node->value=%d",ptr->data); ptr=ptr->next; } getch(); } /*==output============ enter the data=1 enter the node=2 enter the node=3 enter the node=4 enter the node=5 enter the node=6 enter the node=7 enter the node=8 enter the node=9 enter the node=10 new_node->value=9 new_node->value=8 new_node->value=7 new_node->value=6 new_node->value=5 new_node->value=4 new_node->value=3 new_node->value=2 new_node->value=1 */ ****************************************************************** Stacks #include<stdio.h> #include<conio.h> #define MAX 10 int st[MAX],top=-1; void push(int st[],int val); int pop(int st[]); int peep(int st[]); void display(int st[]); main() { int val,option; clrscr(); do { printf("\n****MAIN MENU*****"); printf("\n 1.PUSFH"); printf("\n 2.POP"); printf("\n 3.PEEP"); printf("\n 4.DISPLAY"); printf("\n 5.EXIT"); printf("\n *******************"); printf("\n\nEnter your option="); scanf("%d",&option); switch(option) { case 1: printf("\n enter the num. to be pushed on to the stack="); scanf("%d",&val); push(st,val); break; case 2: val=pop(st); printf("\n The value deleted from the stack is :%d",val); break; case 3: val=peep(st); printf("\n The value stored at the top of the stack is=%d",val); break; case 4: display(st); break; } //switch }while(option !=5); getch(); return 0; } void push(int st[],int val) { if(top==MAX-1) { printf("\n STACK OVERFLOW"); } else { top++; st[top]=val; } } int pop(int st[]) { int val; if(top==-1) { printf("\n STACK UNDERFLOW"); return -1; } else { val=st[top]; top--; return val; } } void display(int st[]) { int i; if(top==-1) { printf("\n STACK IS EMPTY"); } else { for(i=top;i>=0;i--) { printf("\n %d",st[i]); } } } int peep(int st[]) { if (top==NULL) { printf("\n STACK IS EMPTY"); return -1; } else { return(st[top]); } } /*===output======= ******************* Enter your option=1 enter the num. to be pushed on to the stack=2 ****MAIN MENU***** 1.PUSFH 2.POP 3.PEEP 4.DISPLAY 5.EXIT ******************* Enter your option=4 2 1 ****MAIN MENU***** 1.PUSFH 2.POP 3.PEEP 4.DISPLAY 5.EXIT ******************* Enter your option=2 The value deleted from the stack is :2 ****MAIN MENU***** 1.PUSFH 2.POP 3.PEEP 4.DISPLAY 5.EXIT ******************* Enter your option=3 STACK IS EMPTY The value stored at the top of the stack is=-1 ****MAIN MENU***** 1.PUSFH 2.POP 3.PEEP 4.DISPLAY 5.EXIT ******************* Enter your option=5 *//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////defition :- Create Binary tree and display and search element and deletion. * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > struct tree { int data ; struct tree *left_child ; struct tree *right_child ; }; struct tree * insert ( struct tree * ); struct tree * search ( struct tree * , int ); void display_preorder( struct tree * ); void display_postorder( struct tree * ); void display_inorder( struct tree * ); void deletion_element ( struct tree ** , int ); struct tree * findmax ( struct tree *current ); void main() { int choice , value; struct tree *root = NULL , *search_element = NULL ; while ( 1 ) { //system("cls"); printf ( " \n\n \t Binary Tree " ); printf ( " \n\n 1. Insert \n 2. Display \n 3. Search \n 4. Delete \n 0. Exit " ); printf ( " \n\n Enter your choice : " ); scanf ( "%d" , &choice ); switch ( choice ) { case 1 : root = insert ( root ); printf ( " \n Node Inserted !" ); break; case 2 : printf ( " \n\n Tree Traverse preorder : " ); display_preorder( root ); printf ( " \n\n Tree Traverse inorder : " ); display_inorder( root ); printf ( " \n\n Tree Traverse postorder : " ); display_postorder( root ); break ; case 3 : printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); search_element = search ( root ,value ); if ( search_element != NULL ) { printf ( " \n\n %d value is found ! " , search_element->data ); } else { printf ( " \n\n value not found ! "); } break; case 4 : printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); deletion_element ( &root , value ); break; case 0 : printf ( " \n\n Thank you ! " ); getch(); exit (); default : printf ( " \n\n Invalid choice ! " ); } getch(); } } void display_preorder( struct tree *root ) { if ( root != NULL ) { printf ( " %d " , root->data ); display_preorder( root->left_child ) ; display_preorder( root->right_child ) ; } } void display_postorder( struct tree *root ) { if ( root != NULL ) { display_postorder( root->left_child ) ; display_postorder( root->right_child ) ; printf ( " %d " , root->data ); } } void display_inorder( struct tree *root ) { if ( root != NULL ) { display_inorder( root->left_child ) ; printf ( " %d " , root->data ); display_inorder( root->right_child ) ; } } struct tree *insert ( struct tree *root ) { int value ; int c ; struct tree *current , *previous , *new_node ; current = previous = root ; printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); new_node = ( struct tree * ) malloc ( sizeof ( struct tree ) ); new_node->data = value ; new_node->left_child = NULL ; new_node->right_child = NULL ; if ( root == NULL ) { root = new_node ; } else { while ( current != NULL ) { previous = current ; if ( current->data < value ) current = current->right_child ; else current = current->left_child ; } if ( previous->data < value ) previous->right_child = new_node ; else previous->left_child = new_node ; } return root ; } struct tree * search ( struct tree *root , int value) { while ( root != NULL ) { if ( root->data == value ) return root ; else if ( root->data > value ) root = root->left_child ; else root = root->right_child ; } return root ; } void deletion_element ( struct tree **root , int value ) { struct tree *current = *root ; struct tree *parent = *root ; struct tree *temp = NULL ; int value1 , c ; while ( current != NULL && current->data != value ) { parent = current ; if ( current->data > value ) current = current->left_child ; else current = current->right_child ; } if ( current == NULL ) { printf ( " \n\n Node does not Exits ! " ); } else { // select case ... if ( current->left_child == NULL && current->right_child == NULL ) // leaf node c = 1 ; else if ( current->left_child == NULL || current->right_child == NULL ) // A node have 1 child c = 2; else // A node have 2 child c = 3 ; switch ( c ) { case 1 : if ( current == parent ) { *root = NULL ; } else { if ( parent->left_child == current ) parent->left_child = NULL ; else parent->right_child = NULL ; } free ( current ); break ; case 2 : if ( current == *root ) { if ( (*root)->left_child == NULL ) (*root) = (*root)->right_child; else (*root) = (*root)->left_child; } else { if ( parent->left_child == current ) { if ( current->left_child == NULL ) parent->left_child = current->right_child ; else parent->left_child = current->left_child ; } else { if ( current->left_child == NULL ) parent->right_child = current->right_child ; else parent->right_child = current->left_child ; } } free ( current ); break ; case 3 : temp = findmax ( current->left_child ); value1 = temp->data ; deletion_element ( &(*root) , value1 ); current->data = value1 ; break; } } } struct tree * findmax ( struct tree *current ) { if ( current != NULL ) while ( current->right_child != NULL ) current = current->right_child ; return current; } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 1 Enter value : 10 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 1 Enter value : 20 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 1 Enter value : 30 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 1 Enter value : 40 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 2 Tree Traverse preorder : 10 20 30 40 Tree Traverse inorder : 10 20 30 40 Tree Traverse postorder : 40 30 20 10 Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 3 Enter value : 20 20 value is found ! Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 4 Enter value : 20 Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 2 Tree Traverse preorder : 10 30 40 Tree Traverse inorder : 10 30 40 Tree Traverse postorder : 40 30 10 Binary Tree 1. Insert 2. Display 3. Search 4. Delete 0. Exit Enter your choice : 0 Thank you !---------------------------------------------------------------------------------------------------------------------------------------------------------********************************************************************************************************************** * * * * subject :- data structure * * defition :- Create Binary tree and display and search element. * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > struct tree { int data ; struct tree *left_child ; struct tree *right_child ; }; struct tree * insert ( struct tree * ); struct tree * search ( struct tree * , int ); void display ( struct tree * ); void main() { int choice , value; struct tree *root = NULL , *search_element = NULL ; while ( 1 ) { //system("cls"); printf ( " \n\n \t Binary Tree " ); printf ( " \n\n 1. Insert \n 2. Display \n 3. Search \n 0. Exit " ); printf ( " \n\n Enter your choice : " ); scanf ( "%d" , &choice ); switch ( choice ) { case 1 : root = insert ( root ); printf ( " \n Node Inserted !" ); break; case 2 : printf ( " \n\n Tree Traverse in post order : " ); display ( root ); break ; case 3 : printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); search_element = search ( root ,value ); if ( search_element != NULL ) { printf ( " \n\n %d value is found ! " , search_element->data ); } else { printf ( " \n\n value not found ! "); } break; case 0 : printf ( " \n\n Thank you ! " ); getch(); exit (); default : printf ( " \n\n Invalid choice ! " ); } getch(); } } struct tree *insert ( struct tree *root ) { int value ; struct tree *current , *previous , *new_node ; current = previous = root ; printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); new_node = ( struct tree * ) malloc ( sizeof ( struct tree ) ); new_node->data = value ; new_node->left_child = NULL ; new_node->right_child = NULL ; if ( root == NULL ) { root = new_node ; } else { while ( current != NULL ) { previous = current ; if ( current->data < value ) current = current->right_child ; else current = current->left_child ; } if ( previous->data < value ) previous->right_child = new_node ; else previous->left_child = new_node ; } return root ; } void display ( struct tree *root ) { if ( root != NULL ) { display ( root->left_child ) ; display ( root->right_child ) ; printf ( " %d " , root->data ); } } struct tree * search ( struct tree *root , int value) { while ( root != NULL ) { if ( root->data == value ) return root ; else if ( root->data > value ) root = root->left_child ; else root = root->right_child ; } return root ; } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Output : Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 1 Enter value : 30 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 1 Enter value : 2 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 1 Enter value : 32 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 1 Enter value : 15 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 1 Enter value : 5 Node Inserted ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 2 Tree Traverse in post order : 5 15 2 32 30 Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 3 Enter value : 15 15 value is found ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 3 Enter value : 25 value not found ! Binary Tree 1. Insert 2. Display 3. Search 0. Exit Enter your choice : 0 Thank you !********************************************************************************************************************** * * * * subject :- data structure * * defition :- Create Binary tree and display_postorder . * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > struct tree { int data ; struct tree *left_child ; struct tree *right_child ; }; struct tree * insert ( struct tree * ); void display_postorder ( struct tree * ); void display_preorder ( struct tree * ); void display_inorder ( struct tree * ); void main() { int choice ; struct tree *root = NULL ; while ( 1 ) { system("cls"); printf ( " \n\n \t Binary Tree " ); printf ( " \n\n 1. Insert \n 2. display \n 0. Exit " ); printf ( " \n\n Enter your choice : " ); scanf ( "%d" , &choice ); switch ( choice ) { case 1 : root = insert ( root ); printf ( " \n Node Inserted !" ); break; case 2 : printf ( " \n\n Tree Traverse in post order : " ); display_postorder ( root ); printf ( " \n\n Tree Traverse in pre order : " ); display_preorder ( root ); printf ( " \n\n Tree Traverse in in-order : " ); display_inorder ( root ); break ; case 0 : printf ( " \n\n Thank you ! " ); getch(); exit (); default : printf ( " \n\n Invalid choice ! " ); } getch(); } } struct tree *insert ( struct tree *root ) { int value ; struct tree *current , *previous , *new_node ; current = previous = root ; printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); new_node = ( struct tree * ) malloc ( sizeof ( struct tree ) ); new_node->data = value ; new_node->left_child = NULL ; new_node->right_child = NULL ; if ( root == NULL ) { root = new_node ; } else { while ( current != NULL ) { previous = current ; if ( current->data < value ) current = current->right_child ; else current = current->left_child ; } if ( previous->data < value ) previous->right_child = new_node ; else previous->left_child = new_node ; } return root ; } void display_postorder ( struct tree *root ) { if ( root != NULL ) { display_postorder ( root->left_child ) ; display_postorder ( root->right_child ) ; printf ( " %d " , root->data ); } } void display_preorder ( struct tree *root ) { if ( root != NULL ) { printf ( " %d " , root->data ); display_preorder ( root->left_child ) ; display_preorder ( root->right_child ) ; } } void display_inorder ( struct tree *root ) { if ( root != NULL ) { display_inorder ( root->left_child ) ; printf ( " %d " , root->data ); display_inorder ( root->right_child ) ; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Binary Tree 1. Insert 2. display 0. Exit Enter your choice : 2 Tree Traverse in post order : 3 10 6 Tree Traverse in pre order : 6 3 10 Tree Traverse in in-order : 3 6 10 Thank you !********************************************************************************************************************** * * * subject :- data structure * * defition :- Create Binary tree and display preorder and inorder without recursive function . * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > struct tree { int data ; struct tree *left_child ; struct tree *right_child ; }; struct tree * insert ( struct tree * ); void display_preorder ( struct tree * ); void display_inorder ( struct tree * ); void push ( struct tree * , struct tree * , int *) ; struct tree * pop ( struct tree * , int * ) ; void main() { int choice ; struct tree *root = NULL ; while ( 1 ) { system("cls"); printf ( " \n\n \t Binary Tree with Non Recursive function" ); printf ( " \n\n 1. Insert \n 2. display \n 0. Exit " ); printf ( " \n\n Enter your choice : " ); scanf ( "%d" , &choice ); switch ( choice ) { case 1 : root = insert ( root ); printf ( " \n Node Inserted !" ); break; case 2 : printf ( " \n\n Tree Traverse in pre order : " ); display_preorder ( root ); printf ( " \n\n Tree Traverse in in-order : " ); display_inorder ( root ); break ; case 0 : printf ( " \n\n Thank you ! " ); getch(); exit (); default : printf ( " \n\n Invalid choice ! " ); } getch(); } } struct tree *insert ( struct tree *root ) { int value ; struct tree *current , *previous , *new_node ; current = previous = root ; printf ( " \n\n Enter value : " ); scanf ( "%d" , &value ); new_node = ( struct tree * ) malloc ( sizeof ( struct tree ) ); new_node->data = value ; new_node->left_child = NULL ; new_node->right_child = NULL ; if ( root == NULL ) { root = new_node ; } else { while ( current != NULL ) { previous = current ; if ( current->data < value ) current = current->right_child ; else current = current->left_child ; } if ( previous->data < value ) previous->right_child = new_node ; else previous->left_child = new_node ; } return root ; } void display_preorder ( struct tree *root ) { struct tree *stack[ 20 ] = { NULL } ; struct tree *temp = NULL ; int tos = -1 ; if ( root != NULL ) { push ( stack , root , &tos ) ; while ( tos >= 0 ) { temp = pop ( stack , &tos ) ; printf ( " %d " , temp->data ) ; if ( temp->right_child != NULL ) push ( stack , temp->right_child , &tos ) ; if ( temp->left_child != NULL ) push ( stack , temp->left_child , &tos ) ; } } else { printf ( " \n\n Tree is Empty ! " ); } } void display_inorder ( struct tree *root ) { struct tree *stack[ 20 ] = { NULL } ; struct tree *temp = NULL , *current ; int tos = -1 ; if ( root != NULL ) { push ( stack , root , &tos ) ; current = root->left_child ; while ( tos >= 0 ) { while ( current != NULL ) { push ( stack , current , &tos ); current = current->left_child ; } temp = pop ( stack , &tos ) ; printf ( " %d" , temp->data ); current = temp->right_child ; if ( current != NULL ) { push ( stack , current , &tos ); current = current->left_child ; } } } else { printf ( " \n\n Tree is Empty ! " ); } } void push ( struct tree *stack[] , struct tree *node , int *tos ) { if ( node != NULL ) stack [ ++(*tos) ] = node ; } struct tree * pop ( struct tree *stack[] , int *tos ) { struct tree *node = stack [ *tos ]; (*tos)--; return node ; } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 1 Enter value : 5 Node Inserted ! Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 1 Enter value : 6 Node Inserted ! Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 1 Enter value : 7 Node Inserted ! Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 1 Enter value : 3 Node Inserted ! Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 1 Enter value : 4 Node Inserted ! Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 1 Enter value : 2 Node Inserted ! Binary Tree with Non Recursive function 1. Insert 2. display 0. Exit Enter your choice : 2 Tree Traverse in pre order : 5 3 2 4 6 7 Tree Traverse in in-order : 2 3 4 5 6 7 ************************************************************************************************************************************************************************************************************************************************* * * * subject :- data structure * * defition :- circular queue * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 5 void endque(struct queue *q); void deque(struct queue *q); void display(struct queue q); struct queue { int f; int r; int arr[size]; }; void main() { struct queue q; int choice,ch; q.f=-1; q.r=-1; do { printf("\n1.make the queue"); printf("\n2.delete the element from queue"); printf("\n3.diplay queue"); printf("\n\n\nenter your choice :: "); scanf("%d",&choice); switch(choice) { case 1: endque(&q); break; case 2: deque(&q); break; case 3: display(q); break; default: printf("invalid choice"); break; } printf("\n\ndo you want to continue so enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } void endque(struct queue *q) { int val; printf("\n enter value"); scanf("%d",&val); if(q->r==size-1 && q->f==0 ) { printf("\nqueue is full"); } else if( q->f-q->r==1) { printf("\nqueue is full"); } else if(q->f==-1 && q->r==-1) { q->f++; q->r++; q->arr[q->r]=val; } else if( q->r==size-1) { q->r=0; q->arr[q->r]=val; } else { q->r++; q->arr[q->r]=val; } } void display(struct queue q) { int temp; temp=q.f; if(q.f==-1 || q.r==-1) { printf("\n queue is empty"); } else { printf("\n"); if( q.r>=q.f) { while(temp<=q.r ) { printf(" %d",q.arr[temp]); temp++; } } else if(q.f>q.r) { while(temp!=q.r) { if(temp==size) { temp=0; } else { printf(" %d",q.arr[temp]); temp++; } } printf(" %d",q.arr[temp]); } } } void deque(struct queue *q) { if(q->f==-1 && q->r==-1) { printf("\n queue empty"); } else { printf("\n deleted element is :: %d",q->arr[q->f]); if(q->f==size-1) { q->f=0; } else if(q->f==q->r) { q->f=-1; q->r=-1; } else { q->f++; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value20 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value30 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value40 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value50 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value60 queue is full do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 3 10 20 30 40 50 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 20 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value60 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 70 invalid choice do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value70 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 3 30 40 50 60 70 do you want to continue so enter(1) :: ******************************************************************************************************************************************************************************************************************************************************************************************************** * * subject :- data structure * * defition :- circular queue * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 5 void endque(struct queue *q); void deque(struct queue *q); void display(struct queue q); struct queue { int f; int r; int arr[size]; }; void main() { struct queue q; int choice,ch; q.f=-1; q.r=-1; do { printf("\n1.make the queue"); printf("\n2.delete the element from queue"); printf("\n3.diplay queue"); printf("\n\n\nenter your choice :: "); scanf("%d",&choice); switch(choice) { case 1: endque(&q); break; case 2: deque(&q); break; case 3: display(q); break; default: printf("invalid choice"); break; } printf("\n\ndo you want to continue so enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } void endque(struct queue *q) { int val; printf("\n enter value"); scanf("%d",&val); if(q->r==size-1 && q->f==0 ) { printf("\nqueue is full"); } else if( q->f-q->r==1) { printf("\nqueue is full"); } else if(q->f==-1 && q->r==-1) { q->f++; q->r++; q->arr[q->r]=val; } else if( q->r==size-1) { q->r=0; q->arr[q->r]=val; } else { q->r++; q->arr[q->r]=val; } } void display(struct queue q) { int temp; temp=q.f; if(q.f==-1 || q.r==-1) { printf("\n queue is empty"); } else { printf("\n"); if( q.r>=q.f) { while(temp<=q.r ) { printf(" %d",q.arr[temp]); temp++; } } else if(q.f>q.r) { while(temp!=q.r) { if(temp==size) { temp=0; } else { printf(" %d",q.arr[temp]); temp++; } } printf(" %d",q.arr[temp]); } } } void deque(struct queue *q) { if(q->f==-1 && q->r==-1) { printf("\n queue empty"); } else { printf("\n deleted element is :: %d",q->arr[q->f]); if(q->f==size-1) { q->f=0; } else if(q->f==q->r) { q->f=-1; q->r=-1; } else { q->f++; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value20 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value30 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value40 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value50 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value60 queue is full do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 3 10 20 30 40 50 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 20 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value60 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 70 invalid choice do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value70 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 3 30 40 50 60 70 do you want to continue so enter(1) :: ******************************************************************************************************************************************************************************************************************************************************************************************************* * * * * subject :- data structure * * defition :- convert infix to postfix * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> #include<string.h> int push(char stack[],int tos,char); int pop(char stack[],int tos,char,int); int pre(char); void main() { char stack[30],arr[30],ans[30]; int j=0,i,k=0,tos=-1,len,c=0,flag=0,t=0,a=0,b=0; printf("\n enter string :: "); scanf("%s",arr); len=strlen(arr); for(i=0;i<len;i++) { if(arr[i]=='+' || arr[i]=='-' || arr[i]=='*' || arr[i]=='/' || arr[i]=='%' || arr[i]=='^') { if(i!=0 && i!=len-1) { if(arr[i]=='^' && stack[tos]=='^') { tos=push(stack,tos,arr[i]); } else if(pre(arr[i])>pre(stack[tos])) { tos=push(stack,tos,arr[i]); } else { while(pre(arr[i])<=pre(stack[tos])) { tos=pop(stack,tos,ans,j); j++; } tos=push(stack,tos,arr[i]); } } else { flag=1; break; } } else if('('==arr[i]) { tos=push(stack,tos,arr[i]); t=i; c++; } else if( '{'==arr[i]) { tos=push(stack,tos,arr[i]); a=i; c++; } else if('['==arr[i]) { tos=push(stack,tos,arr[i]); b=i; c++; } else if(')'==arr[i] || '}'==arr[i] || ']'==arr[i]) { if(c>0) { if(arr[t]=='(' && arr[i]==')') { tos=pop(stack,tos,ans,j); j++; tos--; } else if(arr[b]=='[' && arr[i]==']') { tos=pop(stack,tos,ans,j); j++; tos--; } else if( arr[a]=='{' && arr[i]=='}') { tos=pop(stack,tos,ans,j); j++; tos--; } else { flag=1; break; } } else { flag=1; break; } k++; } else if(arr[i]>64 && arr[i]<91 || arr[i]>96 && arr[i]<123) { ans[j]=arr[i]; j++; } else { flag=1; break; } } while(tos!=-1) { tos=pop(stack,tos,ans,j); j++; } ans[j]='\0'; if(flag==0 && tos==-1 && k==c) { printf("\n%s",ans); } else { printf("\ninvalid"); } getch(); } int push(char stack[],int tos,char arr) { tos++; stack[tos]=arr; return tos; } int pop(char stack[],int tos,char ans[],int j) { if(stack[tos]!='(' || stack[tos]!='{' || stack[tos]!='[') { ans[j]=stack[tos]; } tos--; } int pre(char x) { if(x=='^') { return 3; } else if(x=='*' || x=='/' || x=='%') { return 2; } else if(x=='+' || x=='-') { return 1; } else { return 0; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ enter string :: a+b-(c*d)/g ab+cd*g/- ------------------------------------------------------------------------ enter string :: (a+{b-c})*d/e abc-+d*e/ ************************************************************************************************************************************************************************************************************************************************ * * subject :- data structure * * defition :- copy stack to another stack * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 10 int push(int stack[],int tos,int val); int isempty(int tos); int isfull(int tos); void main() { int stack[size],tos=-1,choice,ch=1,val,stack1[size],tos1=-1,temp,temp1; do { printf("\n enter value into stack :: "); scanf("%d",&val); tos=push(stack,tos,val); printf("\n do you want to enter value so enter(1) :: "); scanf("%d",&ch); }while(ch==1); temp=tos; if(temp==-1) { printf("\n stack is empty"); } else { printf("\n\n this is original stack\n"); while(temp!=-1) { printf("\n%d",stack[temp]); temp--; } } if(tos==-1) { printf("\n stack is empty"); } else { while(tos!=-1) { tos1++; stack1[tos1]=stack[tos]; tos--; } } temp1=tos1; if(temp1==-1) { printf("\n stack is empty"); } else { printf("\n\n this is copy stack\n"); while(temp1!=-1) { printf("\n%d",stack1[temp1]); temp1--; } } getch(); } int push(int stack[],int tos,int val) { if(isfull(tos)==1) { printf("\n stack is full"); } else { tos++; stack[tos]=val; } return tos; } int isempty(int tos) { if(tos==-1) { return 1; } else { return 0; } } int isfull(int tos) { if(tos==size-1) { return 1; } else { return 0; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ enter value into stack :: 10 do you want to enter value so enter(1) :: 1 enter value into stack :: 20 do you want to enter value so enter(1) :: 1 enter value into stack :: 30 do you want to enter value so enter(1) :: 1 enter value into stack :: 40 do you want to enter value so enter(1) :: 1 enter value into stack :: 50 do you want to enter value so enter(1) :: 2 this is original stack 50 40 30 20 10 this is copy stack 10 20 30 40 50 ********************************************************************************************************************************************************************************************************************************************************************** * * subject :- data structure * * defition :- decimal to binary * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> int decimal_to_binary(int arr[],int tos,int dec_num); void display(int arr[],int tos); void main() { int arr[30],tos=-1,dec_num; printf("\n\n\t enter decimal number :: "); scanf("%d",&dec_num); tos=decimal_to_binary(arr,tos,dec_num); display( arr,tos); getch(); } int decimal_to_binary(int arr[],int tos,int dec_num) { while(dec_num!=0) { tos++; arr[tos]=dec_num%2; dec_num=dec_num/2; } return tos; } void display(int arr[],int tos) { int i; printf("\n\n binary number is :: "); for(i=tos;i>=0;i--) { printf("%d",arr[i]); } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ enter decimal number :: 125 binary number is :: 1111101 -------------------------------------------------------------------- enter decimal number :: 100 binary number is :: 1100100 ************************************************************************************************************************************************************************************************************************************************ * * * subject :- data structure * * defition :- dobly linklist * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> void create_link_list(struct node *); void display_link_list(struct node *); void insert_first(struct node *); void insert_last(struct node *); void insert_before(struct node *); void insert_after(struct node *); void delete_first(struct node *); void delete_last(struct node *); void delete_node(struct node *); void swap_value(struct node *); struct node { int data; struct node *next,*previous; }; void main() { struct node *start=NULL; int ch; do { printf("\n\n Link List Operation...\n\n 1. Create \n 2. Display \n 3. Insert First \n 4. Insert Last "); printf("\n 5. Insert Before \n 6. Insert After \n 7. Delete node fisrt \n 8. Delete node last "); printf("\n 9. Delete node \n 10. Swap Vale \n 0. Exit"); printf("\n\n Enter your chioice : "); scanf("%d",&ch); switch(ch) { case 0 : printf("\n\n Thank you...!"); getch(); break; case 1 : create_link_list(&start); break; case 2 : display_link_list(start); break; case 3 : insert_first(&start); break; case 4 : insert_last(&start); break; case 5 : insert_before(&start); break; case 6 : insert_after(start); break; case 7 : delete_first(&start); break; case 8 : delete_last(&start); break; case 9 : delete_node(&start); break; case 10 : swap_value(start); break; default : printf("\n\n Invalid choice...!"); } getch(); }while(ch != 0); } void create_link_list(struct node **first) { struct node *newnode = *first,*pre=NULL; int val; printf("\n\n Enter value -1 to stop insertion...!\n"); do { printf("\n\t Enter value for node : "); scanf("%d",&val); if( val != -1) { newnode = (struct node *)malloc(sizeof(struct node)); newnode->data = val; newnode->next = NULL; newnode->previous = NULL; if( newnode != NULL ) { if( *first == NULL ) { *first = newnode; pre = newnode; } else { pre->next = newnode; newnode->previous = pre; pre = newnode; } } else { printf("\n\n No space available...!"); break; } } else { printf("\n\n Link List is created...!"); break; } }while(val != -1); } void display_link_list(struct node *first) { if( first != NULL ) { printf("\n\n Link list left to right : "); while( first->next != NULL ) { printf(" %d",first->data); first = first->next; } printf(" %d",first->data); printf("\n\n Link list right to left : "); while( first->previous != NULL ) { printf(" %d",first->data); first = first->previous; } printf(" %d",first->data); } else { printf("\n\n Link List not created...!"); } } // This function is insert node at first... void insert_first(struct node **first) { struct node *new_node; int val; new_node = (struct node *)malloc(sizeof(struct node)); if(new_node != NULL) { printf("\n\n Insert First...\n"); printf("\n\t Enter value : "); scanf("%d",&new_node->data); new_node->next = NULL; new_node->previous = NULL; if( *first == NULL ) *first = new_node; else { new_node->next = *first; (*first)->previous = new_node; *first = new_node; } printf("\n\n Node is Inserted at First...!"); } else printf("\n\n Not engough space...!"); } // This function is insert node at last... void insert_last(struct node **first) { struct node *new_node,*traverse = *first; new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert Last...\n"); printf("\n\t Enter value : "); scanf("%d",&new_node->data); new_node->next = NULL; new_node->previous = NULL; if( *first == NULL ) *first = new_node; else { while( traverse->next != NULL ) traverse = traverse->next; traverse->next = new_node; new_node->previous = traverse; } printf("\n\n Node inserted at last...!"); } else printf("\n\n Not engough space...!"); } // This function is insert node before given node (value) ... void insert_node_before(struct node **first) { struct node *new_node , *temp = *first ; int value; if( *first != NULL ) { new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert Node Before...\n"); printf("\n\n Entre value where you want to add : "); scanf("%d",&value); if( temp->data == value ) { printf("\n\n Enter value : "); scanf("%d",&new_node->data); new_node->next = (*first); new_node->previous = NULL; (*first)->previous = new_node; *first = new_node; printf("\n\n Node Inserted at before %d...!",temp->data); } else { while( temp != NULL && temp->data != value ) { temp = temp->next; } if( temp != NULL ) { printf("\n\n Enter value : "); scanf("%d",&new_node->data); new_node->next = temp; new_node->previous = temp->previous; temp->previous->next = new_node; temp->previous = new_node; printf("\n\n Node Inserted at before %d...!",temp->data); } else { printf("\n\n Node not found...!"); free(new_node); } } } else printf("\n\n Not engough space...!"); } else { printf("\n\n Link List not created...!"); } } // This function is insert node after given node (value) ... void insert_node_after(struct node *first) { struct node *new_node , *temp = first ; int value; if( first != NULL ) { new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert Node After...\n"); printf("\n\n Entre value where you want to add : "); scanf("%d",&value); while( temp != NULL && temp->data != value ) { temp = temp->next; } if( temp->next == NULL ) { printf("\n\n Enter value : "); scanf("%d",&new_node->data); temp->next = new_node; new_node->previous = temp; new_node->next = NULL; } else if( temp != NULL ) { new_node->next = temp->next; temp->next->previous = new_node; new_node->previous = temp; temp->next = new_node; printf("\n\n Node Inserted at after %d...!",temp->data); } else { printf("\n\n Node not found...!"); free(new_node); } } else printf("\n\n Not engough space...!"); } else { printf("\n\n Link List not created...!"); } } // This function is delete first node ... void delete_node_first(struct node **first) { struct node *pre = *first; if( *first != NULL ) { printf("\n\n Delete Node First...\n"); if((*first)->next == NULL ) { free(pre); *first = NULL; } else { *first = pre->next; (*first)->previous = NULL; free(pre); } printf("\n\n Node is deleted...!"); } else { printf("\n\n Link List not created...!"); } } // This function is delete last node ... void delete_node_last(struct node **first) { struct node *pre = *first,*temp=NULL; if( *first != NULL ) { printf("\n\n Delete Node Last...\n"); temp = *first; while( temp->next != NULL) { temp = temp->next; } if( temp != NULL ) { if ( temp == *first ) { free(temp); *first = NULL; } else { temp->previous->next = NULL; free(temp); } } printf("\n\n Node is deleted...!"); } else { printf("\n\n Link List not created...!"); } } // This function is delete node ... void delete_node(struct node **first) { struct node *pre = *first,*temp=NULL; int value; if( *first != NULL ) { printf("\n\n Delete Node...\n"); printf("\n\t Enter value : "); scanf("%d",&value); if( pre->data == value) { if((*first)->next == NULL) { free(*first); *first = NULL; } else { *first = pre->next; (*first)->previous = NULL; free(pre); } } else { temp = *first; while( temp != NULL && temp->data != value) { temp = temp->next; } if(temp->next == NULL) { temp->previous->next = NULL; free(temp); } else if( temp != NULL ) { temp->previous->next = temp->next; temp->next->previous = temp->previous; free(temp); } else { printf("\n\n Node is not found...!"); } } printf("\n\n Node is deleted...!"); } else { printf("\n\n Link List not created...!"); } } // This function is swap the value of two given value... void swap_value(struct node *first) { struct node *node1,*node2; int temp; if( first != NULL ) { printf("\n\n Swap Value...\n"); printf("\n\n Enter first value : "); scanf("%d",&temp); node1 = first; while( node1 != NULL && node1->data != temp) node1 = node1->next; if( node1 == NULL ) printf("\n\n Node not found...!"); else { printf("\n\n Enter second value : "); scanf("%d",&temp); node2 = first; while( node2 != NULL && node2->data != temp) node2 = node2->next; if( node2 == NULL ) printf("\n\n Node not found...!"); else { temp = node1->data; node1->data = node2->data; node2->data = temp; printf("\n\n Value of Node is swaped...!"); } } } else { printf("\n\n Link List not created...!"); } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 1 Enter value for Link List (-1 for terminate)... Enter value : 10 Enter value : 20 Enter value : 30 Enter value : -1 Link List is created...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 2 Link list left to right : 10 20 30 Link list right to left : 30 20 10 Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 3 Insert First... Enter value : 25 Node is Inserted at First...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 2 Link list left to right : 25 10 20 30 Link list right to left : 30 20 10 25 Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 8 Delete Node Last... Node is deleted...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 2 Link list left to right : 25 10 20 Link list right to left : 20 10 25 Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 9 Delete Node... Enter value : 10 Node is deleted...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 0. Exit Enter your chioice : 2 Link list left to right : 25 20 Link list right to left : 20 25 */********************************************************************************************************************** * * * defition :- Double Hashing Technique. * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #include < math.h > #define SIZE 10 #define R 7 void search_value( int , int ); void display ( int ); int hash_function( int ); void assign_value( int , int , int *); void main() { int arr[SIZE]; int value , choice,i; int count = -1 ; for( i = 0 ; i < SIZE ; i++ ) arr[ i ] = 0; while ( 1 ) { printf( " \n\n 1. Insert \n 2. Search \n 3. Exit \n\n Enter choice : "); scanf( "%d" , &choice ); if ( choice == 1 ) { printf( " \n Enter value : " ); scanf("%d" , &value ); assign_value( value , arr , &count ); } else if ( choice == 2 ) { printf( " \n Enter value : " ); scanf("%d" , &value ); search_value( value , arr ); } else { printf( " \n Thank you ! " ); getch(); exit(); } getch(); } } void display ( int arr[] ) { int i = 0 ; printf("\n\n Array is : " ); for ( i = 0; i < SIZE ; i++ ) { printf(" %d " , arr[i] ); } getch(); } int hash_function( int value ) { return value % SIZE ; } int hash_function2( int value ) { return R - ( value % R ); } void assign_value( int value , int arr[], int *count) { int hash_value = hash_function( value ); int hash_value2 = hash_function2 ( value ); int i = 0 ; int index = 0 ; if( (*count) != SIZE - 1 ) { while ( 1 ) { index = ( hash_value + (i * hash_value2 ) ) % SIZE ; if( arr[ index ] == 0 ) { arr[ index ] = value ; (*count)++; display ( arr ); return ; } else { i++ ; } } } else { printf(" \n Array Full " ); } } void search_value( int value , int arr[] ) { int hash_value = hash_function( value ); int hash_value2 = hash_function2 ( value ); int i = 0 ; int index = 0 , flag=0 ; int hold_value; hold_value = ( hash_value + (i * hash_value2 ) ) % SIZE ; ; while ( 1 ) { index = ( hash_value + (i * hash_value2 ) ) % SIZE ; ; if( arr[ index ] == value ) { printf( " \n Value is Found ! " ); return ; } else { if( hold_value == index && flag == 1 ) { printf( "\n Value not found %d " , i ); return ; } else { i++ ; } flag = 1; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 10 Array is : 10 0 0 0 0 0 0 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 20 Array is : 10 20 0 0 0 0 0 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 30 Array is : 10 20 0 0 0 30 0 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 40 Array is : 10 20 40 0 0 30 0 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 60 Array is : 10 20 40 60 0 30 0 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 50 Array is : 10 20 40 60 0 30 50 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 2 Enter value : 30 Value is Found ! 1. Insert 2. Search 3. Exit Enter choice : 3 Thank you !********************************************************************************************************************** * * * defition :- Expression Tree Evaluation * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 20 struct extree { char data; struct extree *left_child ; struct extree *right_child ; }; void push ( struct extree * , struct extree * , int * ); struct extree * pop ( struct extree * , int * ); void post_order_traverse ( struct extree * ); struct extree * create_expression_tree ( char ); int expression_tree_evaluation ( struct extree *root ); int calculate ( int , int , char ) ; void main() { struct extree *root = NULL; char expression[ SIZE ] ; int result ; system ( "cls" ); printf ( "\n\n Enter postfix Expression : " ); scanf ( "%s" , expression ); root = create_expression_tree ( expression ); printf ( " \n\n Post order Traversion is : " ); post_order_traverse ( root ) ; result = expression_tree_evaluation ( root ); printf ( " \n\n Evaluation ans is : %d " , result ); printf ( " \n\n Thank You ! " ); getch(); } struct extree * create_expression_tree ( char expression[] ) { struct extree *new_node = NULL ; struct extree *stack[ SIZE ] ; int i , tos = -1; for ( i = 0 ; expression [ i ] != NULL ; i++ ) { new_node = ( struct extree * ) malloc ( sizeof ( struct extree ) ); new_node->data = expression [ i ] ; new_node->left_child = NULL ; new_node->right_child = NULL; if ( expression [ i ] == '+' || expression [ i ] == '-' || expression [ i ] == '*' || expression [ i ] == '/' ) { new_node->right_child = pop ( stack , &tos ); new_node->left_child = pop ( stack , &tos ); push ( stack , new_node , &tos ) ; } else { push ( stack , new_node , &tos ); } } return pop ( stack , &tos ); } void push ( struct extree *stack[] , struct extree *new_node , int *tos ) { (*tos)++; stack [ *tos ] = new_node ; } struct extree * pop ( struct extree *stack[] , int *tos ) { return stack [ (*tos)-- ] ; } void post_order_traverse ( struct extree *root ) { if ( root != NULL ) { post_order_traverse ( root->left_child ); post_order_traverse ( root->right_child ); printf ( " %c " , root->data ); } } int expression_tree_evaluation ( struct extree *root ) { int operand1 , operand2 ; char operators ; if ( root != NULL ) { operators = root->data ; if ( root->left_child->data >= 48 && root->left_child->data <= 57 ) { operand1 = root->left_child->data - 48 ; } else { operand1 = expression_tree_evaluation ( root->left_child ) ; } if ( root->right_child->data >= 48 && root->right_child->data <= 57 ) { operand2 = root->right_child->data - 48 ; } else { operand2 = expression_tree_evaluation ( root->right_child ) ; } return calculate ( operand1 , operand2 , operators ); } } int calculate ( int num1 , int num2 , char op ) { switch ( op ) { case '+' : return num1 + num2 ; case '-' : return num1 - num2 ; case '*' : return num1 * num2 ; case '/' : return num1 / num2 ; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Enter postfix Expression : 35*5+ Post order Traversion is : 3 5 * 5 + Evaluation ans is : 20 Thank You !********************************************************************************************************************** * * * * * defition :- Expression Tree * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 20 struct extree { char data; struct extree *left_child ; struct extree *right_child ; }; void push ( struct extree * , struct extree * , int * ); struct extree * pop ( struct extree * , int * ); void post_order_traverse ( struct extree * ); struct extree * create_expression_tree ( char ); void main() { struct extree *root = NULL; char expression[ SIZE ] ; system ( "cls" ); printf ( "\n\n Enter postfix Expression : " ); scanf ( "%s" , expression ); root = create_expression_tree ( expression ); printf ( " \n\n Post order Traversion is : " ); post_order_traverse ( root ) ; printf ( " \n\n Thank You ! " ); getch(); } struct extree * create_expression_tree ( char expression[] ) { struct extree *new_node = NULL ; struct extree *stack[ SIZE ] ; int i , tos = -1; for ( i = 0 ; expression [ i ] != NULL ; i++ ) { new_node = ( struct extree * ) malloc ( sizeof ( struct extree ) ); new_node->data = expression [ i ] ; new_node->left_child = NULL ; new_node->right_child = NULL; if ( expression [ i ] == '+' || expression [ i ] == '-' || expression [ i ] == '*' || expression [ i ] == '/' ) { new_node->right_child = pop ( stack , &tos ); new_node->left_child = pop ( stack , &tos ); push ( stack , new_node , &tos ) ; } else { push ( stack , new_node , &tos ); } } return pop ( stack , &tos ); } void push ( struct extree *stack[] , struct extree *new_node , int *tos ) { (*tos)++; stack [ *tos ] = new_node ; } struct extree * pop ( struct extree *stack[] , int *tos ) { return stack [ (*tos)-- ] ; } void post_order_traverse ( struct extree *root ) { if ( root != NULL ) { post_order_traverse ( root->left_child ); post_order_traverse ( root->right_child ); printf ( " %c " , root->data ); } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Enter postfix Expression : 35*5+ Post order Traversion is : 3 5 * 5 + Thank You !********************************************************************************************************************** * * * defition :- Representaion of Graph using linklist and Traverse using DFS Technique * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 10 int visit [ SIZE ] ; struct node { int vertex ; int weight ; struct node *next ; }; void prepare_matrix ( struct node * , int ); void display_graph ( struct node * , int ); void traverse_graph_DFS ( struct node ** , int ); void main() { int node , i , initial_node ; struct node *graph_node[ SIZE ] ; system("cls"); for ( i = 0 ; i < SIZE ; i++ ) { graph_node[ i ] = NULL ; } printf ( " \n Graph Using Linklist " ); printf ( " \n\n Enter total number of node " ); scanf ( "%d" , &node ); prepare_matrix ( graph_node , node ) ; display_graph ( graph_node , node ); printf ( " \n\n Enter Initial node : " ); scanf ( "%d" , &initial_node ); if ( initial_node < 1 || initial_node > node ) { printf ( " \n Invalid node " ); } else { printf ( " \n\n Traverse path is : " ); initial_node--; traverse_graph_DFS ( graph_node , initial_node ) ; } printf ( " \n\n Thank You ! " ); getch(); } void prepare_matrix ( struct node *graph_node[] , int node) { int i , j ; int from , to , weight , choice , weight_choice; struct node *current , *new_node; printf ( " \n\n Select type of graph .. " ); printf ( " \n 1. Directed \n 2. Undirected " ); printf( " \n Enter chocie : " ); scanf( "%d" , &choice ); printf ( " \n\n Select Weighted or not .. " ); printf ( " \n 1. Weight graph \n 2. Not weighted graph " ); printf( " \n Enter chocie : " ); scanf( "%d" , &weight_choice ); if ( choice < 1 || choice > 2 || weight_choice < 1 || weight_choice > 2) { printf ( " \n Invalid choice ... "); } else { printf ( " \n\n Enter node which want to connect ( -1 for terminate ) :" ); while ( 1 ) { new_node = ( struct node * ) malloc ( sizeof ( struct node ) ); new_node->next = NULL; printf ( " \n\n From : " ); scanf( "%d" , &from ); printf ( " \n To : " ); scanf( "%d" , &to ); if ( from == -1 || from < 1 || from > node|| to == -1 || to < 1 || to > node) { printf ( " \n Process Completed.." ); break ; } else { if ( choice == 1 ) { if ( weight_choice == 1 ) { // weighted AND directed printf ( " \n Enter Weight : " ); scanf ( "%d" , &weight ) ; new_node->vertex = --to ; new_node->weight = weight ; if ( graph_node[ --from ] == NULL ) graph_node [ from ] = new_node ; else { current = graph_node [ from ] ; while ( current->next != NULL ) current = current->next ; current->next = new_node; } } else { // not weighted AND directed new_node->vertex = --to ; new_node->weight = 1 ; if ( graph_node[ --from ] == NULL ) graph_node [ from ] = new_node ; else { current = graph_node [ from ] ; while ( current->next != NULL ) current = current->next ; current->next = new_node; } } // end if } else if ( choice == 2 ) { if ( weight_choice == 1 ) { // weighted AND undirected printf ( " \n Enter Weight : " ); scanf ( "%d" , &weight ) ; new_node->vertex = --to ; // from to new_node->weight = weight ; if ( graph_node[ --from ] == NULL ) graph_node [ from ] = new_node ; else { current = graph_node [ from ] ; while ( current->next != NULL ) current = current->next ; current->next = new_node; } //---------------------------------------------------------------------------------- // to from new_node = ( struct node * ) malloc ( sizeof ( struct node ) ); new_node->next = NULL; new_node->vertex = --to ; new_node->weight = weight ; if ( graph_node[ to] == NULL ) graph_node [ to ] = new_node ; else { current = graph_node [ to ] ; while ( current->next != NULL ) current = current->next ; current->next = new_node; } } else { // not weighted AND undirected new_node->vertex = --to ; // from to new_node->weight = 1 ; if ( graph_node[ --from ] == NULL ) graph_node [ from ] = new_node ; else { current = graph_node [ from ] ; while ( current->next != NULL ) current = current->next ; current->next = new_node; } //---------------------------------------------------------------------------------- // to from new_node = ( struct node * ) malloc ( sizeof ( struct node ) ); new_node->next = NULL; new_node->vertex = from ; new_node->weight = 1 ; if ( graph_node[ to] == NULL ) graph_node [ to ] = new_node ; else { current = graph_node [ to ] ; while ( current->next != NULL ) current = current->next ; current->next = new_node; } } // end if } } // end if } // end while } } void display_graph ( struct node *graph_node[] , int node) { int i , j ; struct node *current ; printf ( " \n\n\n Graph using linklist \n\n " ); printf ( " \n\n\n Vertex \t Adjency list \n\n " ); for ( i = 0 ; i < node ; i++ ) { printf ( "\n\n %d vertex : " , i+1 ); current = graph_node[ i ]; while ( current != NULL ) { printf ( " %d->" , current->vertex + 1 ); current = current->next ; } printf ( " NULL" ); } } // Traverse graph using Depth First Search Algorithm ( DFS ) ... // Function for DFS is : 1. push() 2. pop() 3. traverse_graph() void traverse_graph_DFS ( struct node **graph_node, int initial_node) { struct node *current ; visit [ initial_node ] = 1 ; printf ( " %d " , initial_node + 1); current = *( graph_node + initial_node ); while ( current != NULL ) { if ( visit [ current->vertex ] == 0 ) traverse_graph_DFS ( graph_node , current->vertex ); else current = current->next; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Graph Using Linklist Enter total number of node 5 Select type of graph .. 1. Directed 2. Undirected Enter chocie : 1 Select Weighted or not .. 1. Weight graph 2. Not weighted graph Enter chocie : 1 Enter node which want to connect ( -1 for terminate ) : From : 1 To : 3 Enter Weight : 10 From : 1 To : 5 Enter Weight : 15 From : 2 To : 3 Enter Weight : 12 From : 3 To : 5 Enter Weight : 16 From : -1 To : -1 Process Completed.. Graph using linklist Vertex Adjency list 1 vertex : 3-> 5-> NULL 2 vertex : 3-> NULL 3 vertex : 5-> NULL 4 vertex : NULL 5 vertex : NULL Enter Initial node : 1 Traverse path is : 1 3 5 Thank You !********************************************************************************************************************** * * * defition :- Matrxi Representaion of Graph and Traverse using BFS Technique * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 10 void prepare_matrix ( int , int ); void display_graph ( int , int ); void traverse_graph_BFS ( int , int , int ); void push ( int , int * , int * , int ); int pop ( int , int * , int * ) ; void main() { int node , initial_node; int graph_node[ SIZE ][ SIZE ] ; system("cls"); printf ( " \n Matrix Representation of Graph.. " ); printf ( " \n\n Enter total number of node " ); scanf ( "%d" , &node ); prepare_matrix ( graph_node , node ) ; display_graph ( graph_node , node ); printf ( " \n\n Enter Initial node : " ); scanf ( "%d" , &initial_node ); if ( initial_node < 1 || initial_node > node ) { printf ( " \n Invalid node " ); } else { printf ( " \n\n Traverse path is : " ); initial_node--; traverse_graph_BFS ( graph_node , node , initial_node ) ; } printf ( " \n\n Thank You ! " ); getch(); } void prepare_matrix ( int graph[][SIZE] , int node) { int i , j ; int from , to , weight , choice , weight_choice; // assign 0 to all for ( i = 0 ; i < SIZE ; i++ ) { for ( j = 0 ; j < SIZE ; j++ ) { graph[ i ][ j ] = 0 ; } } printf ( " \n\n Select type of graph .. " ); printf ( " \n 1. Directed \n 2. Undirected " ); printf( " \n Enter chocie : " ); scanf( "%d" , &choice ); printf ( " \n\n Select Weighted or not .. " ); printf ( " \n 1. Weight graph \n 2. Not weighted graph " ); printf( " \n Enter chocie : " ); scanf( "%d" , &weight_choice ); if ( choice < 1 || choice > 2 || weight_choice < 1 || weight_choice > 2) { printf ( " \n Invalid choice ... "); } else { printf ( " \n\n Enter node which want to connect ( -1 for terminate ) :" ); while ( 1 ) { printf ( " \n\n From : " ); scanf( "%d" , &from ); printf ( " \n To : " ); scanf( "%d" , &to ); if ( from == -1 || from < 1 || from > node|| to == -1 || to < 1 || to > node) { printf ( " \n Process Completed.." ); break ; } else { if ( choice == 1 ) { if ( weight_choice == 1 ) { printf ( " \n Enter Weight : " ); scanf ( "%d" , &weight ) ; graph [ --from ][ --to ] = weight ; } else { graph [ --from ][ --to ] = 1 ; } // end if } else if ( choice == 2 ) { if ( weight_choice == 1 ) { printf ( " \n Enter Weight : " ); scanf ( "%d" , &weight ) ; graph [ --from ][ --to ] = weight ; graph [ to ][ from ] = weight ; } else { graph [ --from ][ --to ] = 1 ; graph [ to ][ from ] = 1 ; } // end if } } // end if } // end while } } void display_graph ( int graph[][SIZE] , int node) { int i , j ; printf ( " \n\n Matrix Representation of graph... \n\n " ); for ( i = 0 ; i < node ; i++ ) { printf ( " %d" , i+1 ); } printf ( "\n"); for ( i = 0 ; i < node ; i++ ) { printf ( "\n %d" , i+1 ); for ( j = 0 ; j < node ; j++ ) { printf (" %d" , graph[ i ][ j ] ); } } } // Traverse graph using Depth First Search Algorithm ( BFS ) ... // Function for DFS is : 1. push() 2. pop() 3. traverse_graph() void traverse_graph_BFS ( int graph_node[][SIZE] , int node , int initial_node ) { int queue [ SIZE*2 ] = { 0 } ; int visit_node[ SIZE ] = { 0 } ; int visit , i ; int front = -1 , rear = -1 ; push ( queue , &front , &rear , initial_node ); while ( front <= rear ) { visit = pop ( queue , &front , &rear ); if ( visit_node [ visit ] == 0 ) { printf ( " %d" , visit + 1 ); visit_node [ visit ] = 1 ; for ( i = 0 ; i < node ; i++ ) { if ( graph_node [ visit ][ i ] != 0 ) { push ( queue , &front , &rear , i ); } else { // do nothing } } } else { // do nothing } } } int pop ( int queue[] , int *front , int *rear ) { int value = queue [ (*front)++ ] ; return value ; } void push ( int queue[] , int *front , int *rear , int value ) { queue [ ++(*rear) ] = value ; if ( *front == -1 ) { *front = 0 ; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Matrix Representation of Graph.. Enter total number of node 5 Select type of graph .. 1. Directed 2. Undirected Enter chocie : 1 Select Weighted or not .. 1. Weight graph 2. Not weighted graph Enter chocie : 2 Enter node which want to connect ( -1 for terminate ) : From : 1 To : 2 From : 1 To : 3 From : 1 To : 4 From : 1 To : 5 From : 2 To : 3 From : 2 To : 4 From : 2 To : 5 From : 3 To : 4 From : 3 To : 5 From : 4 To : 5 From : 5 To : 1 From : -1 To : -1 Process Completed.. Matrix Representation of graph... 1 2 3 4 5 1 0 1 1 1 1 2 0 0 1 1 1 3 0 0 0 1 1 4 0 0 0 0 1 5 1 0 0 0 0 Enter Initial node : 1 Traverse path is : 1 2 3 4 5 Thank You !********************************************************************************************************************** * * * defition :- Matrxi Representaion of Graph and Traverse using DFS Technique * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> #define row 10 #define col 10 int push(int stack[],int tos,int i); int pop(int stack[],int *tos); int dfs(int mat[][col],int i,int visited[],int tos); void main() { int mat[row][col]={0},visited[row]={0},tos=-1; int i,j,k,flag=0,node,edge,num,ch=0,choice; do { printf("\n enter node = "); scanf("%d",&node); if(node<10) { printf("\n you want to point this node to another so enter (1) = "); scanf("%d",&ch); if(ch==1) { printf("\n how many edges = "); scanf("%d",&num); for(i=0;i<num;i++) { printf("\n enter edge = "); scanf("%d",&edge); mat[node][edge]=1; } } } else { printf("\n invalid node"); } printf("\n you want to exit then enter (-1) = "); scanf("%d",&choice); }while(choice!=-1); printf("\n print graph\n\n 1 2 3 4 5 6 7 8 9\n\n"); for(i=1;i<row;i++) { printf("%d | ",i); for(j=1;j<col;j++) { printf(" %d",mat[i][j]); } printf(" |\n"); } printf("\n ***** graph traversal using dfs *****\n"); for(i=1;i<row;i++) { for(j=1;j<col;j++) { if(mat[i][j]!=0) { tos=dfs(mat,i,visited,tos); } } } getch(); } int dfs(int mat[][col],int i,int visited[],int tos) { int node; int stack[row],ans[row],j=0,k; tos=push(stack,tos,i); while(tos!=-1) { node=pop(stack,&tos); if(visited[node]==0) { visited[node]=1; printf("\n%d",node); for(k=1;k<row;k++) { if(mat[node][k]!=0 && visited[k]==0) { tos=push(stack,tos,k); } } } } return tos; } int push(int stack[],int tos,int i) { tos++; stack[tos]=i; return tos; } int pop(int stack[],int *tos) { return stack[(*tos)--]; } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ enter node = 1 you want to point this node to another so enter (1) = 1 how many edges = 3 enter edge = 3 enter edge = 5 enter edge = 7 you want to exit then enter (-1) = 1 enter node = 3 you want to point this node to another so enter (1) = 1 how many edges = 2 enter edge = 4 enter edge = 6 you want to exit then enter (-1) = 1 enter node = 4 you want to point this node to another so enter (1) = 1 how many edges = 2 enter edge = 7 enter edge = 9 you want to exit then enter (-1) = -1 print graph 1 2 3 4 5 6 7 8 9 1 | 0 0 1 0 1 0 1 0 0 | 2 | 0 0 0 0 0 0 0 0 0 | 3 | 0 0 0 1 0 1 0 0 0 | 4 | 0 0 0 0 0 0 1 0 1 | 5 | 0 0 0 0 0 0 0 0 0 | 6 | 0 0 0 0 0 0 0 0 0 | 7 | 0 0 0 0 0 0 0 0 0 | 8 | 0 0 0 0 0 0 0 0 0 | 9 | 0 0 0 0 0 0 0 0 0 | ***** graph traversal using dfs ***** 1 7 5 3 6 4 9********************************************************************************************************************** * * defition :- linear probing * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> #define SIZE 10 int linearpro(int); int insert(int,int,int hash[],int *count); void display(int hash[]); int search(int key,int val,int hash[]); int linearpro(int val) { int key; key = val % SIZE; return key; } int insert(int key,int val,int hash[],int *count) { if((*count)!= SIZE) { if(hash[key] == 0) { hash[key] = val; } else { key++; if(key == SIZE) { key = 0; } while(hash[key] != 0) { key++; } if(val != -1) { hash[key] = val; } } (*count)++; } else { printf("\n HASH is full"); } return key; } void display(int hash[]) { int i; for(i=0;i<SIZE;i++) { printf("\nHASH[%d] : %d",i,hash[i]); } } int search(int key,int val,int hash[]) { int temp=key,flag=0; if(hash[key]==val) { printf("\n%d value is found at %d index ",val,key); } else { key++; if(key == SIZE) { key = 0; } while(hash[key] != 0 && temp!=key) { if(hash[key]==val) { printf("\n%d value is found at %d index ",val,key); flag=1; break; } else { key++; } } if(flag==0) { printf("\n enter value is not found"); } } } void main() { int val,ch,key=0,count=0; int hash[SIZE]={0}; do{ printf("\n\n1. INSERT"); printf("\n2. DISPLAY"); printf("\n3. SEARCH"); printf("\n4. EXIT"); printf("\n\nENTER YOUR CHOICE : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nENTER '-1' TO STOP !!\n"); do { printf("\nEnter Value : "); scanf("%d",&val); key = linearpro(val); key = insert(key,val,hash,&count); if(count==SIZE) { break; } }while(val != -1); break; case 2: display(hash); break; case 3: printf("\n enter value you are search = "); scanf("%d",&val); key = linearpro(val); key = search(key,val,hash); break; } }while(ch != 4); getch(); } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1. INSERT 2. DISPLAY 3. SEARCH 4. EXIT ENTER YOUR CHOICE : 1 ENTER '-1' TO STOP !! Enter Value : 10 Enter Value : 20 Enter Value : 30 Enter Value : 40 Enter Value : 50 Enter Value : -1 1. INSERT 2. DISPLAY 3. SEARCH 4. EXIT ENTER YOUR CHOICE : 2 HASH[0] : 10 HASH[1] : 20 HASH[2] : 30 HASH[3] : 40 HASH[4] : 50 HASH[5] : 0 HASH[6] : 0 HASH[7] : 0 HASH[8] : 0 HASH[9] : 0 1. INSERT 2. DISPLAY 3. SEARCH 4. EXIT ENTER YOUR CHOICE : 3 enter value you are search = 40 40 value is found at 3 index 1. INSERT 2. DISPLAY 3. SEARCH 4. EXIT ENTER YOUR CHOICE : 3 enter value you are search = 60 enter value is not found 1. INSERT 2. DISPLAY 3. SEARCH 4. EXIT ENTER YOUR CHOICE :********************************************************************************************************************** * * * defition :- maintain two stack using single array * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 4 int push(int stack[],int tos,int *tos1,int val); int pop(int stack[],int tos,int *tos1); int display(int stack[],int tos,int *tos1); int peep(int stack[],int tos,int *tos1); int isempty(int tos,int *tos1); int isfull(int tos,int *tos1); void main() { int stack[size],tos=-1,tos1=size,choice,ch=1,val; do { printf("\n 1.insert value in stack"); printf("\n 2.display stack value"); printf("\n 3.delete stack value"); printf("\n 4.value of top position"); printf("\n 5.stack is empty or not"); printf("\n 6.stack is full or not"); printf("\n\n enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nenter value :: "); scanf("%d",&val); tos=push(stack,tos,&tos1,val); break; case 2: display(stack,tos,&tos1); break; case 3: tos=pop(stack,tos,&tos1); break; case 4: peep(stack,tos,&tos1); break; case 5: if(isempty(tos,&tos1)==1) { printf("\n stack is empty"); } else { printf("\n stack is not empty"); } break; case 6: if(isfull(tos,&tos1)==1) { printf("\n stack is full"); } else { printf("\n stack is not full"); } break; default: printf("\n invalid choice"); } printf("\n do you want to continue enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } int push(int stack[],int tos,int *tos1,int val) { int ch1; printf("\n you are enter value in first stack so enter(1) and second so enter(2) :: "); scanf("%d",&ch1); if(ch1==1) { if(isfull(tos,tos1)==1) { printf("\nfirst stack is full"); } else { tos++; stack[tos]=val; } } else if(ch1==2) { if(isfull(tos,tos1)==1) { printf("\nsecond stack is full"); } else { //(*tos1)--; stack[--(*tos1)]=val; } } return tos; } int display(int stack[],int tos,int *tos1) { int temp,temp1; temp=tos; temp1=*tos1; if(temp==-1) { printf("\n first stack is empty"); } else { printf("\n first stack is :: "); while(temp!=-1) { printf(" %d",stack[temp]); temp--; } } if(temp1==size) { printf("\n second stack is empty"); } else { printf("\n second stack is :: "); while(temp1!=size) { printf(" %d",stack[temp1]); temp1++; } } } int pop(int stack[],int tos,int *tos1) { if(isempty(tos,&tos1)==1) { printf("\n stack is empty\n"); } else { printf("\ndeleted element : %d",stack[tos]); tos--; } return tos; } int peep(int stack[],int tos,int *tos1) { if(tos==-1) { printf("\n first stack is empty\n"); } else { printf("\n top value of first stack is :: %d",stack[tos]); } if(*tos1==size) { printf("\n second stack is empty\n"); } else { printf("\n top value of second stack is :: %d",stack[*tos1]); } } int isempty(int tos,int *tos1) { int temp; temp=*tos1; if(tos==-1 || temp==size) { return 1; } else { return 0; } } int isfull(int tos,int *tos1) { int temp1; temp1=*tos1; if(tos==size-1 || temp1-1 == 0 || tos >= temp1-1) { return 1; } else { return 0; } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 10 you are enter value in first stack so enter(1) and second so enter(2) :: 1 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 20 you are enter value in first stack so enter(1) and second so enter(2) :: 1 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 30 you are enter value in first stack so enter(1) and second so enter(2) :: 2 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 40 you are enter value in first stack so enter(1) and second so enter(2) :: 2 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 50 you are enter value in first stack so enter(1) and second so enter(2) :: 1 first stack is full do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 50 you are enter value in first stack so enter(1) and second so enter(2) :: 2 second stack is full do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 2 first stack is :: 20 10 second stack is :: 40 30 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 4 top value of first stack is :: 20 top value of second stack is :: 40 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 5 stack is not empty do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 6 stack is full do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 3 deleted element : 20 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 3 deleted element : 10 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 3 stack is empty do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 3 stack is empty do you want to continue enter(1) ::********************************************************************************************************************** * * defition :- Create Max Heap Tree using array * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 30 void display ( int [] , int ); int max_heap_tree ( int [] ); void main() { int ch; int tree [ SIZE ] = { 0 } ; int size ; system ("cls" ); while ( 1 ) { printf ( " \n\n Max Binary Heap Tree " ); printf ( " \n\n 1. Make Max Heap Tree \n 2. Display Max heap tree \n 3. Exit " ) ; printf ( " \n\n Enter your choice : " ) ; scanf ( "%d" , &ch ); switch ( ch ) { case 1 : size = max_heap_tree ( tree ); break ; case 2 : printf ( " \n\nMax Heap Tree is :: \n" ) ; display ( tree , size ); break; case 3 : printf ( " \n\n Thank You ! " ); getch(); exit () ; default : printf ( " \n\n Invalid choice !" ); } getch(); } } int max_heap_tree ( int tree[] ) { int current = 0 , position = 0 ; int i = 0 ; char continues = 'y'; int value , temp ; printf ( " \n Enter value : " ); scanf ( "%d" , &value ) ; tree [ i ] = value ; while ( 1 ) { printf ( " \n do you want to continue ? ( y/n ) : " ); scanf ( " %c" , &continues ); if ( continues != 'y' && continues != 'Y' ) return i ; else { printf ( " \n Enter value : " ); scanf ( "%d" , &value ) ; tree [ ++i ] = value ; position = i ; current = ( position - 1 ) / 2 ; while ( tree [ current ] < tree [ position ] ) { temp = tree [ position ]; tree [ position ] = tree [ current ]; tree [ current ] = temp ; position = current ; current = position / 2 ; } } } return i ; } void display ( int tree[] , int size ) { int i ; for ( i = 0 ; i <= size ; i++ ) { printf ( "\n [ %d ] :: %d" , i , tree [ i ] ); } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Max Binary Heap Tree 1. Make Max Heap Tree 2. Display Max heap tree 3. Exit Enter your choice : 1 Enter value : 5 do you want to continue ? ( y/n ) : y Enter value : 15 do you want to continue ? ( y/n ) : y Enter value : 20 do you want to continue ? ( y/n ) : y Enter value : 10 do you want to continue ? ( y/n ) : y Enter value : 18 do you want to continue ? ( y/n ) : y Enter value : 25 do you want to continue ? ( y/n ) : n Max Binary Heap Tree 1. Make Max Heap Tree 2. Display Max heap tree 3. Exit Enter your choice : 2 Max Heap Tree is :: [ 0 ] :: 20 [ 1 ] :: 18 [ 2 ] :: 25 [ 3 ] :: 5 [ 4 ] :: 10 [ 5 ] :: 15 Max Binary Heap Tree 1. Make Max Heap Tree 2. Display Max heap tree 3. Exit Enter your choice : 3 Thank You ! *********************************************************************************************************************** * * * defition :- Create Min Heap Tree using array * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 30 void display ( int [] , int ); int Min_heap_tree ( int [] ); void main() { int ch; int tree [ SIZE ] = { 0 } ; int size ; system ("cls" ); while ( 1 ) { printf ( " \n\n Min Binary Heap Tree " ); printf ( " \n\n 1. Make Min Heap Tree \n 2. Display Min heap tree \n 3. Exit " ) ; printf ( " \n\n Enter your choice : " ) ; scanf ( "%d" , &ch ); switch ( ch ) { case 1 : size = Min_heap_tree ( tree ); break ; case 2 : printf ( " \n\nMin Heap Tree is :: \n" ) ; display ( tree , size ); break; case 3 : printf ( " \n\n Thank You ! " ); getch(); exit () ; default : printf ( " \n\n Invalid choice !" ); } getch(); } } int Min_heap_tree ( int tree[] ) { int current = 0 , position = 0 ; int i = 0 ; char continues = 'y'; int value , temp ; printf ( " \n Enter value : " ); scanf ( "%d" , &value ) ; tree [ i ] = value ; while ( 1 ) { printf ( " \n do you want to continue ? ( y/n ) : " ); scanf ( " %c" , &continues ); if ( continues != 'y' && continues != 'Y' ) return i ; else { printf ( " \n Enter value : " ); scanf ( "%d" , &value ) ; tree [ ++i ] = value ; position = i ; current = ( position - 1 ) / 2 ; while ( tree [ current ] > tree [ position ] ) { temp = tree [ position ]; tree [ position ] = tree [ current ]; tree [ current ] = temp ; position = current ; current = position / 2 ; } } } return i ; } void display ( int tree[] , int size ) { int i ; for ( i = 0 ; i <= size ; i++ ) { printf ( "\n [ %d ] :: %d" , i , tree [ i ] ); } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Min Binary Heap Tree 1. Make Min Heap Tree 2. Display Min heap tree 3. Exit Enter your choice : 1 Enter value : 5 do you want to continue ? ( y/n ) : y Enter value : 15 do you want to continue ? ( y/n ) : y Enter value : 20 do you want to continue ? ( y/n ) : y Enter value : 10 do you want to continue ? ( y/n ) : y Enter value : 18 do you want to continue ? ( y/n ) : y Enter value : 25 do you want to continue ? ( y/n ) : n Min Binary Heap Tree 1. Make Min Heap Tree 2. Display Min heap tree 3. Exit Enter your choice : 2 Min Heap Tree is :: [ 0 ] :: 5 [ 1 ] :: 10 [ 2 ] :: 20 [ 3 ] :: 15 [ 4 ] :: 18 [ 5 ] :: 25********************************************************************************************************************** * * * * defition :- 7.Write a program to test whether a given string is palindrome * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> int push(char stack[],int tos,char); void main() { char stack[30],arr[30]; int i,j,len,len1,tos=-1,flag=0; printf("\n enter string :: "); scanf("%s",arr); len=strlen(arr); printf("\n string :: %s",arr); if(len%2==0) { for(i=0;i<len/2;i++) { tos=push(stack,tos,arr[i]); } for(i=tos,j=len/2;i>=0;i--,j++) { if(stack[i]==arr[j]) { flag=1; } else { flag=0; break; } } if(flag==1) { printf("\n string is palindrom"); } else { printf("\n string is not palindrom"); } } else { for(i=0;i<len/2;i++) { tos=push(stack,tos,arr[i]); } for(i=tos,j=len/2+1;i>=0;i--,j++) { if(stack[i]==arr[j]) { flag=1; } else { flag=0; break; } } if(flag==1) { printf("\n string is palindrom"); } else { printf("\n string is not palindrom"); } } getch(); } int push(char stack[],int tos,char arr) { tos++; stack[tos]=arr; return tos; } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ enter string :: nayan string :: nayan string is palindrom ---------------------------------------------------------------------------- enter string :: gautam string :: gautam string is not palindrom *************************************************************************************************************************************************************************************************************************************************** * * * defition :- Create Priority Heap Tree using array , assume max value means high priority and it will become top position * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 30 struct heap_tree { int value ; int priority ; }; void display ( struct heap_tree [] , int ); int max_heap_tree ( struct heap_tree [] ); void main() { int ch; struct heap_tree tree [ SIZE ] ; int size ; system ("cls" ); while ( 1 ) { printf ( " \n\n Max Binary Heap Tree " ); printf ( " \n\n 1. Make Max Heap Tree \n 2. Display Max heap tree \n 3. Exit " ) ; printf ( " \n\n Enter your choice : " ) ; scanf ( "%d" , &ch ); switch ( ch ) { case 1 : size = max_heap_tree ( tree ); break ; case 2 : printf ( " \n\nMax Heap Tree is :: \n" ) ; display ( tree , size ); break; case 3 : printf ( " \n\n Thank You ! " ); getch(); exit () ; default : printf ( " \n\n Invalid choice !" ); } getch(); } } int max_heap_tree ( struct heap_tree tree[] ) { int current = 0 , position = 0 ; int i = 0 ; char continues = 'y'; int value , priority ; struct heap_tree temp ; printf ( " \n Enter value : " ); scanf ( "%d" , &value ) ; printf ( " \n Enter priority : " ); scanf ( "%d" , &priority ) ; tree [ i ].value = value ; tree [ i ].priority = priority ; while ( 1 ) { printf ( " \n do you want to continue ? ( y/n ) : " ); scanf ( " %c" , &continues ); if ( continues != 'y' && continues != 'Y' ) return i ; else { printf ( " \n Enter value : " ); scanf ( "%d" , &value ) ; printf ( " \n Enter priority : " ); scanf ( "%d" , &priority ) ; tree [ ++i ].value = value ; tree [ i ].priority = priority ; position = i ; current = ( position - 1 ) / 2 ; while ( tree [ current ].priority < tree [ position ].priority ) { temp = tree [ position ]; tree [ position ] = tree [ current ]; tree [ current ] = temp ; position = current ; current = (position - 1 ) / 2 ; } } } return i ; } void display ( struct heap_tree tree[] , int size ) { int i ; printf ( " \n\n Value : " ); for ( i = 0 ; i <= size ; i++ ) { printf ( " %2d" , tree [ i ].value ); } printf ( " \n\n priority : " ); for ( i = 0 ; i <= size ; i++ ) { printf ( " %2d" , tree [ i ].priority ); } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Max Binary Heap Tree 1. Make Max Heap Tree 2. Display Max heap tree 3. Exit Enter your choice : 1 Enter value : 23 Enter priority : 2 do you want to continue ? ( y/n ) : y Enter value : 8 Enter priority : 1 do you want to continue ? ( y/n ) : y Enter value : 14 Enter priority : 3 do you want to continue ? ( y/n ) : y Enter value : 30 Enter priority : 2 do you want to continue ? ( y/n ) : y Enter value : 80 Enter priority : 2 do you want to continue ? ( y/n ) : y Enter value : 50 Enter priority : 1 do you want to continue ? ( y/n ) : y Enter value : 45 Enter priority : 3 do you want to continue ? ( y/n ) : n Max Binary Heap Tree 1. Make Max Heap Tree 2. Display Max heap tree 3. Exit Enter your choice : 2 Max Heap Tree is :: Value : 14 30 45 8 80 50 23 priority : 3 2 3 1 2 1 2********************************************************************************************************************** * * * defition :- Quadratic probing Technique. * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #include < math.h > #define SIZE 5 void search_value( int , int ); void display ( int ); int hash_function( int ); void assign_value( int , int , int *); void main() { int arr[SIZE]; int value , choice,i; int count = -1 ; for( i = 0 ; i < SIZE ; i++ ) arr[ i ] = 0; while ( 1 ) { printf( " \n\n 1. Insert \n 2. Search \n 3. Exit \n\n Enter choice : "); scanf( "%d" , &choice ); if ( choice == 1 ) { printf( " \n Enter value : " ); scanf("%d" , &value ); assign_value( value , arr , &count ); } else if ( choice == 2 ) { printf( " \n Enter value : " ); scanf("%d" , &value ); search_value( value , arr ); } else { printf( " \n Thank you ! " ); getch(); exit(); } getch(); } } void display ( int arr[] ) { int i = 0 ; printf("\n\n Array is : " ); for ( i = 0; i < SIZE ; i++ ) { printf(" %d " , arr[i] ); } getch(); } int hash_function( int value ) { return value % SIZE ; } void assign_value( int value , int arr[], int *count) { int hash_value = hash_function( value ); int i = 0 ; int index = 0 ; if( (*count) != SIZE - 1 ) { while ( 1 ) { index = ( hash_value + (i*i) ) % SIZE ; if( arr[ index ] == 0 ) { arr[ index ] = value ; (*count)++; display ( arr ); return ; } else { i++ ; } } } else { printf(" \n Array Full " ); } } void search_value( int value , int arr[] ) { int hash_value = hash_function( value ); int i = 0 ; int index = 0 , flag=0 ; int hold_value; hold_value = ( hash_value + (i*i) ) % SIZE ; while ( 1 ) { index = ( hash_value + (i*i) ) % SIZE ; if( arr[ index ] == value ) { printf( " \n Value is Found ! " ); return ; } else { if( hold_value == index && flag == 1 ) { printf( "\n Value not found %d " , i ); return ; } else { i++ ; } flag = 1; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 10 Array is : 10 0 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 15 Array is : 10 15 0 0 0 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 20 Array is : 10 15 0 0 20 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 25 Array is : 10 15 0 25 20 1. Insert 2. Search 3. Exit Enter choice : 1 Enter value : 17 Array is : 10 15 17 25 20 1. Insert 2. Search 3. Exit Enter choice : 2 Enter value : 17 Value is Found ! 1. Insert 2. Search 3. Exit Enter choice : 3 Thank you !********************************************************************************************************************** * * defition :- queue using linklist * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> struct node* endqueue(struct node *head); struct node* dequeue (struct node *head); struct node* display(struct node *head); struct node { int data; struct node *next; }; void main() { int num=0, choice,con=0; struct node *head=NULL,*temp1; do { printf("1.insert value in queue\n2.delete value from queue \n3.display queue"); printf("\n\n enter your choice :: "); scanf("%d",&choice); switch(choice) { case 1: temp1=endqueue(head); break; case 2: temp1=dequeue(temp1); break; case 3: display(temp1); break; default: printf("\ninvalid choice"); break; } printf("\ncontinu so enter (1) :: "); scanf("%d",&con); }while(con==1); getch(); } struct node* endqueue(struct node *head) { struct node *new_node,*current,*temp; int ch; printf("\n\n you are not enter more value so enter (-1)\n"); do{ new_node=(struct node *)malloc(sizeof(struct node)); printf("\nEnter Value :"); scanf("%d",&ch); if(ch!=-1) { new_node->data=ch; new_node->next=NULL; if(head==NULL) { head=new_node; current=new_node; current->next=NULL; } else { current->next=new_node; current=new_node; current->next=NULL; } } }while(ch!=-1); return head; } struct node* dequeue(struct node *head) { //struct node *temp1; if(head==NULL) { printf("\n queue is empty"); } else { printf("\n%d value is deleted",head->data); head=head->next; } return head; } struct node* display(struct node *head) { struct node *temp1; temp1=head; //printf("display"); if(temp1==NULL) { printf("\n queue is empty"); } else { while(temp1!=NULL) { printf("\n%d",temp1->data); temp1=temp1->next; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ 1.insert value in queue 2.delete value from queue 3.display queue enter your choice :: 1 you are not enter more value so enter (-1) Enter Value :10 Enter Value :20 Enter Value :30 Enter Value :40 Enter Value :50 Enter Value :-1 continu so enter (1) :: 1 1.insert value in queue 2.delete value from queue 3.display queue enter your choice :: 3 10 20 30 40 50 continu so enter (1) :: 1 1.insert value in queue 2.delete value from queue 3.display queue enter your choice :: 2 10 value is deleted continu so enter (1) :: 1 1.insert value in queue 2.delete value from queue 3.display queue enter your choice :: 2 20 value is deleted continu so enter (1) :: 1 1.insert value in queue 2.delete value from queue 3.display queue enter your choice :: 3 30 40 50 continu so enter (1) :: *********************************************************************************************************************************************************************************************************************************************************** * * * defition :- queue * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 10 void endque(struct queue *q); void deque(struct queue *q); void display(struct queue q); struct queue { int f; int r; int arr[size]; }; void main() { struct queue q; int choice,ch; q.f=-1; q.r=-1; do { printf("\n1.make the queue"); printf("\n2.delete the element from queue"); printf("\n3.diplay queue"); printf("\n\n\nenter your choice :: "); scanf("%d",&choice); switch(choice) { case 1: endque(&q); break; case 2: deque(&q); break; case 3: display(q); break; default: printf("invalid choice"); break; } printf("\n\ndo you want to continue so enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } void endque(struct queue *q) { int val; printf("\n enter value = "); scanf("%d",&val); if(q->r==size-1) { printf("\n queue is full"); } else if(q->f==-1 && q->r==-1) { q->f++; q->r++; q->arr[q->r]=val; } else { q->r++; q->arr[q->r]=val; } } void display(struct queue q) { int temp; temp=q.f; if(q.f==-1 || q.r==-1 || q.f>q.r) { printf("\n queue is empty"); } else { printf("\n"); while(temp<=q.r) { printf(" %d ",q.arr[temp]); temp++; } } } void deque(struct queue *q) { if(q->f==-1 && q->r==-1) { printf("\n queue empty"); } else { if(q->f>q->r) { printf("\n queue empty"); q->f=-1; q->r=-1; } else { printf("\n deleted element is :: %d",q->arr[q->f]); q->f++; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 15 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 25 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 3 10 15 25 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 15 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 deleted element is :: 25 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 2 queue empty do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 3 queue is empty do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 10 invalid choice do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 10 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 20 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 30 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 40 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 50 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 60 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 70 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 80 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 90 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 11 do you want to continue so enter(1) :: 1 1.make the queue 2.delete the element from queue 3.diplay queue enter your choice :: 1 enter value = 12 queue is full do you want to continue so enter(1) ::********************************************************************************************************************** * * * defition :- quick sort * ************************************************************************************************************************ #include<conio.h> #include<stdio.h> #define size 50 void quicksort(int [],int,int); void main() { int arr[size],no,i,j; printf("\nHow many number you want to enter = "); scanf("%d",&no); for(i=0;i<no;i++) { printf("Enter a number = "); scanf("%d",&arr[i]); } quicksort(arr,0,no-1); printf("\nSorted array is = "); for(j=0;j<no;j++) { printf("%4d",arr[j]); } getch(); } void quicksort(int arr[],int f,int l) { int pivot,temp,j,i; if(f<l) { pivot=f; i=f; j=l; while(i<j) { while(arr[i]<=arr[pivot]&&i<l) { i++; } while(arr[j]>arr[pivot]) { j--; } if(i<j) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } temp=arr[pivot]; arr[pivot]=arr[j]; arr[j]=temp; quicksort(arr,f,j-1); quicksort(arr,j+1,l); } } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ How many number you want to enter = 5 Enter a number = 25 Enter a number = 12 Enter a number = 26 Enter a number = 30 Enter a number = 35 Sorted array is = 12 25 26 30 35 ********************************************************************************************************************************************************************************************************************************************* * * * defition :- reverse queue using stack...... * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 10 void endque(struct queue *q); void reverse(struct queue *q); void display(struct queue q); struct queue { int f; int r; int tos; int arr[size]; int stack[size]; }; void main() { struct queue q; int choice,ch; q.f=-1; q.r=-1; q.tos=-1; do { printf("\n1.make the queue"); printf("\n2.diplay queue"); printf("\n3.reverse queue"); printf("\n\n\nenter your choice :: "); scanf("%d",&choice); switch(choice) { case 1: endque(&q); break; case 2: display(q); break; case 3: reverse(&q); break; default: printf("invalid choice"); break; } printf("\n\ndo you want to continue so enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } void endque(struct queue *q) { int val; printf("\n enter value :: "); scanf("%d",&val); if(q->r==size-1) { printf("\n queue is full"); } else if(q->f==-1 && q->r==-1) { q->f++; q->r++; q->arr[q->r]=val; } else { q->r++; q->arr[q->r]=val; } } void display(struct queue q) { int temp; temp=q.f; if(q.f==-1 || q.r==-1 || q.f>q.r) { printf("\n queue is empty\n"); } else { printf("\n"); while(temp<=q.r) { printf("\n%d\n",q.arr[temp]); temp++; } } if(q.tos==-1) { printf("\n queue is not reverse\n"); } else { printf("\n queue is reverse\n"); while(q.tos!=-1) { printf("\n%d",q.stack[q.tos]); q.tos--; } } } void reverse(struct queue *q) { int temp1; temp1=q->f; if(temp1==-1 && q->r==-1) { printf("\n queue empty"); } else { while(temp1<=q->r) { q->tos++; q->stack[q->tos]=q->arr[temp1]; temp1++; } printf("\n queue is reverse \n"); } } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 1 enter value :: 10 do you want to continue so enter(1) :: 1 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 20 invalid choice do you want to continue so enter(1) :: 1 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 1 enter value :: 20 do you want to continue so enter(1) :: 1 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 1 enter value :: 30 do you want to continue so enter(1) :: 1 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 2 10 20 30 queue is not reverse do you want to continue so enter(1) :: 1 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 3 queue is reverse do you want to continue so enter(1) :: 1 1.make the queue 2.diplay queue 3.reverse queue enter your choice :: 2 10 20 30 queue is reverse 30 20 10 do you want to continue so enter(1) :: ************************************************************************************************************************************************************************************************************************************************************* * * * * defition :- Seprate Chaining Technique. * ************************************************************************************************************************ #include < stdio.h > #include < conio.h > #define SIZE 5 struct node { int value ; struct node *next ; }; void search_value( int , struct node * ); void assign_value( int , struct node * ); void display ( struct node * ); void main() { struct node *arr[SIZE]; int i = 0; int value , choice ; system("cls"); for( i = 0 ; i < SIZE ; i++ ) { arr[i] = NULL ; } while ( 1 ) { printf( " \n\n 1. Insert \n 2. Search \n 3. Display \n 4. Exit \n\n Enter choice : "); scanf( "%d" , &choice ); if ( choice == 1 ) { printf( " \n Enter value : " ); scanf("%d" , &value ); assign_value( value , arr ); } else if ( choice == 2 ) { printf( " \n Enter value : " ); scanf("%d" , &value ); search_value( value , arr ); } else if ( choice == 3 ) { display ( arr ) ; } else { printf( " \n Thank you ! " ); getch(); exit(0); } getch(); } getch(); } int hash_function( int value ) { return value % SIZE ; } void assign_value( int value , struct node *arr[] ) { int hash_value = hash_function( value ); int i = 0 ; struct node *current = arr[ hash_value ] ; struct node *new_node; new_node = ( struct node * ) malloc ( sizeof( struct node ) ) ; new_node->value = value; new_node->next = NULL ; if( arr[ hash_value ] == NULL ) { arr[ hash_value ] = new_node ; } else { while ( current->next != NULL ) { current = current->next ; } current->next = new_node; } display ( arr ) ; } void search_value( int value , struct node *arr[] ) { int hash_value = hash_function( value ); int i = 0 ; struct node *current = arr[ hash_value ] ; if( arr[ hash_value ] == NULL ) { printf(" value not found " ); } else { while ( current != NULL && current->value != value ) { current = current->next ; } if( current != NULL ) { printf(" Value found ! " ); } else { printf(" Value not found ! " ); } } } void display ( struct node *arr[] ) { int i = 0 ; struct node *current; printf( " \n\n Value stored .. " ); for ( i = 0 ; i < SIZE ; i++ ) { current = arr [ i ] ; printf( "\n\n %d Link List : " , i + 1 ); while( current != NULL ) { printf( " %d -> " , current->value ); current = current->next ; } printf( " NULL \n" ); } getch(); } *********************************************************************************************************************** ------------------------------------------------------------------------ code ------------------------------------------------------------------------ Enter choice : 1 Enter value : 15 Value stored .. 1 Link List : 10 -> 15 -> NULL 2 Link List : NULL 3 Link List : NULL 4 Link List : NULL 5 Link List : NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 20 Value stored .. 1 Link List : 10 -> 15 -> 20 -> NULL 2 Link List : NULL 3 Link List : NULL 4 Link List : NULL 5 Link List : NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 30 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : NULL 3 Link List : NULL 4 Link List : NULL 5 Link List : NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 17 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : NULL 3 Link List : 17 -> NULL 4 Link List : NULL 5 Link List : NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 16 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : 16 -> NULL 3 Link List : 17 -> NULL 4 Link List : NULL 5 Link List : NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 18 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : 16 -> NULL 3 Link List : 17 -> NULL 4 Link List : 18 -> NULL 5 Link List : NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 19 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : 16 -> NULL 3 Link List : 17 -> NULL 4 Link List : 18 -> NULL 5 Link List : 19 -> NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 26 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : 16 -> 26 -> NULL 3 Link List : 17 -> NULL 4 Link List : 18 -> NULL 5 Link List : 19 -> NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 1 Enter value : 44 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : 16 -> 26 -> NULL 3 Link List : 17 -> NULL 4 Link List : 18 -> NULL 5 Link List : 19 -> 44 -> NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 2 Enter value : 25 Value not found ! 1. Insert 2. Search 3. Display 4. Exit Enter choice : 2 Enter value : 26 Value found ! 1. Insert 2. Search 3. Display 4. Exit Enter choice : 3 Value stored .. 1 Link List : 10 -> 15 -> 20 -> 30 -> NULL 2 Link List : 16 -> 26 -> NULL 3 Link List : 17 -> NULL 4 Link List : 18 -> NULL 5 Link List : 19 -> 44 -> NULL 1. Insert 2. Search 3. Display 4. Exit Enter choice : 4 Thank you !********************************************************************************************************************** * * * defition :- Single Link List & its operation * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> void create_link_list(struct node *); void display_link_list(struct node *); void insert_first(struct node *); void insert_last(struct node *); void delete_node(struct node *); void delete_node_first(struct node *); void delete_node_last(struct node *); void insert_node_before(struct node *); void insert_node_after(struct node *); void swap_value(struct node *); void reverse_link_list(struct node *); struct node { int data; struct node *next; }; void main() { struct node *head=NULL; int ch; // clrscr(); while(1) { //system("cls"); printf("\n\n Link List Operation...\n\n 1. Create \n 2. Display \n 3. Insert First \n 4. Insert Last "); printf("\n 5. Insert Before \n 6. Insert After \n 7. Delete node fisrt \n 8. Delete node last "); printf("\n 9. Delete node \n 10. Swap Vale \n 11. Reverse \n 0. Exit"); printf("\n\n Enter your chioice : "); scanf("%d",&ch); if( ch == 0 ) { printf("\n\n Thank you...!"); getch(); exit(0); } else { switch(ch) { case 1 : create_link_list(&head); break; case 2 : display_link_list(head); break; case 3 : insert_first(&head); break; case 4 : insert_last(&head); break; case 5 : insert_node_before(&head); break; case 6 : insert_node_after(head); break; case 7 : delete_node_first(&head); break; case 8 : delete_node_last(&head); break; case 9 : delete_node(&head); break; case 10 : swap_value(head); break; case 11 : reverse_link_list(&head); break; default : printf("\n\n Invalid choice...!"); } } getch(); } } // This function is create a Link list... void create_link_list(struct node **first) { struct node *new_node = *first,*pre=NULL; int value; printf("\n\n Enter value for Link List (-1 for terminate)...\n"); while(1) { printf("\n\t Enter value : "); scanf("%d",&value); if( value != -1) { new_node = (struct node *)malloc(sizeof(struct node)); // create node and assign value... new_node->data = value; new_node->next = NULL; if( new_node != NULL ) // check memory space is available or not... { if( *first == NULL ) // check first node is empty or not... { *first = new_node; pre = new_node; } else { pre->next = new_node; pre = new_node; } } else { printf("\n\n Not engough space...!"); break; } } else { printf("\n\n Link List is created...!"); break; } } } // This function is display a Link list... void display_link_list(struct node *first) { if( first != NULL ) { printf("\n\n Link list is : "); while( first != NULL ) { printf(" %d",first->data); first = first->next; } } else { printf("\n\n Link List not created...!"); } } // This function is insert node at first... void insert_first(struct node **first) { struct node *new_node; new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert First...\n"); printf("\n\t Enter value : "); scanf("%d",&new_node->data); new_node->next = NULL; if( *first == NULL ) *first = new_node; else { new_node->next = *first; *first = new_node; } printf("\n\n Node is Inserted at First...!"); } else printf("\n\n Not engough space...!"); } // This function is insert node at last... void insert_last(struct node **first) { struct node *new_node,*traverse = *first;; new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert Last...\n"); printf("\n\t Enter value : "); scanf("%d",&new_node->data); new_node->next = NULL; if( *first == NULL ) *first = new_node; else { while( traverse->next != NULL ) traverse = traverse->next; traverse->next = new_node; } printf("\n\n Node inserted at last...!"); } else printf("\n\n Not engough space...!"); } // This function is delete node ... void delete_node(struct node **first) { struct node *pre = *first,*temp=NULL; int value; if( *first != NULL ) { printf("\n\n Delete Node...\n"); printf("\n\t Enter value : "); scanf("%d",&value); if( pre->data == value) { *first = pre->next; free(pre); } else { temp = *first; while( temp != NULL && temp->data != value) { pre = temp; temp = temp->next; } if( temp != NULL ) { pre->next = temp->next; free(temp); } else { printf("\n\n Node is not found...!"); } } printf("\n\n Node is deleted...!"); } else { printf("\n\n Link List not created...!"); } } // This function is delete first node ... void delete_node_first(struct node **first) { struct node *pre = *first; if( *first != NULL ) { printf("\n\n Delete Node First...\n"); *first = pre->next; free(pre); printf("\n\n Node is deleted...!"); } else { printf("\n\n Link List not created...!"); } } // This function is delete last node ... void delete_node_last(struct node **first) { struct node *pre = *first,*temp=NULL; if( *first != NULL ) { printf("\n\n Delete Node Last...\n"); temp = *first; while( temp->next != NULL) { pre = temp; temp = temp->next; } if( temp != NULL ) { if ( temp == *first ) { free(temp); *first = NULL; } else { pre->next = NULL; free(temp); } } printf("\n\n Node is deleted...!"); } else { printf("\n\n Link List not created...!"); } } // This function is insert node before given node (value) ... void insert_node_before(struct node **first) { struct node *new_node , *temp = *first , *pre; int value; if( *first != NULL ) { new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert Node Before...\n"); printf("\n\n Entre value where you want to add : "); scanf("%d",&value); if( temp->data == value ) { printf("\n\n Enter value : "); scanf("%d",&new_node->data); new_node->next = *first; *first = new_node; printf("\n\n Node Inserted at before %d...!",temp->data); } else { while( temp != NULL && temp->data != value ) { pre = temp; temp = temp->next; } if( temp != NULL ) { printf("\n\n Enter value : "); scanf("%d",&new_node->data); pre->next = new_node; new_node->next = temp; printf("\n\n Node Inserted at before %d...!",temp->data); } else { printf("\n\n Node not found...!"); free(new_node); } } } else printf("\n\n Not engough space...!"); } else { printf("\n\n Link List not created...!"); } } // This function is insert node after given node (value) ... void insert_node_after(struct node *first) { struct node *new_node , *temp = first ; int value; if( first != NULL ) { new_node = (struct node *)malloc(sizeof(struct node)); if( new_node != NULL ) { printf("\n\n Insert Node After...\n"); printf("\n\n Entre value where you want to add : "); scanf("%d",&value); while( temp != NULL && temp->data != value ) { temp = temp->next; } if( temp != NULL ) { printf("\n\n Enter value : "); scanf("%d",&new_node->data); new_node->next = temp->next; temp->next = new_node; printf("\n\n Node Inserted at after %d...!",temp->data); } else { printf("\n\n Node not found...!"); free(new_node); } } else printf("\n\n Not engough space...!"); } else { printf("\n\n Link List not created...!"); } } // This function is swap the value of two given value... void swap_value(struct node *first) { struct node *node1,*node2; int temp; if( first != NULL ) { printf("\n\n Swap Value...\n"); printf("\n\n Enter first value : "); scanf("%d",&temp); node1 = first; while( node1 != NULL && node1->data != temp) node1 = node1->next; if( node1 == NULL ) printf("\n\n Node not found...!"); else { printf("\n\n Enter second value : "); scanf("%d",&temp); node2 = first; while( node2 != NULL && node2->data != temp) node2 = node2->next; if( node2 == NULL ) printf("\n\n Node not found...!"); else { temp = node1->data; node1->data = node2->data; node2->data = temp; printf("\n\n Value of Node is swaped...!"); } } } else { printf("\n\n Link List not created...!"); } } // This function is reverse the whole link list... void reverse_link_list(struct node **first) { struct node *current,*pre,*next; if( *first != NULL ) { current = *first; pre = next = NULL; while( current != NULL ) { next = current->next; current->next = pre; pre = current; current = next ; } *first = pre; printf("\n\n Link List is Reversed...!"); } else { printf("\n\n Link List not created...!"); } } /****************************************************** Output : Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 1 Enter value for Link List (-1 for terminate)... Enter value : 10 Enter value : 20 Enter value : 30 Enter value : -1 Link List is created...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 2 Link list is : 10 20 30 Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 3 Insert First... Enter value : 25 Node is Inserted at First...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 3 Insert First... Enter value : 21 Node is Inserted at First...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 2 Link list is : 21 25 10 20 30 Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 8 Delete Node Last... Node is deleted...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 2 Link list is : 21 25 10 20 Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 11 Link List is Reversed...! Link List Operation... 1. Create 2. Display 3. Insert First 4. Insert Last 5. Insert Before 6. Insert After 7. Delete node fisrt 8. Delete node last 9. Delete node 10. Swap Vale 11. Reverse 0. Exit Enter your chioice : 2 Link list is : 20 10 25 21 *******************************************************/********************************************************************************************************************** * * * defition :- stack using linklist * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> struct node* push(struct node *head); struct node* pop(struct node *head); struct node* peep(struct node *head); struct node* display(struct node *head); struct node { int data; struct node *next; }; void main() { int num=0, choice,con=0; struct node *head=NULL,*temp1; do { printf(" 1. push value in stack\n 2. pop value from stack \n 3.print top value\n 4.display stack "); printf("\n\nenter your choice :: "); scanf("%d",&choice); switch(choice) { case 1: temp1=push(head); break; case 2: temp1=pop(temp1); break; case 3: peep(temp1); break; case 4: display(temp1); break; default: printf("\ninvalid choice"); break; } printf("\ncontinu so enter (1) :: "); scanf("%d",&con); }while(con==1); getch(); } struct node* push(struct node *head) { struct node *new_node,*current,*temp; int ch; do{ new_node=(struct node *)malloc(sizeof(struct node)); printf("\nEnter Value :"); scanf("%d",&ch); if(ch!=-1) { new_node->data=ch; new_node->next=NULL; if(head==NULL) { head=new_node; temp=new_node; temp->next=NULL; } else { head=new_node; head->next=temp; temp=new_node; } } }while(ch!=-1); return head; } struct node* pop(struct node *head) { if(head!=NULL) { printf("\n delete value :: %d",head->data); head=head->next; return head; } else { head=NULL; printf("\n stack is empty"); } return head; } struct node* peep(struct node *head) { printf("\ntop value is :: %d",head->data); } struct node* display(struct node *head) { struct node *temp1; temp1=head; if(head==NULL) { printf("\nstack is empty"); } else { while(temp1!=NULL) { printf("\n%d",temp1->data); temp1=temp1->next; } } } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ 1. push value in stack 2. pop value from stack 3.print top value 4.display stack enter your choice :: 1 Enter Value :10 Enter Value :20 Enter Value :30 Enter Value :40 Enter Value :50 Enter Value :-1 continu so enter (1) :: 1 1. push value in stack 2. pop value from stack 3.print top value 4.display stack enter your choice :: 4 50 40 30 20 10 continu so enter (1) :: 1 1. push value in stack 2. pop value from stack 3.print top value 4.display stack enter your choice :: 2 delete value :: 50 continu so enter (1) :: 1 1. push value in stack 2. pop value from stack 3.print top value 4.display stack enter your choice :: 3 top value is :: 40 continu so enter (1) :: 1 1. push value in stack 2. pop value from stack 3.print top value 4.display stack enter your choice :: 4 40 30 20 10 continu so enter (1) :: ****************************************************************************************************************************************************************************************************************************************************** * * * defition :- stack using structure * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 10 void push(struct stack *s,int val); void pop(struct stack *s); void display(struct stack s); int peep(struct stack *s,int tos); int isempty(int tos); int isfull(int tos); struct stack { int arr[size]; int tos; }; void main() { struct stack s; int choice,ch=1,val; s.tos=-1; do { printf("\n 1.insert value in stack"); printf("\n 2.display stack value"); printf("\n 3.delete stack value"); printf("\n 4.value of top position"); printf("\n 5.stack is empty or not"); printf("\n 6.stack is full or not"); printf("\n\n enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nenter value :: "); scanf("%d",&val); push(&s,val); break; case 2: display(s); break; case 3: pop(&s); break; case 4: peep(&s,s.tos); break; case 5: if(isempty(s.tos)==1) { printf("\n stack is empty"); } else { printf("\n stack is not empty"); } break; case 6: if(isfull(s.tos)==1) { printf("\n stack is full"); } else { printf("\n stack is not full"); } break; default: printf("\n invalid choice"); } printf("\n do you want to continue enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } void push(struct stack *s,int val) { if(s->tos==size-1) { printf("\n stack is full"); } else { ++s->tos; s->arr[s->tos]=val; } } void display(struct stack s) { int i; if(s.tos==-1) { printf("\n stack is empty"); } else { for(i=s.tos;i>=0;i--) { printf("\n%d",s.arr[i]); //temp--; } } } void pop(struct stack *s) { if(s->tos==-1) { printf("\n stack is empty\n"); } else { printf("\ndeleted element : %d",s->arr[s->tos]); s->tos--; } } int peep(struct stack *s,int tos) { if(s->tos==-1) { printf("\n stack is empty\n"); } else { printf("\n%d",s->arr[tos]); } } int isempty(int tos) { int temp; temp=tos; if(temp==-1) { return 1; } else { return 0; } } int isfull(int tos) { if(tos==size-1) { return 1; } else { return 0; } } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 10 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 15 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 20 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 25 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 30 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 2 30 25 20 15 10 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 3 deleted element : 30 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 4 25 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 5 stack is not empty do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 6 stack is not full do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 2 25 20 15 10 do you want to continue enter(1) :: ******************************************************************************************************************************************************************************************************************************************************* * * * defition :- stack * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> # define size 10 int push(int stack[],int tos,int val); int pop(int stack[],int tos); int display(int stack[],int tos); int peep(int stack[],int tos); int isempty(int tos); int isfull(int tos); void main() { int stack[size],tos=-1,choice,ch=1,val; do { printf("\n 1.insert value in stack"); printf("\n 2.display stack value"); printf("\n 3.delete stack value"); printf("\n 4.value of top position"); printf("\n 5.stack is empty or not"); printf("\n 6.stack is full or not"); printf("\n\n enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nenter value :: "); scanf("%d",&val); tos=push(stack,tos,val); break; case 2: display(stack,tos); break; case 3: tos=pop(stack,tos); break; case 4: peep(stack,tos); break; case 5: if(isempty(tos)==1) { printf("\n stack is empty"); } else { printf("\n stack is not empty"); } break; case 6: if(isfull(tos)==1) { printf("\n stack is full"); } else { printf("\n stack is not full"); } break; default: printf("\n invalid choice"); } printf("\n do you want to continue enter(1) :: "); scanf("%d",&ch); }while(ch==1); getch(); } int push(int stack[],int tos,int val) { if(isfull(tos)==1) { printf("\n stack is full"); } else { tos++; stack[tos]=val; } return tos; } int display(int stack[],int tos) { int temp; temp=tos; if(temp==-1) { printf("\n stack is empty"); } else { while(temp!=-1) { printf("\n%d",stack[temp]); temp--; } } } int pop(int stack[],int tos) { if(isempty(tos)==1) { printf("\n stack is empty\n"); } else { printf("\ndeleted element : %d",stack[tos]); tos--; } return tos; } int peep(int stack[],int tos) { if(tos==-1) { printf("\n stack is empty\n"); } else { printf("\n%d",stack[tos]); } } int isempty(int tos) { if(tos==-1) { return 1; } else { return 0; } } int isfull(int tos) { if(tos==size-1) { return 1; } else { return 0; } } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 10 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 20 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 30 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 40 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 1 enter value :: 50 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 2 50 40 30 20 10 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 3 deleted element : 50 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 4 40 do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 5 stack is not empty do you want to continue enter(1) :: 1 1.insert value in stack 2.display stack value 3.delete stack value 4.value of top position 5.stack is empty or not 6.stack is full or not enter your choice : 6 stack is not full do you want to continue enter(1) :: ******************************************************************************************************************************************************************************************************************************************************** * * * defition :- symbol balancing * ************************************************************************************************************************ #include<stdio.h> #include<conio.h> #include<string.h> int push(char stack[],int tos,char); void main() { char stack[30],arr[30],ans[30]; int j=0,i,k,tos=-1,len,c=0,flag=0; printf("\n enter string :: "); scanf("%s",arr); len=strlen(arr); for(i=0;i<len;i++) { if('('==arr[i] || '{'==arr[i] || '['==arr[i]) { tos=push(stack,tos,arr[i]); c++; } else if(')'==arr[i] && stack[tos]=='(') { flag=0; tos--; } else if('}'==arr[i] && stack[tos]=='{') { flag=0; tos--; } else if(']'==arr[i] && stack[tos]=='[') { flag=0; tos--; } else { flag=1; break; } } if(flag==0 && tos==-1) { printf("\n valid"); } else { printf("\ninvalid"); } getch(); } int push(char stack[],int tos,char arr) { tos++; stack[tos]=arr; return tos; } *********************************************************************************************************************** ------------------------------------------------------------------------ output ------------------------------------------------------------------------ enter string :: ({}[]) valid ----------------------------------------------------- enter string :: ([]{}){ invalid *********************************************************************************************************************************
0 comments:
Post a Comment