Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. /* Structure of a node */
  6. struct node {
  7. int data; // Data
  8. struct node *next; // Address
  9. }*head;
  10.  
  11.  
  12. void createList(int n);
  13. void insertNodeAtEnd(int data);
  14. void displayList();
  15.  
  16.  
  17. int main()
  18. {
  19. int n, data;
  20.  
  21. /*
  22. * Create a singly linked list of n nodes
  23. */
  24. printf("Enter the total number of nodes: ");
  25. scanf("%d", &n);
  26. createList(n);
  27.  
  28. printf("\nData in the list \n");
  29. displayList();
  30.  
  31. /*
  32. * Insert data at the end of the singly linked list
  33. */
  34. printf("\nEnter data to insert at end of the list: ");
  35. scanf("%d", &data);
  36. insertNodeAtEnd(data);
  37.  
  38. printf("\nData in the list \n");
  39. displayList();
  40.  
  41. return 0;
  42. }
  43.  
  44.  
  45. /*
  46. * Create a list of n nodes
  47. */
  48. void createList(int n)
  49. {
  50. struct node *newNode, *temp;
  51. int data, i;
  52.  
  53. head = (struct node *)malloc(sizeof(struct node));
  54.  
  55. /*
  56. * If unable to allocate memory for head node
  57. */
  58. if(head == NULL)
  59. {
  60. printf("Unable to allocate memory.");
  61. }
  62. else
  63. {
  64. /*
  65. * Reads data of node from the user
  66. */
  67. printf("Enter the data of node 1: ");
  68. scanf("%d", &data);
  69.  
  70. head->data = data; // Link the data field with data
  71. head->next = NULL; // Link the address field to NULL
  72.  
  73. temp = head;
  74.  
  75. /*
  76. * Create n nodes and adds to linked list
  77. */
  78. for(i=2; i<=n; i++)
  79. {
  80. newNode = (struct node *)malloc(sizeof(struct node));
  81.  
  82. /* If memory is not allocated for newNode */
  83. if(newNode == NULL)
  84. {
  85. printf("Unable to allocate memory.");
  86. break;
  87. }
  88. else
  89. {
  90. printf("Enter the data of node %d: ", i);
  91. scanf("%d", &data);
  92.  
  93. newNode->data = data; // Link the data field of newNode with data
  94. newNode->next = NULL; // Link the address field of newNode with NULL
  95.  
  96. temp->next = newNode; // Link previous node i.e. temp to the newNode
  97. temp = temp->next;
  98. }
  99. }
  100.  
  101. printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
  102. }
  103. }
  104.  
  105.  
  106. /*
  107. * Create a new node and inserts at the end of the linked list.
  108. */
  109. void insertNodeAtEnd(int data)
  110. {
  111. struct node *newNode, *temp;
  112.  
  113. newNode = (struct node*)malloc(sizeof(struct node));
  114.  
  115. if(newNode == NULL)
  116. {
  117. printf("Unable to allocate memory.");
  118. }
  119. else
  120. {
  121. newNode->data = data; // Link the data part
  122. newNode->next = NULL;
  123.  
  124. temp = head;
  125.  
  126. // Traverse to the last node
  127. while(temp->next != NULL)
  128. temp = temp->next;
  129.  
  130. temp->next = newNode; // Link address part
  131.  
  132. printf("DATA INSERTED SUCCESSFULLY\n");
  133. }
  134. }
  135.  
  136.  
  137. /*
  138. * Display entire list
  139. */
  140. void displayList()
  141. {
  142. struct node *temp;
  143.  
  144. /*
  145. * If the list is empty i.e. head = NULL
  146. */
  147. if(head == NULL)
  148. {
  149. printf("List is empty.");
  150. }
  151. else
  152. {
  153. temp = head;
  154. while(temp != NULL)
  155. {
  156. printf("Data = %d\n", temp->data); // Print data of current node
  157. temp = temp->next; // Move to next node
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement