2. Singly Linked List
Data Next
5 500 10 2000 15
1000 500 2000
Memory Address
A singly linked list is a linked list in which each node contains only one link
field pointing to the next node in the list.
3. Singly Linked List - Structure Definition
struct node
{
int data;
struct node * next;
};
Next
Data Size of this Structure:
For Integer: 4 bytes
For Pointer : 4 Bytes
Totally : 8 bytes
4. Allocating memory for node
malloc(): returns a pointer to the starting address of the memory block.
Example:
newnode= (struct node*) malloc(sizeof(struct node));
5. Storing data in the data field
To access members of a structure through a pointer, use the (->) arrow operator.
Example:
newnode -> data = 10;
node data field of the node
7. Questions
struct node
{
char data;
struct node * next;
};
1. What function is used for allocating memory?
2. How many fields are there in singly linked list node?
3. How to store data in data field of a node?
What is the size of the
above structure ?
8. Steps for writing code
1. Define Structure of the node
2. Allocate memory
3. Get or assign the values to the data field
4. Make Connection between the nodes.