Linked List s a data structure consisting of nodes linked to each other by pointers pointing to the address of next node in the list in a most simple case. The last node will point to a NULL pointer.
The concept is very simple but when it come to programming Linked lists, it always become a hard stone to break for me possibly due not not so much familiarity with the pointers. So here i am going to explain how to create programs on linked lists in a very convenient manner statring from the basics.
The concept is very simple but when it come to programming Linked lists, it always become a hard stone to break for me possibly due not not so much familiarity with the pointers. So here i am going to explain how to create programs on linked lists in a very convenient manner statring from the basics.
Defining Data type
Before proceeding, we must define a what data type should be convenient for defining a node in a linked list. A node contains a value and a memory address pointing to next node. So now its evident that we must define a data type rather than using existing ones to accommodate two different parts of node. The value part can be Integer or floating point number or even a character or string which can be defined by their respective traditional data types while the memory address can be defined by a pointer. These two can be grouped together in a structure which will define a user-defined data type for a single node.
struct linkedlist
{
int value;
struct linkedlist *next;
};
The above structure named linked lists contains two declarations.
1. First one illustrates the the value a node will hold. It has been declared as int. which constraints that linked list will hold only integer values. But you can make your linkedlist structure to hold values of different data types. This i will tell you in the later part of the series- Understanding Linked List programming. For now i want to make it as simple as possible.
2. The next one is a pointer to the next node which will hold the address of next node. Since the next node will have a similar data type, the pointer is of type linkedlist structure. Remember pointers do have types like int* ptr, void *ptr etc.
Creating first node of Linked list
To start creating a linked list, we must make sure that the list is empty. For this we must initialize the head node and current node as NULL.
struct linkedlist *head=NULL;
struct linkedlist *current=NULL;
The above two lines of code initializes head node as well as current node to NULL.
Following lines of code shows how to create a root node.
struct linkedlist *ptr=(struct linkedlist*)malloc(sizeof(struct linkedlist));
ptr->value=10;
ptr->next=NULL;
head=current=ptr;
Explanation:
- The first statement struct linkedlist *ptr=(struct linkedlist*)malloc(sizeof(struct linkedlist)); creates a pointer of type linkedlist and allocates a size equal to the size of structure linked list so that it can accommodate both the value and memory address. (struct linkedlist*) is used for casting.
- The second statement ptr->value=10; assigns a value of 10 to value field of root node.
- The next statement ptr->next=NULL; assigns address of next node as NULL as currently only root node exists which points to no other node.
- The last statement assigns the memory address of head as well as current node as ptr. So that they can be accessed using memory address ptr.
Adding nodes to the root node and deleting nodes from the linked list will be covered in the next post.
No comments:
Post a Comment