Mon Dec 14, 2009 10:50 pm
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
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.