An array is a commonly used data structure in most of the languages. Because it is simple, it needs O(1) time for accessing elements. It is also compact.
But an array has a serious drawback - it can not grow or shrink. You need to estimate the array size and define it during compile time.
This drawback is not present a linked list. A linked list is a data structure which can grow or shrink dynamically.
A linked list has nodes each of which contain contain data and a link to next node. These nodes are dynamically allocated structures. If you need more nodes, you just need to allocate memory for these and link these nodes to the existing list.
The nodes of a linked list have to be defined as self-referential structures in C. That is structures with data members and one member which is a pointer to the structure of same type. This pointer will work as a link to next node.
Of course we can work without creating the typedef. But typedef makes code more elegant. We will use this NODEPTR. Allocate memory for it, then populate it and finally link it.
A linked list must have a starting node called head node or start node which will be the pointer to first node of the list. Any operation to the list needs this head. This head has to maintained because if the program overwrites this pointer, the list will be lost. We should initialize head with NULL because initially the list is empty.
Now to add nodes to this list, let us write a function which takes head as a parameter and data for the new node as second parameter. The function will return the head pointer.
We find the last node of the linked list by looping till temp->next =NULL. Then we link this last node to new node.
The function is fine for all nodes except the first node. When the list is empty and you want to add a node, you should not use the for loop to find the last node. Instead, if head is NULL, you should assign new node to head and return it.
And now we have to test this function. Let us also write a function to traverse the list and print it. This function is quite simple
Let us use these functions and write the complete program
Compile and run this program . Here is how your output looks like.
If you are curious and want to know how these links are working and modify the printf in the print_list function.
Now with this change, your output may look like
So our first program on linked list is ready. In the next post we will see how to delete a node from linked list and how to search for a value in the list.
But an array has a serious drawback - it can not grow or shrink. You need to estimate the array size and define it during compile time.
This drawback is not present a linked list. A linked list is a data structure which can grow or shrink dynamically.
A linked list has nodes each of which contain contain data and a link to next node. These nodes are dynamically allocated structures. If you need more nodes, you just need to allocate memory for these and link these nodes to the existing list.
The nodes of a linked list have to be defined as self-referential structures in C. That is structures with data members and one member which is a pointer to the structure of same type. This pointer will work as a link to next node.
struct node { int data; struct node *next;//pointer to another node }; typedef struct node * NODEPTR;
Of course we can work without creating the typedef. But typedef makes code more elegant. We will use this NODEPTR. Allocate memory for it, then populate it and finally link it.
A linked list must have a starting node called head node or start node which will be the pointer to first node of the list. Any operation to the list needs this head. This head has to maintained because if the program overwrites this pointer, the list will be lost. We should initialize head with NULL because initially the list is empty.
NODEPTR head = NULL;
First let us write a function to create a new node, assign values to the data of this node and return pointer to this node.NODEPTR create_node(int value) { NODEPTR temp = (NODEPTR) malloc(sizeof(struct node)); temp->next = NULL; temp->n = value; return temp; }
Now to add nodes to this list, let us write a function which takes head as a parameter and data for the new node as second parameter. The function will return the head pointer.
NODEPTR append_node(NODEPTR head,int val) { NODEPTR newnode ; newnode = create_node(val);
/*find the last node in the linked list */ for(temp = head; temp->next!=NULL;temp = temp->next) ;/*do not forget this semicolon*/ /*link the new node to temp - the last node*/ temp->next = newnode; return head; }
We find the last node of the linked list by looping till temp->next =NULL. Then we link this last node to new node.
The function is fine for all nodes except the first node. When the list is empty and you want to add a node, you should not use the for loop to find the last node. Instead, if head is NULL, you should assign new node to head and return it.
NODEPTR append_node(NODEPTR head,int val) { NODEPTR newnode ; newnode = create_node(val);
/* if the list is empty*/
if (head==NULL)
head = newnode;
else{ for(temp = head; temp->next!=NULL;temp = temp->next) ; temp->next = newnode;
}
return head;
}
And now we have to test this function. Let us also write a function to traverse the list and print it. This function is quite simple
void print_list(NODEPTR head) { NODEPTR temp = head; while(temp!=NULL) { printf("%d---->",temp->data); temp = temp ->next; } }
Let us use these functions and write the complete program
#include<stdio.h> struct node { int data; struct node *next; }; typedef struct node * NODEPTR;
NODEPTR create_node(int value) { NODEPTR temp = (NODEPTR) malloc(sizeof(struct node)); temp->next = NULL; temp->n = value; return temp; }
NODEPTR append_node(NODEPTR head,int val) { NODEPTR newnode,temp ; newnode = create_node(val); /*if the list is empty , assign newnode to head*/ if(head ==NULL) { head = newnode; return head; } /*find the last node in the linked list */ for(temp = head; temp->next!=NULL;temp = temp->next) ;/*do not forget this semicolon*/ /*link the new node to temp - the last node*/ temp->next = newnode; return head; } void print_list(NODEPTR head) { NODEPTR temp = head; while(temp!=NULL) { printf("%d---->",temp->data); temp = temp ->next; } } int main() { NODEPTR head = NULL; int i; for(i = 0;i<10;i++) { int n; printf("Enter a number :"); scanf("%d",&n); head = append_node(head,n); } printf("The list is :"); print_list(head); }
Compile and run this program . Here is how your output looks like.
If you are curious and want to know how these links are working and modify the printf in the print_list function.
printf("temp=%p temp->data=%d temp->next=%p\n;",temp,temp->data,temp->next);
Now with this change, your output may look like
So our first program on linked list is ready. In the next post we will see how to delete a node from linked list and how to search for a value in the list.
Hey there! Do you use Twitter? I'd like to follow you if that would be okay.
ReplyDeleteI'm absolutely enjoying your blog and look forward to new updates.
my site ... homepage ()