Advertisement
Sabbir-bin

insert fisrst in liked list

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