0
void addToEnd()
{
    newnode = (struct node*)malloc(sizeof(struct node));

    printf ("Enter the customer name :");
    scanf ("%s", newnode->cName);

    printf ("\nEnter customer number :");
    scanf ("%d", &newnode->cNumber);

    printf ("\nEnter transaction description :");
    scanf ("%s", newnode->tDescrip);

    newnode->next = NULL;
    if(list==NULL)
        list = newnode;
    else if (list != NULL && newnode < list)
    {
        newnode->next = list;
        list = newnode;
    }
    else
    {
        temp = list;
        while (temp != NULL)
        {
            if (newnode > temp)
            {
                prev = temp;
                temp = temp->next;
            }
        }
        newnode->next = prev->next;
        prev->next = newnode;
    }
}

i tried this code, but tis code is just add to start but not to end, how am i suppose to add the node to the end?

0

2 Answers 2

2

Let's see, whether the following isn't as easily understood.

Simply change the pointer:

struct node** tail = &list;
while (*tail != NULL) {
    tail = &((*tail)->next);
}
*tail = newnode;
Sign up to request clarification or add additional context in comments.

1 Comment

Why is this code found so seldom? It is one of the places where C is better, more expressive than java. No if, terse code.
0

Please study the following append function for adding a node in to Linked List

append( int num )
{
    struct node *temp,*r;
    /* CREATING A NODE AND ASSIGNING A VALUE TO IT */

    temp= (struct node *)malloc(sizeof(struct node));
    temp->data=num;
    r=(struct node *)p;

    if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */
    {
        p=temp;
        p->next =NULL;
    }
    else
    { /* GO TO LAST AND ADD*/

        while( r->next != NULL)
            r=r->next;
            r->next =temp;
            r=temp;
            r->next=NULL;
    }
}/* ADD A NEW NODE AT BEGINNING */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.