in DS
887 views
0 votes
0 votes

How to Visualize this code ?



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

struct node{
        int data;
        struct node *next;
    };
    
    void addFirst(struct node **head,int val){
        
        struct node *newNode =malloc(sizeof(struct node));
        newNode->data=val;
        newNode->next=*head;
        *head=newNode;
        
    }
    void printList(struct node *head){
        
        struct node *temp=head;
        
        while(temp!=NULL){
            printf("%d ",temp->data);
            temp=temp->next;
        }
        printf("NULL");
    }
    
int main(){
    struct node *head;
    
    addFirst(&head,10);
    addFirst(&head,20);
    addFirst(&head,30);
    addFirst(&head,40);
    addFirst(&head,50);
    
    printList(head);
    
}
in DS
by
887 views

1 comment

i think there is one fault in program inside main().

  struct node *head;   will create a pointer to a struct node type but inside it garbage will be there. so in first node the next field will point a garbage value which may give segmentation error while running printList(head); because there is no null in any  node so program may not terminate also.

so i think after   struct node *head;  head should be initialized to null first. so that when we call addFirst();  the last node has null in it.

 

0
0

1 Answer

1 vote
1 vote
The given code implements a linked list data structure using C programming language. A linked list is a data structure in which each element, called a node, contains a value and a pointer to the next node in the sequence.

Next, there is a structure definition called `node`. This structure represents a single node in the linked list. It has two components: an integer data field to hold the value of the node, and a pointer to the next node in the list.

Moving on, there is a function named `addFirst`. This function takes a double pointer to the head of the linked list (`struct node **head`) and an integer value (`val`) as parameters. Its purpose is to add a new node with the given value at the beginning of the list. Inside the function, a new node is dynamically allocated using `malloc`. The value is assigned to the data field of the new node, and its `next` pointer is set to point to the current head node. Finally, the head pointer is updated to point to the new node, effectively making it the new head of the list.

Another function called `printList` is defined to print the elements of the linked list. It takes a pointer to the head of the list (`struct node *head`) as a parameter. Inside the function, a temporary pointer (`temp`) is initialized with the head pointer. The function then traverses the list by iterating through the nodes one by one using the `temp` pointer. It prints the value of each node and moves to the next node until it reaches the end of the list (where `temp` becomes `NULL`). Finally, it prints "NULL" to indicate the end of the list.

In the `main` function, a pointer to the head of the linked list (`struct node *head`) is declared.

The `addFirst` function is called multiple times in the `main` function to add elements to the linked list. Each call adds a new node at the beginning of the list, with values 10, 20, 30, 40, and 50 respectively. The `addFirst` function handles updating the head pointer to reflect the new node as the head.

Finally, the `printList` function is called with the head pointer to print the elements of the linked list. The values are printed in reverse order since each new node is added at the beginning of the list. The output will be "50 40 30 20 10 NULL", representing the linked list elements in reverse order.

I hope this explanation helps you understand the code better!

2 Comments

That's a beautiful explanation.

Hope the questions asked is clear enough and not too long enough in the future.

I also suggest that questions should be categorised with options or no options. And the answer code presented in the exam should also be shared.

This is kind of fooling people.
0
0
thanks !
0
0

Related questions