fork download
  1. // // C Program for Implementation of Singly Linked List
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Define the Node structure
  6. struct Node {
  7. int data;
  8. struct Node* next;
  9. };
  10.  
  11. // Function to create a new node
  12. struct Node* createNode(int data) {
  13. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  14. newNode->data = data;
  15. newNode->next = NULL;
  16. return newNode;
  17. }
  18.  
  19. // Function to insert a new element at the beginning of the singly linked list
  20. void insertAtFirst(struct Node** head, int data) {
  21. struct Node* newNode = createNode(data);
  22. newNode->next = *head;
  23. *head = newNode;
  24. }
  25.  
  26. // Function to insert a new element at the end of the singly linked list
  27. void insertAtEnd(struct Node** head, int data) {
  28. struct Node* newNode = createNode(data);
  29. if (*head == NULL) {
  30. *head = newNode;
  31. return;
  32. }
  33. struct Node* temp = *head;
  34. while (temp->next != NULL) {
  35. temp = temp->next;
  36. }
  37. temp->next = newNode;
  38. }
  39.  
  40. // Function to insert a new element at a specific position in the singly linked list
  41. void insertAtPosition(struct Node** head, int data, int position) {
  42. struct Node* newNode = createNode(data);
  43. if (position == 0) {
  44. insertAtFirst(head,data);
  45. return;
  46. }
  47. struct Node* temp = *head;
  48. for (int i = 0; temp != NULL && i < position - 1; i++) {
  49. temp = temp->next;
  50. }
  51. if (temp == NULL) {
  52. printf("Position out of range\n");
  53. free(newNode);
  54. return;
  55. }
  56. newNode->next = temp->next;
  57. temp->next = newNode;
  58. }
  59.  
  60. // Function to delete the first node of the singly linked list
  61. void deleteFromFirst(struct Node** head) {
  62. if (*head == NULL) {
  63. printf("List is empty\n");
  64. return;
  65. }
  66. struct Node* temp = *head;
  67. *head = temp->next;
  68. free(temp);
  69. }
  70.  
  71. // Function to delete the last node of the singly linked list
  72. void deleteFromEnd(struct Node** head) {
  73. if (*head == NULL) {
  74. printf("List is empty\n");
  75. return;
  76. }
  77. struct Node* temp = *head;
  78. if (temp->next == NULL) {
  79. free(temp);
  80. *head = NULL;
  81. return;
  82. }
  83. while (temp->next->next != NULL) {
  84. temp = temp->next;
  85. }
  86. free(temp->next);
  87. temp->next = NULL;
  88. }
  89.  
  90. // Function to delete a node at a specific position in the singly linked list
  91. void deleteAtPosition(struct Node** head, int position) {
  92. if (*head == NULL) {
  93. printf("List is empty\n");
  94. return;
  95. }
  96. struct Node* temp = *head;
  97. if (position == 0) {
  98. deleteFromFirst(head);
  99. return;
  100. }
  101. for (int i = 0; temp != NULL && i < position - 1; i++) {
  102. temp = temp->next;
  103. }
  104. if (temp == NULL || temp->next == NULL) {
  105. printf("Position out of range\n");
  106. return;
  107. }
  108. struct Node* next = temp->next->next;
  109. free(temp->next);
  110. temp->next = next;
  111. }
  112.  
  113. // Function to print the LinkedList
  114. void print(struct Node* head) {
  115. struct Node* temp = head;
  116. while (temp != NULL) {
  117. printf("%d -> ", temp->data);
  118. temp = temp->next;
  119. }
  120. printf("NULL\n");
  121. }
  122.  
  123. // Driver Code
  124. int main() {
  125. struct Node* head = NULL;
  126.  
  127. insertAtFirst(&head, 10);
  128. printf("Linked list after inserting the node:10 at the beginning \n");
  129. print(head);
  130.  
  131. printf("Linked list after inserting the node:20 at the end \n");
  132. insertAtEnd(&head, 20);
  133. print(head);
  134.  
  135. printf("Linked list after inserting the node:5 at the end \n");
  136. insertAtEnd(&head, 5);
  137. print(head);
  138.  
  139. printf("Linked list after inserting the node:30 at the end \n");
  140. insertAtEnd(&head, 30);
  141. print(head);
  142.  
  143. printf("Linked list after inserting the node:15 at position 2 \n");
  144. insertAtPosition(&head, 15, 2);
  145. print(head);
  146.  
  147. printf("Linked list after deleting the first node: \n");
  148. deleteFromFirst(&head);
  149. print(head);
  150.  
  151. printf("Linked list after deleting the last node: \n");
  152. deleteFromEnd(&head);
  153. print(head);
  154.  
  155. printf("Linked list after deleting the node at position 1: \n");
  156. deleteAtPosition(&head, 1);
  157. print(head);
  158.  
  159. return 0;
  160. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Linked list after inserting the node:10 at the beginning 
10 -> NULL
Linked list after inserting the node:20 at the end 
10 -> 20 -> NULL
Linked list after inserting the node:5 at the end 
10 -> 20 -> 5 -> NULL
Linked list after inserting the node:30 at the end 
10 -> 20 -> 5 -> 30 -> NULL
Linked list after inserting the node:15 at position 2 
10 -> 20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the first node: 
20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the last node: 
20 -> 15 -> 5 -> NULL
Linked list after deleting the node at position 1: 
20 -> 5 -> NULL