Implementation of linked list

How to traverse a linked list

Displaying the contents of a linked list is very simple. We keep moving the temp node to the next one and display its contents.
When temp is NULL, we know that we have reached the end of linked list so we get out of the while loop.
  1. struct node *temp = head;
  2. printf("\n\nList elements are - \n");
  3. while(temp != NULL)
  4. {
  5. printf("%d --->",temp->data);
  6. temp = temp->next;
  7. }
The output of this program will be:
List elements are - 
1 --->2 --->3 --->

How to add elements to linked list

You can add elements to either beginning, middle or end of linked list.

Add to beginning

  • Allocate memory for new node
  • Store data
  • Change next of new node to point to head
  • Change head to point to recently created node
  1. struct node *newNode;
  2. newNode = malloc(sizeof(struct node));
  3. newNode->data = 4;
  4. newNode->next = head;
  5. head = newNode;

Add to end

  • Allocate memory for new node
  • Store data
  • Traverse to last node
  • Change next of last node to recently created node
  1. struct node *newNode;
  2. newNode = malloc(sizeof(struct node));
  3. newNode->data = 4;
  4. newNode->next = NULL;
  5. struct node *temp = head;
  6. while(temp->next != NULL){
  7. temp = temp->next;
  8. }
  9. temp->next = newNode;

Add to middle

  • Allocate memory and store data for new node
  • Traverse to node just before the required position of new node
  • Change next pointers to include new node in between
  1. struct node *newNode;
  2. newNode = malloc(sizeof(struct node));
  3. newNode->data = 4;
  4. struct node *temp = head;
  5. for(int i=2; i < position; i++) {
  6. if(temp->next != NULL) {
  7. temp = temp->next;
  8. }
  9. }
  10. newNode->next = temp->next;
  11. temp->next = newNode;

How to delete from a linked list

You can delete either from beginning, end or from a particular position.

Delete from beginning

  • Point head to the second node
  1. head = head->next;

Delete from end

  • Traverse to second last element
  • Change its next pointer to null
  1. struct node* temp = head;
  2. while(temp->next->next!=NULL){
  3. temp = temp->next;
  4. }
  5. temp->next = NULL;

Delete from middle

  • Traverse to element before the element to be deleted
  • Change next pointers to exclude the node from the chain
  1. for(int i=2; i< position; i++) {
  2. if(temp->next!=NULL) {
  3. temp = temp->next;
  4. }
  5. }
  6. temp->next = temp->next->next;

Complete program for linked list operations

Here is the complete program for all the linked list operations we learnt till now. Lots of edge cases have been left out to make the program short.
We suggest you to just have a look at the program and try to implement it yourself.
Also, notice how we pass address of head as struct node **headRef in the functions insertAtFront and deleteFromFront. These two functions change the position of head pointer so we need to pass the address of head pointer and change its value within the function.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. void display(struct node* head)
  9. {
  10. struct node *temp = head;
  11. printf("\n\nList elements are - \n");
  12. while(temp != NULL)
  13. {
  14. printf("%d --->",temp->data);
  15. temp = temp->next;
  16. }
  17. }
  18. void insertAtMiddle(struct node *head, int position, int value) {
  19. struct node *temp = head;
  20. struct node *newNode;
  21. newNode = malloc(sizeof(struct node));
  22. newNode->data = value;
  23. int i;
  24. for(i=2; inext != NULL) {
  25. temp = temp->next;
  26. }
  27. }
  28. newNode->next = temp->next;
  29. temp->next = newNode;
  30. }
  31. void insertAtFront(struct node** headRef, int value) {
  32. struct node* head = *headRef;
  33. struct node *newNode;
  34. newNode = malloc(sizeof(struct node));
  35. newNode->data = value;
  36. newNode->next = head;
  37. head = newNode;
  38. *headRef = head;
  39. }
  40. void insertAtEnd(struct node* head, int value){
  41. struct node *newNode;
  42. newNode = malloc(sizeof(struct node));
  43. newNode->data = value;
  44. newNode->next = NULL;
  45. struct node *temp = head;
  46. while(temp->next != NULL){
  47. temp = temp->next;
  48. }
  49. temp->next = newNode;
  50. }
  51. void deleteFromFront(struct node** headRef){
  52. struct node* head = *headRef;
  53. head = head->next;
  54. *headRef = head;
  55. }
  56. void deleteFromEnd(struct node* head){
  57. struct node* temp = head;
  58. while(temp->next->next!=NULL){
  59. temp = temp->next;
  60. }
  61. temp->next = NULL;
  62. }
  63. void deleteFromMiddle(struct node* head, int position){
  64. struct node* temp = head;
  65. int i;
  66. for(i=2; inext != NULL) {
  67. temp = temp->next;
  68. }
  69. }
  70. temp->next = temp->next->next;
  71. }
  72. int main() {
  73. /* Initialize nodes */
  74. struct node *head;
  75. struct node *one = NULL;
  76. struct node *two = NULL;
  77. struct node *three = NULL;
  78. /* Allocate memory */
  79. one = malloc(sizeof(struct node));
  80. two = malloc(sizeof(struct node));
  81. three = malloc(sizeof(struct node));
  82. /* Assign data values */
  83. one->data = 1;
  84. two->data = 2;
  85. three->data = 3;
  86. /* Connect nodes */
  87. one->next = two;
  88. two->next = three;
  89. three->next = NULL;
  90. /* Save address of first node in head */
  91. head = one;
  92. display(head); // 1 --->2 --->3 --->
  93. insertAtFront(&head, 4);
  94. display(head); // 4 --->1 --->2 --->3 --->
  95. deleteFromFront(&head);
  96. display(head); // 1 --->2 --->3 --->
  97. insertAtEnd(head, 5);
  98. display(head); // 1 --->2 --->3 --->5 --->
  99. deleteFromEnd(head);
  100. display(head); // 1 --->2 --->3 --->
  101. int position = 3;
  102. insertAtMiddle(head, position, 10);
  103. display(head); // 1 --->2 --->10 --->3 --->
  104. deleteFromMiddle(head, position);
  105. display(head); // 1 --->2 --->3 --->
  106. }
The output of the above program is
List elements are -                                                                                                                                                       
1 --->2 --->3 --->     
   
List elements are -                                                                                                                                                       
4 --->1 --->2 --->3 --->                                                                                                                                                  
                                                                                                                                                                             
List elements are -                                                                                                                                                       
1 --->2 --->3 --->                                                                                                                                                        
                                                                                                                                                                             
List elements are -                                                                                                                                                       
1 --->2 --->3 --->5 --->                                                                                                                                                  
                                                                                                                                                                             
List elements are -                                                                                                                                                       
1 --->2 --->3 --->                                                                                                                                                        
                                                                                                                                                                             
List elements are -                                                                                                                                                       
1 --->2 --->10 --->3 --->                                                                                                                                                 
                                                                                                                                                                             
List elements are -                                                                                                                                                          1 --->2 --->3 --->     

Comments

Popular posts from this blog

INTRODUCTION OF DBMS

Introduction of Digital computer

DEVELOPED, UNDEVELOPED AND DEVELOPING ECONOMIES || LIST, GDP, UNEMPLOYMENT