#Linked List

26 messages · Page 1 of 1 (latest)

limber wigeon
#
#include<stdlib.h>
struct node{
    int data;
    struct node *next,*prev;
};
//struct node *head,*tail;
struct node *head1,*tail1;
//struct node *head2,*tail2;
void push(struct node *head,int data){
    struct node *L=(struct node*)malloc(sizeof(struct node));
    if(head==NULL){
        head=L;
        L->data=data;
        L->next=NULL;
        L->prev=NULL;
       
        tail1=L;
    }

    else{
        tail1->next=L;
        L->prev=tail1;
        L->next=NULL;
        L->data=data;
        printf("%d",L->data);
        tail1=L;
    }
}
void display(struct node *p){
    do{
        printf("%d",p->data);
        p=p->next;

    }
    while(p->next!=NULL);
        
    
}

int main(){
    struct node *head1=NULL;
    push(head1,3);
    push(head1,4);
    push(head1,8);
    push(head1,10);
    display(head1);
}```
Im getting no output for this. Please help
ancient prawnBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

timid cloud
#

This code is by many means invalid
First thing is that you don't change the head1 pointer so it stays null and always met the if condition
So your program keeps allocating memory, but don't link your nodes together
If you want to change the head1 pointer inside the function, you need to have pointer to the head1 variable
Also, I will pass tail as function argument so we can use our function not only on tail1
Same as with head, we pass pointer to tail* var, so we can change it

So wechange the push function to

void push(struct node** head, struct node** tail, int data)

Now, that you have access to your head and tail, we can check if they actually points to and existing list and if not, create one

struct node* L = (struct node*)malloc(sizeof(struct node));
if (*head == NULL) {
    *head = L;
    *tail = L;
    L->prev = NULL;
    L->next = NULL;
    L->data = data;
}

The line *head = L; changes the head and in variable so it now points to an node.
Now, that we have this done, we can get to the else part of the function
Here we want to link a new node to the last one

else 
{
    struct node* previous_tail = *tail;
    (*tail) = L;
    previous_tail->next = L;
    (*tail)->prev = previous_tail;
    (*tail)->next = NULL;
    L->data = data;
}

Now our push functions is ready and we can try to run it
In order to do so we have to change the main function, as the args taken by function changed

int main() {
    struct node* head1 = NULL;
    struct node* tail1 = NULL;
    push(&head1, &tail1, 3);
    push(&head1, &tail1, 4);
    push(&head1, &tail1, 8);
    push(&head1, &tail1, 10);
    //display(head1);
}

Now that we have our linked list, lets print it
Your display function was almost good but there was one little problem: if you use

p->next != null

as your loop condition, the function won't print the last element

#
void display(struct node* head) {
    do {
        printf("%d", head->data);
        printf("%s", ", ");
        head = head->next;

    } while (head != NULL);
}
#

i have also added comma + space between each value

#

and now it should work

limber wigeon
#

but why is head1 not changing in the whole program

#

??

#

and tail too

#

??

timid cloud
#

When you pass head1 to the function as an argument, computer makes a copy of it (head var)

#

So the line head = L

#

Changed the copy of head1 (the head variable) and not the head1 itself

#

In your core tail1 was changed, because the line 'tail1 = L' writes to the variable tail1 and not some local copy passed as argument

#

Btw you should create a function that will free the memory you have taken with malloc

limber wigeon
limber wigeon
#

Please clarify

timid cloud
#

Well, you don't actually change the value of head1
Lets track value of pointers
First you create head1 and set it to null

main func
head1 = NULL

Then you call push function with head1 passed as "head" argument

main func       push func
head1 = NULL    head = NULL (copy of head1)

When you pass a value to the function, it is copied to the local argument variable. So despite head1 == head, head1 isn't the same variable as head, they are separate
Then your function does its logic and at the end we have

main func        push func
head1 = NULL     head = L

Because head is separate variable, the head = L line does not affect the head1 variable
So when our function ends we have

main func
head1 = NULL
  • leaked node, lost somewhere in the memory
#

This is the reason why your code does not work

#

Beacause head1 variable stays NULL

#

for comparison lets do the same with my code
in the beginning we have

main func
head1 = NULL
tail1 = NULL

head1 is just like in your code, I have also moved tail from global to main function scope, because it is nice to have both of them in main func
then we call the modified push func

void push(struct node** head, struct node** tail, int data)
...
push(&head1, &tail1, 3);

as you can see, now the head variable isn't of type struct node* but struct node** - so it is now pointer to pointer to node
and also as you can see, we are no longer passing a address assigned to head1 push(head1 ... but address of head1 itself push(&head1..
Now, the memory looks like

main func       push func
head1 = NULL <---  head (points to head1)
tail1 = NULL <---  tail (points to tail1)
#

Now that we have direct access to the head1 and tail1 we can change them, and we do

main func           push func
head1 = L <---  head (points to head1)
tail1 = L <---  tail (points to tail1)
#

and after the function end we have

#
main func
head1 = L
tail1 = L
#

now, when we pass head1 in the next push call it isn't NULL, so the function can actually start linking nodes together