3

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.

3
  • 1
    You cannot just cast a parameter to pretend it was passed by reference. I refer to this line: struct Node **p2p2Node= (struct Node **)p2Node; If you want the pointer value to be modified by the function, then the parameter to the function must be a pointer-to-pointer. Commented Oct 21, 2020 at 0:48
  • @paddy Does that mean I should modify the pointer passed in, i.e. nList, to be a pointer of a pointer to a struct? I mean, a pointer to a pointer is still a pointer, it should be okay to be passed into insert as void *p2Node? Commented Oct 21, 2020 at 0:53
  • 3
    Based on the logic I see inside your function, the function's signature should be void insert(struct Node **p2p2Node, void *data) and when you call it for an empty list, nList should be NULL. Then you call insert(&nList, setC); You should also use calloc when allocating inside the insert function, OR explicitly set next to NULL. Currently, the value of the next-pointer on the first node inserted is undefined. Commented Oct 21, 2020 at 0:55

1 Answer 1

1

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");
    }
}
Sign up to request clarification or add additional context in comments.

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.