Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Programs Record

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 54

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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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;}

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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)

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { ptr>right=(node*)malloc(sizeof(node)); buildtree(ptr->right); } else ptr->right=NULL; } }

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;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: while(ptr! { if(ptrptr=ptr->right; if(ptr->data>item) ptr=ptr->left; =NULL) >data<item)

if(ptr->data==item) printf("%d has found \n",item); else printf("%d not found\n",item); } } 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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } void deleteBST(int { 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) { par=ptr; item)

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

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 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) { node *l; int 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);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: deleteBST(item); inorder(root); getch(); return 0; }

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() {

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: int n,i,a[50]; printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the elements into the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); Bubblesort(a,n); getch(); return 0; }

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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() {

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 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; }

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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; } }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Write a C program to sort the Quick sort technique.

elements in the array using

#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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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))

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { if(a[h]<=a[i]) { 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]; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } else { p->next=(node*)malloc(sizeof(node)); (p->next)->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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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);

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: }

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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 {

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 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) {

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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: Write a C program to 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; } } void display() { implement Stack using

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: 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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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()

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { int item,ch,n,del; printf("Enter the scanf("%d",&n); 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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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)

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: return -1; else { node *temp; temp=front; front=front->next; del=temp->data; 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)

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: printf("queue is empty");

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

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

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: } } } int getnode() { int i; for(i=0;i<10;i++) if(data[i]==0) return i; return -1; } void display()

{ 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;

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 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) { 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

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { 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]; } } }

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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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");

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 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; 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; } }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: if(ch==2) { printf("Enter a 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) { node *i; i=head; while(i!=NULL) { 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)

key value to insert

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: { 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; } return NULL; } void display() { 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();

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: case 4: case5: exit(0); } } getch(); return 0; } break; deletell(); break;

Output:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: Write a C program to using heaps. implement priority queues

#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; while(p>0&&tree[p]<tree[i]) { temp=tree[i];

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

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]; tree[i]=tree[lc]; tree[lc]=temp; i=lc; } else { if(y>x&&tree[i]<y) {

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

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; }

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

You might also like