Switch to full style
For C/C++ coders discussions and solutions
Post a reply

list insertion sorting code in c++

Mon Dec 14, 2009 10:50 pm

insertion sort inside the list using C++

cpp code
void insertSortList(LIST *L, int x) 
{
NODE *nextptr,*previus,*newptr;
nextptr=L->head;
previus=nextptr;
if(nextptr==NULL)
{
newptr=(NODE*) malloc(sizeof(NODE));
newptr->data=x;
newptr->next=L->head;
L->head=newptr;
L->size++;
return;
}
else if(x<L->head->data)
{
newptr=(NODE*) malloc(sizeof(NODE));
newptr->data=x;
newptr->next=L->head;
L->head=newptr;
return;
}
else{
while ((nextptr != NULL )&& ((nextptr->data) < x ))
{
previus = nextptr;
nextptr = nextptr->next;
}
newptr=(NODE *)malloc(sizeof(NODE));
newptr->data=x;
newptr->next=nextptr;
previus->next=newptr;
L->size++;
return;
}
}
void deleteNode(LIST *L, int x)
{
NODE *nextptr, *previus;
nextptr = L->head;
if (nextptr->data == x)
{ //the head is treated separately
L->head = nextptr->next;
L->size--;
free(nextptr);
return;
}
else
{
while (nextptr != NULL)
{ //find x and delete it
if (nextptr->data == x)
{ //value found
Previus->next =nextptr->next;
free(nextptr);
L->size--;
return;
}
previus = nextptr; //use *previus as the previous position pointer
nextptr = nextptr->next;
}
}
}//delete




Post a reply
  Related Posts  to : list insertion sorting code in c++
 Java Insertion Sort Code     -  
 Quicksort implementation C++ Code-Integers-Sorting     -  
 Incomplete code for array sorting and merging     -  
 How to write a code for sorting array of 100 number in C++     -  
 balloon sort algorithm C++ implementation code-sorting array     -  
 quicksort algorithm implementation java code- array sorting     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 Linked List C++ Code Implementation     -  
 Read list of files in java I/O code     -  
 can u check tis code to perform union & intersection of list     -  

Topic Tags

C++ Sorting