Linked list value pointed only changing inside function in C

Viewed 164

I am trying to implement a linked list in C:

struct Node{
    struct Node *next;
    void *data;
};

With an insert function:

void insert(void *p2Node, void *data)
{
    struct Node *newNode;
    struct Node **p2p2Node= (struct Node **)p2Node;
    
    if (newNode = malloc(sizeof(struct Node))) /* if successfully allocated */
    {
        newNode->data = data;
        
        if ((*p2p2Node) != NULL) /* if the list is not empty */
        {
            newNode->next =  (*p2p2Node)->next;
            (*p2p2Node)->next = newNode;
        }
        else
            (*p2p2Node) = newNode;

        p2Node = p2p2Node;
    }
    printf("Inside the insert: %s\n", (*p2p2Node)->data);
}

I called insert in main():

int main()
{
    char *setA = "liquid ";
    char *setB = " lgd";
    char *setC = "sample";  
    struct Node *nList = malloc(sizeof(struct Node));

    insert(nList, setC);
    printf("2Get %s\n", nList->data);
    
    return 0;
}

No error or warning was reported, but the value was only changed inside the insert. Back to main() the linked list is still empty.

I do not understand: nList in main() is a void pointer. Inside insert(), *p2Node is not altered, I used p2p2Node to change the value p2Node points to, why is it not working? Did I mismatch the pointers? Is there a way I can make it work without modifying the parameter of insert()?

Thank you.

1 Answers

Use this code to insert values to the linked list.

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

struct node *root = NULL;
int len;

int main()
{
    append();
    display();

    addatbegin();
    display();

    addatafter();
    display();
}

Add values to the end of the list.

void append(){
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
    printf("Enter the data: ");
    scanf("%d", &temp->data);
    temp->link = NULL;
    if(root == NULL) //list is empty
    {
        root=temp;
    }else
    {
        struct node* p;
        p=root;
        while(p->link != NULL)
        {
            p = p->link;
        }
        p->link = temp;
    }
}

Add values to the beginning of the list.

void addatbegin()
{
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
    printf("Enter the data : ");
    scanf("%d", &temp->data);
    temp->link = NULL;
    if(root == NULL)
    {
        temp = root;
    }
    else
    {
        temp->link = root;
        root = temp;
    }
}

Add value after a node

void addatafter()
{
    struct node* temp, *p;
    int loc, i=1;
    printf("Enter the location : ");
    scanf("%d", &loc);
    if(loc > len)
    {
        printf("Invalid input.");
    }
    else
    {
        p = root;
        while(i > loc)
        {
            p = p->link;
            i++;
        }
        temp = (struct node*)malloc(sizeof(struct node));
        printf("Enter the data : ");
        scanf("%d", &temp->data);
        temp->link = NULL;
        temp->link = p->link;
        p->link = temp;
    }   
}

To display the linked list

void display(){
    struct node* temp;
    temp = root;
    if(temp == NULL)
    {
        printf("List id empty.\n");
    }
    else
    {
        while (temp != NULL){
            printf("%d -> ", temp->data);
            temp = temp->link;
        }
        printf("\n\n");
    }
}
Related