0% found this document useful (0 votes)
3 views

struct Node

The document explains the process of creating a linked list in C, focusing on the allocation of memory for a new node using malloc and the structure of a node. It details the components of the code, including the declaration of a pointer to a Node structure, memory allocation, and the creation of nodes. Additionally, it provides a sample C program that demonstrates how to insert data at the beginning of the linked list.

Uploaded by

iammahmudul246
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

struct Node

The document explains the process of creating a linked list in C, focusing on the allocation of memory for a new node using malloc and the structure of a node. It details the components of the code, including the declaration of a pointer to a Node structure, memory allocation, and the creation of nodes. Additionally, it provides a sample C program that demonstrates how to insert data at the beginning of the linked list.

Uploaded by

iammahmudul246
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

struct Node* node2 = (struct Node*)malloc(sizeof(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");

for (i = 0; i < n; i++)


{ return 0;
printf("Enter data for node %d: ", }
i+1);
scanf("%d", &data); node *add_begin(node *head, int data)
{
node *newnode = createnode(data); node *ptr = (node
*)malloc(sizeof(node));
if (head == NULL) ptr -> data = data;
{ ptr -> link = head;
head = newnode;
} head = ptr;
else
{ return head;
temp -> link = newnode; }

You might also like