Advertisement
Shakil_Hossain

last insert

Feb 26th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. // A simple C program to introduce
  2. // a linked list
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. struct Node {
  7.     int data;
  8.     struct Node* next;
  9. };
  10.  
  11. // Program to create a simple linked
  12. // list with 3 nodes
  13. int main()
  14. {
  15.     struct Node* head = NULL;
  16.     struct Node* second = NULL;
  17.     struct Node* third = NULL;
  18.     struct Node* temp = NULL;
  19.     struct Node* insert = NULL;
  20.  
  21.     // allocate 3 nodes in the heap
  22.     head = (struct Node*)malloc(sizeof(struct Node));
  23.     second = (struct Node*)malloc(sizeof(struct Node));
  24.     third = (struct Node*)malloc(sizeof(struct Node));
  25.     insert = (struct Node*)malloc(sizeof(struct Node));
  26.  
  27.     /* Three blocks have been allocated dynamically.
  28.     We have pointers to these three blocks as first,
  29.     second and third
  30.     head         second      third
  31.         |            |           |
  32.         |            |           |
  33.     +---+-----+  +----+----+     +----+----+
  34.     | # | # |    | # | # |   | # | # |
  35.     +---+-----+  +----+----+     +----+----+
  36.  
  37. # represents any random value.
  38. Data is random because we haven’t assigned
  39. anything yet */
  40.  
  41.     head->data = 1; // assign data in first node
  42.     head->next = second; // Link first node with
  43.     // the second node
  44.  
  45.     /* data has been assigned to the data part of the first
  46.     block (block pointed by the head). And next
  47.     pointer of first block points to second.
  48.     So they both are linked.
  49.  
  50.     head         second      third
  51.         |            |           |
  52.         |            |           |
  53.     +---+---+    +----+----+     +-----+----+
  54.     | 1 | o----->| # | # |   | # | # |
  55.     +---+---+    +----+----+     +-----+----+
  56. */
  57.  
  58.     // assign data to second node
  59.     second->data = 2;
  60.  
  61.     // Link second node with the third node
  62.     second->next = third;
  63.  
  64.     /* data has been assigned to the data part of the second
  65.     block (block pointed by second). And next
  66.     pointer of the second block points to the third
  67.     block. So all three blocks are linked.
  68.  
  69.     head         second      third
  70.         |            |           |
  71.         |            |           |
  72.     +---+---+    +---+---+   +----+----+
  73.     | 1 | o----->| 2 | o-----> | # | # |
  74.     +---+---+    +---+---+   +----+----+     */
  75.  
  76.     third->data = 3; // assign data to third node
  77.     third->next = NULL;
  78.  
  79.     /* data has been assigned to data part of third
  80.     block (block pointed by third). And next pointer
  81.     of the third block is made NULL to indicate
  82.     that the linked list is terminated here.
  83.  
  84.     We have the linked list ready.
  85.  
  86.         head
  87.             |
  88.             |
  89.         +---+---+    +---+---+   +----+------+
  90.         | 1 | o----->| 2 | o-----> | 3 | NULL |
  91.         +---+---+    +---+---+   +----+------+
  92.  
  93.  
  94.     Note that only head is sufficient to represent
  95.     the whole list. We can traverse the complete
  96.     list by following next pointers. */
  97.     insert->data =5;
  98.     third->next=insert;
  99.     insert->next=NULL;
  100.     temp=head;
  101.     while (temp != 0)
  102.     {
  103.         printf("[%d]   [%d]",temp,temp->data);
  104.  
  105.         temp = temp->next;
  106.     }
  107.         printf("[NULL]");
  108.    
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement