struct Node
struct Node
Explanation:
1. struct Node*:
o This part of the code is declaring a pointer to a structure of type Node.
o struct Node* indicates that node2 is a pointer to a memory location that is meant to store a
Node structure.
2. node2:
o node2 is the variable name for the pointer that will hold the address of the memory allocated
for the Node structure.
3. (struct Node*):
o This is a type cast. It converts the pointer returned by malloc to a pointer of type struct
Node*.
o malloc returns a void*, which is a generic pointer type that can point to any data type.
However, in C, it's common practice to cast this pointer to the appropriate type, in this case,
struct Node*.
4. malloc(sizeof(struct Node)):
o malloc is a function that allocates a specified amount of memory on the heap and returns a
pointer to the beginning of the block.
o sizeof(struct Node) calculates the size (in bytes) of the Node structure.
o So, malloc(sizeof(struct Node)) requests the allocation of enough memory to store one
Node structure.
5. What the entire line does:
o The line of code requests memory on the heap that's large enough to store one Node structure.
o After allocating this memory, it stores the starting address of the allocated memory in the
pointer node2.
o Now, node2 can be used to access the newly allocated Node structure.
C program for creating linked list and insert data at the beginning of the list
#include <stdio.h> }
#include<stdlib.h>
temp = newnode;
typedef struct node{ }
int data; printf("Linked list: ");
struct node *link; temp = head;
} node; while (temp)
{
node *createnode(int data) printf("%d ", temp->data);
{ temp= temp->link;
node *newnode = (node }
*)malloc(sizeof(node)); printf("\n");
newnode -> data = data;
newnode -> link = NULL; printf("Enter the data to be
insertetd: ");
return newnode; scanf("%d", &insert);
}
node *add_begin(node *head, int data); head = add_begin(head, insert);
temp = head;
int main() printf("Updated Linked list: ");
{ while (temp)
int i, data, n, insert; {
node *head = NULL, *temp = NULL; printf("%d ", temp->data);
temp= temp->link;
printf("Enter number of nodes: "); }
scanf("%d", &n); printf("\n");