Cn anybody give me a program about adding nodes at the end of the list in C?

I just need a basis of a program that I will be creating. I also needed its algorithm. Thanks in advance.

Dani AI

Generated

A few quick, practical corrections and a safe example to replace the broken loop in ’s post. The original issues were: using ptr->next before it was initialized (infinite/undefined behavior), allocating memory only once instead of for each new element, and the print loop never advanced the pointer. correctly flagged the uninitialized next; was right to say allocate on every insertion.

Correct approach in words: for each value you want to append, allocate a fresh node, set its data and next = NULL, then either make it the head if the list is empty or link it after the current tail. When printing, walk the list with a pointer that moves forward (p = p->next) until NULL. Always check malloc's return and free the list when done. Avoid nonstandard clrscr()/getch().

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

void append(Node **head, int value) {
    Node *n = malloc(sizeof *n);
    if (!n) { perror("malloc"); exit(EXIT_FAILURE); }
    n->data = value;
    n->next = NULL;

    if (*head == NULL) {
        *head = n;
        return;
    }
    Node *tail = *head;
    while (tail->next) tail = tail->next;
    tail->next = n;
}

int main(void) {
    Node *head = NULL;
    append(&head, 10);
    append(&head, 20);
    append(&head, 30);

    for (Node *p = head; p; p = p->next)
        printf("%d\n", p->data);

    while (head) {
        Node *tmp = head;
        head = head->next;
        free(tmp);
    }
    return 0;
}

Troubleshooting checklist: set new->next = NULL before linking; advance the traversal pointer when printing; compile with -Wall -Wextra to catch mistakes; for many appends keep a tail pointer to make each append O(1). This addresses the specific bugs shown in the thread and gives a safe, minimal template to build on.

Recommended Answers

All 9 Replies

Your question is incomplete. What list? What type? There are many types of lists.

Taking the list to be a singly ended.
1. Locate End Pointer (endPtr)
2. Create a new node (newPtr)
3. Initialize new node
4. Assign the link part of endPtr the address of newPtr
5. Make newPtr as new endPtr

Your question is incomplete. What list? What type? There are many types of lists.

What I mean is about SINGLY LINKED LIST in C programming.

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int num;
    struct node *next;
}               ;
typedef struct node nd;


main()
{
    int i, x;
    nd *head=NULL, *ptr;
    clrscr();
    head=malloc(sizeof(nd));
    ptr=head;
    printf("Enter numbers: ");
    for(i=0; i<5; i++)
    {
        scanf("%d", &x);
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        ptr->num=x;
        ptr->next=NULL;
    }

    ptr=head;
    printf("\n\nValues: ");
        for(i=0; i<5; i++)
    {
        if(ptr!=NULL)
        {
            printf("\n%d", ptr->num);
        }
    }

    getch;
}

Theres a logical error here but i can't find what it is. The problem is the output goes wrong when I print it. Cn anybody debug this program for me? pls

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

ptr->next is uninitialized before this loop, so its never NULL, so it will run forever, since next contains garbage and will never be NULL.

I've modified that part and changed it to


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

but still it doesn't print the right output?

Because you are initializing memory only once.Initialize memory whenever you add a new element.So dont initialize for the head in the start.Initialize memory after the while loop and also you can initialize the value at the same time.

What I meant to say is that you don't have ptr->next=NULL before the while loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.