Home > Enterprise >  Is there a way in C to create new nodes in a linked list without memory allocation?
Is there a way in C to create new nodes in a linked list without memory allocation?

Time:01-07

I just learned the concept of a linked list in C, and tried implementing it. What I did was create a pointer head, and a pointer itr. To create a new node I would initialize a node normally (without using pointers), and then attach a pointer to it.

struct node temp;    //A single node contains a value 'num' and a pointer to the next node.
temp.num=x;
temp.next=NULL;

if(head==NULL){
    head=&temp;
}
else{
    itr=head;
    while(itr->next!=NULL){
        itr=itr->next;
    }
    itr->next=&temp;
}

This method is not working, and based on my limited knowledge of pointers in C, I cannot figure out why. I know that the right way to do it is by using malloc to create new nodes, but I need to know why this method does not work.

Full Program:

#include <stdio.h>

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

int main(){
    struct node *head=NULL;
    struct node *itr;
    struct node temp;

    int choice;
    printf("1. Enter new Node, 2. Traverse all nodes, 3. Exit.");

    int x;
    while(1){
        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        if(choice==1){
            printf("Enter value: ");
            scanf("%d", &x);

            
            temp.num=x;
            temp.next=NULL;

            if(head==NULL){
                head=&temp;
            }
            else{
                itr=head;
                while(itr->next!=NULL){
                    itr=itr->next;
                }
                itr->next=&temp;
            }
        }

        else if(choice==2){
            if(head==NULL){
                printf("Empty List");
                continue;
            }
            printf("The values are: ");
            itr=head;
            printf("%d ", itr->num);
            while(itr->next!=NULL){
                itr=itr->next;
                printf("%d ", itr->num);
            }
        }

        else{
            break;
        }
    }
}

CodePudding user response:

A linked list generally consists of several nodes, in which each node points to the next node in the list. The last node in the linked list should point to NULL to mark the end of the list.

However, you have only allocated memory for a single node. Therefore, you will only be able to store a single node in the linked list.

The code for adding the first node to the linked list is correct. However, the code for adding a second node cannot work, because you have not allocated any memory for the second node.

What your code for adding the second node is actually doing is overwriting the first node, and making this first node point to itself. That is why you are getting an infinite loop, if you attempt to print the linked list afterwards.

Therefore, I suggest that you instead use malloc for allocating memory for the nodes. That way, you won't be limited to a single node.

CodePudding user response:

You must show your whole code. I guess , in your case, if you define a function like this, the temp elem is funciton local variable, you need use malloc to make it accessable out of the function.

int addNode(struct node *head, inx x) {
struct node temp;    //A single node contains a value 'num' and a pointer to the next node.
temp.num=x;
temp.next=NULL;

if(head==NULL){
    head=&temp;
}
else{
    itr=head;
    while(itr->next!=NULL){
        itr=itr->next;
    }
    itr->next=&temp;
}
}
  •  Tags:  
  • Related