Friday, August 28, 2009

Heap Sort



#include<stdio.h>



#include<conio.h>



main()



{



char
ans;



do



{




int
l,i,r,n,x,a[20],s;



clrscr();



printf("\n\n\t\t\tHEAPSORT");



printf("\n\t\t\t********");



printf("\n\nENTER THE TOTAL NO.OF ELEMENT:->");



scanf("%d",&n);








printf("\n\nENTER THE ELEMENT:->");




for
(i=1;i<=n;i++)



scanf("\n%d",&a[i]);









l = n / 2 + 1;




r = n;




while
(l>1)



{



l--;



sift(a,l,r);



}




while
(r>1)



{




x = a[1];



a[1] =
a[r];



a[r] =
x;



r--;




sift(a,l,r);



}



printf("\n\nHEAP SORT ELEMENTS ARE :->");



printf("\n");




for
(s=1;s<=n;s++)



printf("\n\t\t%d",a[s]);



printf("\n\nDO U CONTINUE (Y/N) :->");



scanf("%s",&ans);



}



while(ans
== 'y' || ans == 'y');








}



sift(int a[20],int left,int right)



{



int
k,j,x;




k = left;




j = 2*k;




x = a[k];



while(j
<= right)



{




if
((j < right) && (a[j] < a[j+1]))




j++;




if
(x > a[j])




goto g;



a[k] =
a[j];




k = j;




j = 2 * k;



}



g:



a[k] =
x;



}





Quick Sort



#include<stdio.h>



#include<conio.h>



main()



{



char
ans;



do



{




int
i,n,m,a[10];



clrscr();



printf("\n\n\t\t\tQUICKSORT");



printf("\n\t\t\t*********");



printf("\n\nENTER THE TOTAL ELEMENT:->");



scanf("%d",&n);



printf("\n\nENTER THE ELEMENT:->");




for
(i=1;i<=n;i++)



scanf("%d",&a[i]);



qsort(a,1,n);



printf("\n\nSORTED ELEMENTS ARE :->");




for
(i=1;i<=n;i++)



printf("\t%d",a[i]);



printf("\n\nDO U CONTINUE (Y/N) :->");



scanf("%s",&ans);



}



while(ans=='y' || ans =='y');



getch();



}



qsort(int a[10],int l,int r)



{



int
i,j,m,t;



i=l;j=r;



m=a[(l+r)/2];



while(a[i]<m)



i=i+1;



while(a[j]>m)



j=j-1;



if(i<=j)



{



t=a[i];a[i]=a[j];a[j]=t;i=i+1;j=j-1;



}



if(l<j)



qsort(a,l,j);



if(i<r)



qsort(a,i,r);



}





Doubly Linked List



#include<stdio.h>



#include<conio.h>



typedef
struct dll



{



int
data;



struct
dll *prev,*next;



}lnode;



typedef
struct



{



int
count,currpos;



lnode *current;



}list;



list l;




void

main()



{




int

ch;




void

insert();




void

delete();




void

display();




void

setposition(int);



l.count=0;



l.currpos=0;



l.current=NULL;




do



{




clrscr();



printf("\nMAIN MENU");




printf("\n=========");




printf("\n\t 1.INSERT");




printf("\n\t 2.DELETE");




printf("\n\t 3.DISPLAY");




printf("\n\t 4.EXIT");




printf("\n\nENTER THE OPTION :->");




scanf("%d",&ch);




switch(ch)




{




case 1:




insert();




getch();




break;




case 2:




delete();




getch();




break;




case 3:




display();




getch();




break;




case 4:




exit(0);




getch();




break;




}



}while(ch!=4);



}




void

insert()



{



int
p,x;



lnode *newnode,*following;



newnode=(lnode*)malloc(sizeof(lnode));



printf("\nENTER
THE POSTION:"
);



scanf("%d",&p);



if(p<1||p>(l.count+1))



printf("\nINVALID POSITION");




else



{



printf("\n ENTER THE VALUE:");



scanf("%d",&x);



newnode->data=x;




if
(p==1)



{



newnode->prev=NULL;




if
(l.count==0)



newnode->next=NULL;



else



{



setposition(1);



newnode->next=l.current;



l.current->prev=newnode;



}



}




else



{



setposition(p-1);



following=l.current->next;



newnode->next=following;



newnode->prev=l.current;



l.current->next=newnode;



if(following!=NULL)




following->prev=newnode;




}




l.current=newnode;




l.currpos=p;




l.count++;




printf("%d IS INSERTED",x);




}




}
























void

delete()



{



int
p,x;



lnode *following;



if(l.count==0)



printf("\n\nLIST
IS EMPTY"
);



else



{



printf("\n
ENTER THE POSITION"
);



scanf("%d",&p);



if(p<1||p>l.count)



printf("\nINVALID POSITION");




else
if(p==1)



{



setposition(1);



x=l.current->data;



following=l.current->next;



if(following!=NULL)




following->prev=NULL;




l.currpos=1;




l.current=l.current->next;




}




else




{




setposition(p);




x=l.current->data;




l.current->prev->next=l.current->next;




if(l.current->next!=NULL)




{




l.current->next->prev=l.current->prev;




l.currpos=p;




l.current=l.current->next;




}




else




{




l.currpos=p-1;




l.current=l.current->prev;




}




}




l.count--;




printf("%d IS DELSETED",x);




}




}




void setposition(int
p)




{




int i;




lnode *current;




if(p<1||(p>l.count))




printf("\nCANNOT FIND");




else if(l.currpos<p)




{




while(l.currpos!=p)




{




l.current=l.current->next;




l.currpos=l.currpos+1;




}




}




else if(l.currpos>p)




{




while(l.currpos!=p)




{




l.currpos=l.currpos-1;




l.current=l.current->prev;




}




}




}




void display()




{




if(l.count==0)




{




printf("\nLIST IS EMPTY");




getch();




exit(0);




}




else




{




lnode *temp;




setposition(1);




temp=l.current;




while(temp!=NULL)




{




printf("\n\t%d \t%d \t%d \t%d",temp->prev,temp->data,temp,temp->next);




temp=temp->next;




}




}




}





Singly Linked List



#include<stdio.h>



#include<conio.h>



typedef
struct lst



{



int
data;



struct
lst *link;



}node;



typedef
struct



{



node *head;



int
count;



}list;




int

menu();




void

insert(int,int);




void

delete();




void

show();



node *setposition(int);



list mylist={NULL,0};




void

main()



{




int
choice,pos,data;




do



{




clrscr();




choice=menu();




switch(choice)




{




case 1:




printf("\n ENTER THE
DATA AND POSITION :->"
);




scanf("%d %d",&data,&pos);




insert(data,pos);




getch();




break;




case 2:




delete();




getch();




break;




case 3:




show();




getch();




break;




case 4:




printf("\n\nEND OF COMPUTATION");




getch();




}



}while(choice!=4);



}




int

menu()



{



int
choice;



do



{



printf("\n\n\t\t\tLINKED LIST OPERATION");



printf("\n\t\t\t*********************");



printf("\nMAIN MENU");



printf("\n=========");



printf("\n\t 1.INSERT");



printf("\n\t 2.DELETE");



printf("\n\t 3.SHOW");



printf("\n\t 4.EXIT");



printf("\n\nEnter your choice :->");



scanf("%d",&choice);



}while(choice<1||choice>4);



return
choice;



}




void

insert(int x,int
p)



{



node *newnode;



node *currnode;



if(p<1||p>mylist.count+1)



{



printf("\nINVALID LOCATION");




return
;



}



newnode=(node*)malloc(sizeof(node));



newnode->data=x;



if(p==1)



{



newnode->link=mylist.head;



mylist.head=newnode;



printf("\n\n %d is INSERTED IN LIST",x);



}




else



{



currnode=setposition(p-1);



newnode->link=currnode->link;



currnode->link=newnode;



printf("\n\n %d is INSERTED IN LIST",x);



}



mylist.count++;



}




void

delete()



{



node *prev,*temp;



int
val;



if(mylist.head!=NULL)



{



printf("\n\n ENTER THE DATA TO BE DELETED:");



scanf("%d",&val);




if
(mylist.head->data==val)



{




temp=mylist.head->link;




free(mylist.head);




mylist.head=temp;




mylist.count--;




printf("\n\n%d IS DELETED FROM THE LIST",val);



}



else



{




prev=mylist.head;




while(prev->link!=NULL &&
prev->link->data!=val)




prev=prev->link;




if(prev->link!=NULL)




{




temp=prev->link->link;




free(prev->link);




prev->link=temp;




mylist.count--;




}



else




printf("\n\n NO MATCH");



}



}



else




printf("\n\nTHE LIST IS EMPTY");



}



node *setposition(int p)



{



int
i;



node *t=mylist.head;



for(i=2;i<=p;i++)



t=t->link;



return
t;



}




void

show()



{



node *t;



if(mylist.head!=NULL)



{



t=mylist.head;




while
(t!=NULL)



{




printf("\n AT %p : %d // %p \n",t,t->data,t->link);




t=t->link;



}



}



else



{




printf("\nTHE LIST IS EMPTY \n");



}



}