Advertisement

SORTED LINKED LIST!

Started by August 21, 2000 12:49 PM
29 comments, last by Virus 24 years, 4 months ago
I''ve been trying to write a function that takes a sorted linked list of intergers and a new interger as parameters,inserts the new integer into its correct position in the list, and returns the resulting list. I have this so far....

node* AppendNode(node** headRef, int num) 
{
    node* current = *headRef;
    node* prev_ptr = *headRef;
    node* newNode;


    newNode = (node*)malloc(sizeof(node));
    newNode->value = num;
    newNode->next = NULL;

	
	
    // special case for length 0
    if (current == NULL) 
	{
    *headRef = newNode;

	}
	else
	{
		while ( current->next != NULL )
		{
			current = current->next;
		}

		current->next = newNode;
	}



	return newNode;
}
 
the real trouble is with the list passed via a pointer to the head pointer. could you tell me how to format this fucntion, so that it''ll insert each new value in the right place in the list. note: right now it only inserts the new node at the end of the list. ||||-- Our creation is the transformation of one. --|||
||||-- Our creation is the transformation of one. --|||
the structure is something like this

    typedef struct Node{    int value;    struct Node* next;}node;    
Advertisement
I posted the anonymous........
||||-- Our creation is the transformation of one. --|||
        typedef struct Node{	int value;	struct Node* next;}node;node* AppendNode(node** headRef, int num) {	node* current = *headRef;	node* prev_ptr = *headRef;    node* newNode;    newNode = (node*)malloc(sizeof(node));    newNode->value = num;    newNode->next = NULL;			// special case for length 0    if (current == NULL) 	{    *headRef = newNode;	}	else	{		while ( current->next != NULL )		{			current = current->next;		}		current->next = newNode;	}	return newNode;}void main (void){	node* head = NULL;	int number;	char ch;while ( ch != 'n' ){	cout<<"\nEnter number ";	cin>>number;	AppendNode(&head, number );	cout<<"\nAnother? ";	cin>>ch;}	while ( head != NULL )	{		printf ( "%d\n", head->value );		head = head->next;	}	}        



Just in case you wanted more info!


Edited by - Virus on August 21, 2000 2:06:39 PM
||||-- Our creation is the transformation of one. --|||
    problem = 1;while(homework != done){  post(problem);  while(!answered)  {    wait(5);    repost();  }  problem++;}    
COME ON!

||||-- Our creation is the transformation of one. --|||
||||-- Our creation is the transformation of one. --|||
Advertisement
A HINT AT LEAST! plz
||||-- Our creation is the transformation of one. --|||
wow, I''ve never seen so many posts just asking one question. So, you want to have your linked-list code order the integers? Obviously right now all it does is insert the integer to the end of the list, because of your

while ( current->next != NULL )
{
current = current->next;
}
current->next = newNode;

code after the check for a NULL head node. All you have to do now is replace that while() parameter with a

while (newNode->num > current->num && current->next != NULL)

and the code should run fine. (and be sure to return headRef, not return newNode). Also, since you''re returning a node, maybe you should utilize that in your main, to test its functionality... either that or just not return anything (would make your code a tiny bit faster if it had a void return type, assuming you don''t need it to return anything)

Happy coding!




"Man is the only animal that laughs and weeps; for he is the only animal that is struck with the difference between what things are and what they ought to be."
        --William Hazlitt
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
oops... looked at my code and forgot to notice something... change your while() to

while (current->next != NULL && newNode->num > current->num)

order counts! this way it will skip the num comparison if it comes to the end of the list


"Man is the only animal that laughs and weeps; for he is the only animal that is struck with the difference between what things are and what they ought to be."
        --William Hazlitt
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
I did exactly what you told me to do... and it didn't work..

there must be something else.

the function must take a sorted linked list of intergers and a new interger as parameters,insert the new integer into its correct position in the list, and return the resulting list

            node* AppendNode(node** headRef, int num) {	node* current = *headRef;	node* prev_ptr = *headRef;    node* newNode;    newNode = (node*)malloc(sizeof(node));    newNode->value = num;    newNode->next = NULL;			// special case for length 0    if (current == NULL) 	{    *headRef = newNode;	}	else	{while (current->next != NULL && newNode->value > current->value)		{			current = current->next;		}		current->next = newNode;	}	return *headRef;}            


Edited by - Virus on August 21, 2000 2:35:42 PM

Edited by - Virus on August 21, 2000 2:36:20 PM
||||-- Our creation is the transformation of one. --|||

This topic is closed to new replies.

Advertisement