-1

I wrote a program to create and print a single linked list. I have used structure pointer to structure pointer for modifying the list. When I print the list it only prints last two nodes added.

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

struct node{
        int data;
        struct node *next;
};

struct node * createnode(int num){
        struct node * n;
        n=(struct node*)malloc(sizeof(struct node));
        n->data=num;
        n->next=NULL;
        return n;
}
                                                  
void createlist(struct node **head,int num){
        struct node *temp=createnode(num);
        *head=temp;
        return;
}                                                 
void options(){
        printf("Enter your choice\n");
        printf("1.Add at end\n");
        printf("2.Add at beginning\n");
        printf("3.Add at location\n");
        printf("4.Print list\n");                 
}

void add_end(struct node **head){
        int info;
        printf("Value: ");
        scanf("%d",&info);
        if(*head==NULL){
                createlist(head,info);
                return;
        }
        struct node *temp=createnode(info);
        struct node **end=head;
        while((*end)->next!=NULL){
                (*end)=(*end)->next;
        }
        (*end)->next=temp;                        
}                 

void print2(struct node *head){
        struct node * temp=head;
        printf("\nList :");
        while(temp!=NULL){
                printf("%d ",temp->data);
                temp=temp->next;
        }
        printf("\n");
}

int main(){
        struct node *head=NULL;
        createlist(&head,5);

        int choice=0;
        options();
        scanf("%d",&choice);
        while(1){
                switch(choice){
                case 1:add_end(&head);
                       break;
                case 4:print2(head);
                       break;
                default:exit(0);
                }
                options();
                scanf("%d",&choice);
        }

        return 0;
}

Each time, I append 5 to 6 nodes at the end of list and when I print the list, it only prints last to nodes added. I don't know it is wrong with add_end function or print function.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • If you only have a single `int` as the list member, you can dispense with a `create_node()` function and simply do it all in a `add()` node function. If you use a `tail` pointer as well you get O(1) insertion at the end of the list. Two examples without `tail` pointer [Singly Linked List (node only, no wrapper)](https://pastebin.com/5MPLU4wB), with `tail` pointer [Singly Linked List of Integers (example)](https://pastebin.com/R2AewR3A) – David C. Rankin Sep 30 '20 at 04:19
  • Also, in C, there is no need to cast the return of `malloc`, it is unnecessary. See: [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) – David C. Rankin Sep 30 '20 at 04:19

1 Answers1

3

Your add_line routine incorrectly searches for the last node. The end variable is a pointer to pointer, so it is in a sense equivalent to the head parameter and it is not a temporary value, as it should be. Change the last lines to something like this:

void add_end(struct node **head){
    ...
    struct node *end = *head;
    while (end->next) {
           end = end->next;
           // Original line overwrites the 'head':  (*end)=(*end)->next;
    }
    end->next=temp;                        
}  
Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55