//quick sorting of integers
#include
#include
void sort(int a[],int first,int last)
{
int temp,low,high,mid;
low=first;
high=last;
mid=a[(first+last)/2];
do
{
while(a[low] < mid)
low++;
while(a[high] > mid)
high--;
if(low <=high)
{
temp=a[low];
a[low]=a[high];
a[high]=temp;
low++;
high--;
}
}
while(low <= high);
if(first < high)
{
sort(a,first,high);
}
if(low < last)
{
sort( a,low,last);
}
}
void main()
{
int a[10],i,n;
printf("\n enter the size of the array -->");
scanf("%d",&n);
printf("\n enter array elements one by one \n");
for(i=0;i
6:55 AM |
Category:
DATA STRUCTURE
|
#include
#include
#include
#include
struct linked_list
{
int info;
struct linked_list *next;
};
typedef struct linked_list NODE;
NODE * create (NODE *);
void display(NODE *);
int count(NODE *);
NODE *insert(NODE *,int);
NODE *search(NODE *,int);
NODE start;
NODE *create(NODE * node)
{
char ans ='y';
start.next=NULL;
node =&start;
while(ans=='y')
{
node->next=(NODE*)malloc(sizeof(NODE));
node=node->next;
printf("\n enter a number to add->");
scanf("%d",&node->info);
node->next=NULL;
printf("\n do you want to ad more node(y/n)");
scanf(" %c",&ans);
}
return(node);
}
void display (NODE *node)
{
node= &start;
node=node->next;
printf("\n ROOT->");
while(node!=NULL)
{
printf("%d->",node->info);
node=node->next;
}
printf("NULL");
}
int count(NODE *node)
{
node=&start;
int c=0;
while(node->next!=NULL)
{
c++;
node=node->next;
}
return (c);
}
NODE *insert(NODE *node,int key)
{
node=&start;
NODE *newnode,*n1;
NODE *temp=node;
if(node->info==key)
{
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n enter a number to add->");
scanf("%d",&newnode->info);
newnode->next=node;
node=newnode;
return(node);
}
else
{
n1=search(node,key);
if(n1==NULL)
{
printf("\n searching node not found");
exit(0);
}
newnode=(NODE*)malloc(sizeof(NODE));
printf("\n enter a number to add->");
scanf("%d",&newnode->info);
newnode->next=n1->next;
n1->next=newnode;
return(temp);
}
}
NODE *search(NODE *node,int key)
{
node=&start;
while(node!=NULL)
{
if(node->info==key)
{
return(node);
}
node=node->next;
}
return(NULL);
}
void main()
{
NODE *head;
int key;
head=create(head);
printf("\n after creation linked list is--->\n");
display(head);
printf("\n total number of nodes=%d",count(head));
printf("\n enter a searching number where new node will be inserted-->");
scanf("%d",&key);
head=insert(head,key);
printf("\n after insertion list is=\n");
display(head);
getch();
}
8:20 AM |
Category: |