0% found this document useful (0 votes)
123 views51 pages

MIC College of Technology: Write A C Program To Search An Element in The List Using Binary Search Technique

The document contains C program code for implementing various sorting algorithms like bubble sort, insertion sort, selection sort, quick sort and merge sort to sort elements in an array. It also contains code for traversing a graph using depth first search. The programs take input from the user, apply the respective algorithm to sort the array or traverse the graph, and output the sorted array or path traversed.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views51 pages

MIC College of Technology: Write A C Program To Search An Element in The List Using Binary Search Technique

The document contains C program code for implementing various sorting algorithms like bubble sort, insertion sort, selection sort, quick sort and merge sort to sort elements in an array. It also contains code for traversing a graph using depth first search. The programs take input from the user, apply the respective algorithm to sort the array or traverse the graph, and output the sorted array or path traversed.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Experiment No:

Regd no:

Write a C program to search an element in the list using binary search technique #include<stdio.h> #include<conio.h> void binsearch(int list[], int high, int target) { int low=0,mid; while(low<=high) { mid=(low+high)/2; if(list[mid]==target) break; if(target<list[mid]) high=mid-1; if(target>list[mid]) low=mid+1; } if(target==list[mid]) printf("%d is found at location %d\n",target,mid); else printf("%d is not found in the list\n",target); } int main() { int n,i,arr[100],ele; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enetr elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("Enter the element to be searched\n"); scanf("%d",&ele); binsearch(arr,n,ele); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to search an element in the list using binary search technique with recursion. #include<stdio.h> #include<conio.h> void binsearchrec(int list[], int low, int high, int target) { int mid; if(low<=high) { mid=(low+high)/2; if(list[mid]==target) printf("%d is found at position %d\n",target,mid); else if(target<list[mid]) binsearchrec(list,low,mid-1,target); else binsearchrec(list,mid+1,high,target); } else printf("%d is not found in the list\n",target); } int main() { int n,i,arr[100],ele; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enetr elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("Enter the element to be searched\n"); scanf("%d",&ele); binsearchrec(arr,0,n,ele); getch(); return 0;}

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to build a binary search tree , to display the elements in the binary search tree, to insert a node into the tree, to delete a node from the tree. #include<stdio.h> #include<conio.h> struct btree { int data; struct btree *left,*right; }; typedef struct btree node; node *root; node* inordersuc(node *p); void buildtree(node *ptr) { int ele, l,r; if(ptr!=NULL) { printf("Enter element to insert into node\n"); scanf("%d",&ele); ptr->data=ele; printf("Enter 1/0 (yes/No) to insert an element left to %d\n",ele); scanf("%d",&l); if(l==1) { ptr->left=(node*)malloc(sizeof(node)); buildtree(ptr->left); } else ptr->left=NULL; printf("Enter 1/0 (yes/No) to insert an element right to %d\n",ele); scanf("%d",&r); if(r==1) { ptr->right=(node*)malloc(sizeof(node)); buildtree(ptr->right); } else ptr->right=NULL; } }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

void inorder(node *ptr) { if(ptr!=NULL) { inorder(ptr->left); printf("%d->",ptr->data); inorder(ptr->right); } } void preorder(node *ptr) { if(ptr!=NULL) { printf("%d->",ptr->data); preorder(ptr->left); preorder(ptr->right); } } void postorder(node *ptr) { if(ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); printf("%d->",ptr->data); } } void searchBST(int item) { node *ptr=root; while(ptr!=NULL) { if(ptr->data<item) ptr=ptr->right; if(ptr->data>item) ptr=ptr->left;

if(ptr->data==item) printf("%d has found \n",item); else

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: found\n",item);

Regd no: printf("%d not

} } void insertBST(int item) { node *p,*ptr=root,*new; while(ptr!=NULL) { if(ptr->data<item) { p=ptr; ptr=ptr->right; } if(ptr->data>item) { p=ptr; ptr=ptr->left; } if(ptr->data==item) { printf("%d already exists & insertion is not possible\n",item); exit(0); } } if(ptr==NULL) { new=(node*)malloc(sizeof(node)); new->data=item; new->left=new->right=NULL; if(p->data<new->data) p->right=new; else p->left=new; } } void deleteBST(int item) { node *ptr=root,*par; int flag=0,op; while(flag==0&ptr!=NULL) { if(ptr->data<item) { par=ptr; ptr=ptr->right; if(ptr->data>item)

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: {

Regd no: par=ptr;

ptr=ptr->left; } if(ptr->data==item) flag=1; } if(flag==0) { printf("Item is not found in tree.So,deletion is not possible",item); exit(0); } if(ptr->left==NULL&&ptr->right==NULL) op=1; else if(ptr->left!=NULL&&ptr->right!=NULL) op=3; else op=2; if(op==1) { if(ptr==par->left) par->left=NULL; else par->right=NULL; free(ptr); } if(op==2) { if(ptr==par->left) { if(ptr->left==NULL) par->left=par->right; else par->left=ptr->left; } if(ptr==par->right) { if(ptr->left==NULL) ptr->right=ptr->right; else par->right=ptr->left; } free(ptr); } if(op==3) {

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: node int

Regd no: *l; temp;

l=inordersuc(ptr); temp=l->data; deleteBST(l->data); ptr->data=temp; } } } node* inordersuc(node *p) { node *t=p->right; while(t->left!=NULL) t=t->left; return t; } int main() { int item,ele; root=(node*)malloc(sizeof(node)); buildtree(root); printf("\nNodes in the tree in Inorder is:\n"); inorder(root); printf("\nNodes in the tree in Pretorder is:\n"); preorder(root); printf("\nNodes in the tree in Postorder is:\n"); postorder(root); printf("\n Enter element to serch in the tree\n"); scanf("%d",&item); searchBST(item); printf("\n Enter the element to insert into the tree\n"); scanf("%d",&ele); insertBST(ele); inorder(root); deleteBST(item); inorder(root); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to sort the elements in the array using Bubble sort technique. #include<stdio.h> #include<conio.h> void Bubblesort(int a[], int n) { int i,j,temp; i=0; while(i<n) { j=n-1; while(j>i) { if(a[j]<a[j-1]) { temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } j--; } i++; } printf("Sorted elements in the array are:\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); } int main() { int n,i,a[50]; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); Bubblesort(a,n); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to sort the elements in the array using Insertion sort technique. #include<stdio.h> #include<conio.h> void insertionsort(int a[],int n) { int i,j,t; for(j=1;j<n;j++) { t=a[j]; i=j; while(i>0&&a[i-1]>t) { a[i]=a[i-1]; i--; } a[i]=t; } printf("Sorted elements are:\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); } int main() { int n,a[50],i; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enetr the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); insertionsort(a,n); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to sort the elements in the array using Selection sort technique. #include<stdio.h> #include<conio.h> void straightselectionsort(int a[], int n) { int i,j,temp; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("Sorted elements in the list are:\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); } int main() { int a[10],n,i; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); straightselectionsort(a,n); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to sort the elements in the array using Quick sort technique. #include<stdio.h> #include<conio.h> void qsort(int a[],int left,int right) { int i=left, j=right,mid,temp; mid=a[(left+right)/2]; while(i<=j) { while(a[i]<mid&&i<j) i++; while(a[j]>mid&&i<j) j--; if(i<=j) { temp=a[i]; a[i]=a[j]; a[j]=temp; i++; j--; } } if(left<j) qsort(a,left,j); if(j<right) qsort(a,i,right); } int main() { int arr[10],n,i; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); qsort(arr,0,n-1); printf("Sorted elements are:\n"); for(i=0;i<n;i++) printf("%d\n",arr[i]); getch(); return 0;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: }

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to sort the elements in the array using Merge Sort technique. #include<stdio.h> #include<conio.h> int a[10]; int temp[10],n; void mergesort(int,int); void merge(int,int,int); int main() { int i; printf("Enter the size of the array\n"); scanf("%d",&n); printf("\nEnter the values into the array"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(0,n-1); printf("\n sorted list is:"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); return 0; } void mergesort(int low,int high) { if(low<high) { int mid=(low+high)/2; mergesort(low,mid); mergesort(mid+1,high); merge(low,mid,high); } } void merge(int low,int mid,int high) { int k,i=low,h=low,j=mid+1; while((h<=mid)&&(j<=high))

{ if(a[h]<=a[i])

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: {

Regd no:

temp[i]=a[h]; h++; } else { temp[i]=a[j]; j++; } i++; } if(h>mid) for(k=j;k<=high;i++,k++) temp[i]=a[k]; else for(k=h;k<=mid;i++,k++) temp[i]=a[k]; for(k=low;k<=high;k++) a[k]=temp[k]; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to traverse the Graph in Depth First search traversal. #include<stdio.h> #include<conio.h> struct Graph { int v; struct graph *next; }; typedef struct Graph node; node *adjlist[5]; void getnode(node);

int n,mark[5]; void createlist() { int i; printf("\n Enter the no. of nodes you want to insert"); scanf("%d",&n); printf("Enter the List:\n"); for(i=0;i<n;i++) { adjlist[i]=(node*)malloc(sizeof(node)); adjlist[i]->v=i; printf("\n Head node:%d ,[Enter -1 to end]",i); getnode(adjlist[i]); } } void getnode(node *p) { int data; printf("/n Enter adjacent node:"); scanf("%d",&data); if(data==-1) {

p->next=NULL; return; } else { p->next=(node*)malloc(sizeof(node));

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: (p->next)-

Regd no: >v=data;

getnode(p->next); } } void dfs(int h) { node *adj; int v; mark[h]=1; adj=adjlist[h]; printf("%d",h); while(adj!=NULL) { v=adj->v; if(mark[v]==0) dfs(v); adj=adj->next; } } int main() { int x; createlist(); printf("Enter the starting node to visit\n"); scanf("%d",&x); dfs(x); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to search an element in the list using Linear search technique. #include<stdio.h> #include<conio.h> void lsearch(int a[],int n,int target) { int i,flag=0; for(i=0;i<n;i++) { if(a[i]==target) { flag=1; break; } } if(flag==1) printf("%d found at location %d",target,i); else printf("%d not found in the array",target); } int main() { int n,i,a[100],ele; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Enter the target element to search in the list"); scanf("%d",&ele); lsearch(a,n,ele); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to search an element in the list using Linear search technique with recursion. #include<stdio.h> #include<conio.h> void lsearchrec(int a[], int maxsize, int i, int target); int main() { int n,i,a[100],ele; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the target element to find in the list\n"); scanf("%d",&ele); lsearchrec(a,n,0,ele); getch(); return 0; } void lsearchrec(int a[], int n, int i, int target) { if(i<=n) { if(a[i]==target) printf("%d found in the array at location %d\n",target,i); else lsearchrec(a,n,i+1,target); } else printf("%d not found in the list\n",target); }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to implement Stack using arrays. #include<stdio.h> #include<conio.h> int stack[100],top=-1,n; void push(int ele) { if(top==9) printf("Stack is full push is not possible\n"); else stack[++top]=ele; } int pop() { if(top==-1) return -1; else return stack[top--]; } void display() { int i; if(top==-1) printf("Stack is empty\n"); else { printf("Elements in the stack are:\n"); for(i=0;i<=top;i++) printf("%d\n",stack[i]); } } int main() { int item,n,ch,del; printf("Enter the size of the stack\n"); scanf("%d",&n); while(1) { printf("\nMENU \n 1. Push \n 2.Pop \n 3.Display \n 4.Exit\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch)

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: {

Regd no:

case 1: printf("Enter ur element to be inserted into the stack\n"); scanf("%d",&item); push(item); break; case 2: del=pop(); if(del==-1) printf("stack is empty"); else printf("\nPopped element from the stack is %d",del); break; case 3: display(); break; case 4: exit(0); default: printf("\nOut of choice"); } } getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to implement Stack using dynamic memory allocation. #include<stdio.h> #include<conio.h> struct stack { int data; struct stack *next; }; typedef struct stack node; node *top; void push(int ele) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->data=ele; if(top==NULL) { top=ptr; top->next=NULL; } else { ptr->next=top; top=ptr; } } int pop() { node *temp; int del; if(top==NULL) return -1; else { temp=top; top=top->next; del=temp->data; free(temp); return del; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: } void display() {

Regd no:

node *temp=top; if(temp!=NULL) { printf("Elements in the stack are:"); while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } } else printf("Stack is empty"); } int main() { int item,ch,n,del; while(1) { printf("\nMENU \n 1. Push \n 2.Pop \n 3.Display \n 4.Exit\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter ur element to be inserted into the stack\n"); scanf("%d",&item); push(item); break; case 2: del=pop(); if(del==-1) printf("stack is empty\n"); else printf("\nPopped element from the stack is %d",del);

break; case 3: display(); break; case 4: exit(0); default: printf("Out of choice"); } } return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to implement Queue using arrays. #include<stdio.h> #include<conio.h> int q[10],front=-1,rear=-1,n; void qinsert(int ele) { if(rear==9) printf("Queue is full\n"); else { rear=rear+1; q[rear]=ele; if(front==-1) front=rear; } } int qdelete() { int d; if(front==-1) return -1; else { d=q[front]; if(front==rear) front=rear=-1; else front=front+1; return d; } } void display() { int i; printf("Queue elements are:\n"); for(i=front;i<=rear;i++) printf("%d\n",q[i]); } int main() { int item,ch,n,del;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: printf("Enter the scanf("%d",&n);

Regd no: size of the queue\n");

while(1) { printf("\nMENU \n1. Insert \n 2.Delete \n 3.Display \n 4.Exit\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter ur element to be inserted into the queue\n"); scanf("%d",&item); qinsert(item); break; case 2: del=qdelete(); if(del==-1) printf("queue is empty\n"); else printf("\nDeleted element from the queue is %d",del); break; case 3: display(); break; case 4: exit(0); default: printf("\nOut of choice"); } }return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to implement queues using dynamic memory allocation. #include<stdio.h> #include<conio.h> struct queue { int data; struct queue *next; }; typedef struct queue node; node *front,*rear; void qinsert(int ele) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->data=ele; if(rear==NULL) { ptr=rear; front=rear; } else { rear->next=ptr; rear=ptr; } if(front==NULL) front=rear; rear->next=NULL; } int qdelete() { int del; if(front==NULL) return -1; else { node *temp; temp=front; front=front->next;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: del=temp->data;

Regd no:

free(temp); return(del); } } void display() { node *p=front; if(front==NULL) printf("Queue is empty"); else { printf("Elements in the queue are:"); while(p!=NULL) { printf("%d->",p->data); p=p->next; } } } int main() { int item,ch,del; while(1) { printf("\nMENU \n 1. Insert \n 2.Delete \n 3.Display \n 4.Exit\n"); printf("Enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter ur element to be inserted into the queue"); scanf("%d",&item); qinsert(item); break; case 2: del=qdelete(); if(del==-1) printf("queue is empty");

else printf("Deleted element from the queue is %d",del); break; case 3: display(); break;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: case 4:

Regd no: exit(0); break;

default: printf("\nOut of choice"); } } return 0; }

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to create a Single Linked List, to insert an element into the list, to delete an element from the list and to display the list using arrays. #include<stdio.h> #include<conio.h> int data[10],next[10],head; int gatdata(); int findloc(int key); void createll() { int ele,loc,p; printf("Enter a value b/w 0 to 9\n"); scanf("%d",&head); printf("Enter element to insert\n"); scanf("%d",&ele); data[head]=ele; next[head]=-1; loc=p=head; while(1) { printf("Enter element to insert or -1 to exit\n"); scanf("%d",&ele); if(ele==-1) break; loc=getnode(); if(loc!=-1) { data[loc]=ele; next[loc]=-1; next[p]=loc; p=loc; } } } int getnode() { int i; for(i=0;i<10;i++) if(data[i]==0)

return i;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: return -1; } void display()

Regd no:

{ int i=head; printf("Elements in the list are:\n"); while(i!=-1) { printf("%d->",data[i]); i=next[i]; } } void insertll() { int ch,ele,key,loc,i; int prev; printf("\nEnter your chioce \n 1.Begin \n 2.Middle\n3.End\n"); scanf("%d",&ch); printf("\nEnter element to insert"); scanf("%d",&ele); loc=getnode(); if(loc!=-1) { if(ch==1) { data[loc]=ele; next[loc]=head; head=loc; } if(ch==3) { i=head; while(next[i]!=-1) i=next[i]; data[loc]=ele; next[loc]=-1; next[i]=loc; } if(ch==2) { printf("Enter a key value to insert element after the key\n");

scanf("%d",&key); prev=findloc(key); if(prev!=-1)

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: {

Regd no: data[loc]=ele; next[loc]=next[prev];

next[prev]=loc; } else printf("\n%d is not an existed one",key); } } else printf("There is no empty nodes in the list\n"); } int findloc(int key) { int i=head; while(i!=-1) { if(data[i]==key) return i; i=next[i]; } return -1; } void deletell() { int dele,prev,cur; printf("Enter element to delete from the list\n"); scanf("%d",&dele); if(data[head]==dele) { data[head]=0; head=next[head]; } else { prev=findprevloc(dele); if(prev==-1) printf("\n%d is not an existed one",dele); else { cur=next[prev];

data[cur]=0; next[prev]=next[cur]; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: } }

Regd no:

int findprevloc(int key) { int p,prev; p=prev=head; while(p!=-1) { if(data[p]==key) return prev; p=prev;

p=next[p]; } return -1; } int main() { createll(); display(); insertll(); display(); deletell(); display(); getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to create a Single Linked List, to insert an element into the list, to delete an element from the list and to display the list using dynamic memory allocation.

#include<stdio.h> #include<conio.h> struct Link { int data; struct Link *next; }; typedef struct Link node; node* findkeyloc(int key); node* findprevloc(int item); node *head; void createll() { int ele; node *ptr,*prev,*p; while(1) { printf("Enter element to insert or -1 to break"); scanf("%d",&ele); if(ele==-1) break; ptr=(node*)malloc(sizeof(node)); ptr->data=ele; if(head==NULL) prev=head=ptr; else { prev->next=ptr; prev=ptr; } ptr->next=NULL; } } void insertll() { int ch,ele,key;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

node *p,*ptr,*prev;

printf("Enter your choice\n 1.Begin\n 2.Middle\n 3.End\n"); scanf("%d",&ch); printf("Enter element to insert\n"); scanf("%d",&ele); ptr=(node*)malloc(sizeof(node)); if(ch==1) { ptr->data=ele; ptr->next=head; ptr=head; } if(ch==3) { prev=head; while(prev->next!=NULL) { prev=prev->next; prev->data=ele; prev->next=ptr; ptr->next=NULL; } } if(ch==2) { printf("Enter a key value to insert after the key"); scanf("%d",&key);

p=findkeyloc(key); if(p!=NULL) { ptr->data=ele; ptr->next=p->next; p->next=ptr; } else printf("%d is not an existed one"); } } node* findkeyloc(int key)

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: { node *i; i=head; while(i!=NULL) {

Regd no:

if(i->data==key) return i; i=i->next; } return NULL; } void deletell() { int item; node *prev,*temp; printf("Enter ur element to delete from the list"); scanf("%d", &item); if(item==head->data) { temp=head; head=head->next; free(temp); } else { prev=findprevloc(item); if(prev!=NULL) { temp=prev->next; prev->next=temp->next; free(temp); } else printf("%d is not in the list",item); } } node* findprevloc(int item) { node *cur,*prev; cur=prev=head; while(cur!=NULL) { if(cur->data==item) return prev; prev=cur;

cur=cur->next;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: } return NULL; } void display() {

Regd no:

node *i=head; while(i!=NULL) { printf("%d->",i->data); i=i->next; } } int main() { int ch; while(1) { printf("1.Create\n 2.Insert\n 3.Display\n 4.Delete\n 5.Exit\n"); printf("Enter your choice"); scanf("%d",&ch); switch(ch) { case 1: createll(); break; case 2: insertll(); break; case 3: display(); break; case 4: deletell(); break; case5: exit(0); } } getch(); return 0; }

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Output:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

Write a C program to implement priority queues using heaps. #include<stdio.h> #include<conio.h> int n,tree[10],root=1; void buildheap(int i) { int ele,op,n=1; printf("Enter element to insert into tree\n"); scanf("%d",&ele); tree[i]=ele; n++; printf("Enter 1/0(yes/no) to place a left element to %d\n",ele); scanf("%d",&op); if(op==1) buildheap(2*i); printf("Enter 1/0 (yes/no) to place a right element to %d",ele); scanf("%d",&op); if(op==1) buildheap(2*i+1); } void insertheap(int item) { int i,p,temp; if(n>=10) { printf("Heap insertion is not possible\n"); return; } else { n=n+1; tree[n]=item; i=n;

p=i/2;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no:

while(p>0&&tree[p]<tree[i]) { temp=tree[i];

tree[i]=tree[p]; tree[p]=temp; i=p; p=p/2; } } } void deleteheap() { int item,i,x,y,flag,lc,rc,temp; if(n=0) { printf("HEap deletion is not possible\n"); return; } item=tree[1]; tree[1]=tree[n]; n=n-1; flag=0; i=1; while(flag==0&&i<n) { lc=2*i; rc=2*i+1; if(lc<=n) x=tree[lc]; if(rc<=n) y=tree[rc]; if(tree[i]>x&&tree[i]>y) flag=1; else { if(x>y&&tree[i]<x) { temp=tree[i];

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No:

Regd no: tree[i]=tree[lc]; tree[lc]=temp; i=lc; } else { if(y>x&&tree[i]<y) {

temp=tree[i]; tree[i]=tree[rc]; tree[rc]=temp; i=rc; } } } } printf("Deleted element from heap is %d\n",item); } void displayheap() { int k; if(tree[k]==0) printf("Empty List\n"); printf("Elements in the heap are:\n"); for(k=1;k<=10;k++) printf("%d\n",tree[k]); } int main() { int root,ele,item; buildheap(root); displayheap(); printf("Enter element you want to insert\n"); scanf("%d",&ele); insertheap(ele); displayheap(); printf("Enter element you want to delete from the heap\n"); scanf("%d",&item); deleteheap(); displayheap(); getch(); return 0;

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

Experiment No: }

Regd no:

MIC College of Technology

Devineni Venkata Ramana & Dr.Hima Sekhar

You might also like