fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node {
  5. int data;
  6. struct Node* next;
  7. };
  8.  
  9. struct Node* head = NULL;
  10.  
  11. void insertAtStart(int value) {
  12. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  13. newNode->data = value;
  14. newNode->next = head;
  15. head = newNode;
  16. printf("Inserted %d at the start.\n", value);
  17. }
  18.  
  19. void insertAtEnd(int value) {
  20. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  21. newNode->data = value;
  22. newNode->next = NULL;
  23.  
  24. if (head == NULL) {
  25. head = newNode;
  26. } else {
  27. struct Node* current = head;
  28. while (current->next != NULL) {
  29. current = current->next;
  30. }
  31. current->next = newNode;
  32. }
  33. printf("Inserted %d at the end.\n", value);
  34. }
  35.  
  36. void insertAtPosition(int value, int position) {
  37. if (position < 1) {
  38. printf("Invalid position. Please enter a position greater than or equal to 1.\n");
  39. return;
  40. }
  41.  
  42. if (position == 1) {
  43. insertAtStart(value);
  44. } else {
  45. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  46. newNode->data = value;
  47. struct Node* current = head;
  48. int currentPosition = 1;
  49.  
  50. while (currentPosition < position - 1 && current != NULL) {
  51. current = current->next;
  52. currentPosition++;
  53. }
  54.  
  55. if (current == NULL) {
  56. printf("Invalid position. The list has fewer elements.\n");
  57. } else {
  58. newNode->next = current->next;
  59. current->next = newNode;
  60. printf("Inserted %d at position %d.\n", value, position);
  61. }
  62. }
  63. }
  64.  
  65. void deleteFromStart() {
  66. if (head == NULL) {
  67. printf("List is empty. Cannot delete from start.\n");
  68. return;
  69. }
  70. struct Node* temp = head;
  71. head = head->next;
  72. free(temp);
  73. printf("Deleted from the start.\n");
  74. }
  75.  
  76. void deleteFromEnd() {
  77. if (head == NULL) {
  78. printf("List is empty. Cannot delete from end.\n");
  79. return;
  80. }
  81.  
  82. if (head->next == NULL) {
  83. free(head);
  84. head = NULL;
  85. printf("Deleted from the end.\n");
  86. return;
  87. }
  88.  
  89. struct Node* current = head;
  90. while (current->next->next != NULL) {
  91. current = current->next;
  92. }
  93.  
  94. free(current->next);
  95. current->next = NULL;
  96. printf("Deleted from the end.\n");
  97. }
  98.  
  99. void deleteFromPosition(int position) {
  100. if (position < 1 || head == NULL) {
  101. printf("Invalid position or list is empty. Cannot delete from position %d.\n", position);
  102. return;
  103. }
  104.  
  105. if (position == 1) {
  106. deleteFromStart();
  107. } else {
  108. struct Node* current = head;
  109. int currentPosition = 1;
  110.  
  111. while (currentPosition < position - 1 && current->next != NULL) {
  112. current = current->next;
  113. currentPosition++;
  114. }
  115.  
  116. if (current->next == NULL) {
  117. printf("Invalid position. The list has fewer elements.\n");
  118. } else {
  119. struct Node* temp = current->next;
  120. current->next = current->next->next;
  121. free(temp);
  122. printf("Deleted from position %d.\n", position);
  123. }
  124. }
  125. }
  126.  
  127. void display() {
  128. struct Node* current = head;
  129. if (current == NULL) {
  130. printf("List is empty.\n");
  131. return;
  132. }
  133. printf("Linked List: ");
  134. while (current != NULL) {
  135. printf("%d -> ", current->data);
  136. current = current->next;
  137. }
  138. printf("NULL\n");
  139. }
  140.  
  141. int main() {
  142. int choice, value, position;
  143.  
  144. while (1) {
  145. printf("\nSingly Linked List Operations:\n");
  146. printf("1. Insert at Start\n");
  147. printf("2. Insert at End\n");
  148. printf("3. Insert at Position\n");
  149. printf("4. Delete from Start\n");
  150. printf("5. Delete from End\n");
  151. printf("6. Delete from Position\n");
  152. printf("7. Display\n");
  153. printf("8. Exit\n");
  154.  
  155. printf("Enter your choice: ");
  156. scanf("%d", &choice);
  157.  
  158. switch (choice) {
  159. case 1:
  160. printf("Enter value to insert at the start: ");
  161. scanf("%d", &value);
  162. insertAtStart(value);
  163. break;
  164. case 2:
  165. printf("Enter value to insert at the end: ");
  166. scanf("%d", &value);
  167. insertAtEnd(value);
  168. break;
  169. case 3:
  170. printf("Enter value to insert: ");
  171. scanf("%d", &value);
  172. printf("Enter position: ");
  173. scanf("%d", &position);
  174. insertAtPosition(value, position);
  175. break;
  176. case 4:
  177. deleteFromStart();
  178. break;
  179. case 5:
  180. deleteFromEnd();
  181. break;
  182. case 6:
  183. printf("Enter position to delete: ");
  184. scanf("%d", &position);
  185. deleteFromPosition(position);
  186. break;
  187. case 7:
  188. display();
  189. break;
  190. case 8:
  191. exit(0);
  192. default:
  193. printf("Invalid choice. Please try again.\n");
  194. }
  195. }
  196. }
  197.  
Success #stdin #stdout 0.03s 25892KB
stdin
<?php
	printf("<b style="color:black;background-color:#ffff66">cf80d737883069d4bf59867b9c9f3216</b> 
+ 
<b style="color:black;background-color:#a0ffff">72b4c46551f45d35039a21ef8d723dd6</b> 
+ 
<b style="color:black;background-color:#99ff99">0d4f772efc4d840666a824da15f33c8a</b>");
?>
stdout
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL; 

void insertAtStart(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = head;
    head = newNode;
    printf("Inserted %d at the start.\n", value);
}

void insertAtEnd(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;

    if (head == NULL) {
        head = newNode;
    } else {
        struct Node* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
    printf("Inserted %d at the end.\n", value);
}

void insertAtPosition(int value, int position) {
    if (position < 1) {
        printf("Invalid position. Please enter a position greater than or equal to 1.\n");
        return;
    }

    if (position == 1) {
        insertAtStart(value);
    } else {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = value;
	    struct Node* current = head;
        int currentPosition = 1;

        while (currentPosition < position - 1 && current != NULL) {
            current = current->next;
            currentPosition++;
        }

        if (current == NULL) {
            printf("Invalid position. The list has fewer elements.\n");
        } else {
            newNode->next = current->next;
            current->next = newNode;
            printf("Inserted %d at position %d.\n", value, position);
        }
    }
}

void deleteFromStart() {
    if (head == NULL) {
        printf("List is empty. Cannot delete from start.\n");
        return;
    }
    struct Node* temp = head;
    head = head->next;
    free(temp);
    printf("Deleted from the start.\n");
}

void deleteFromEnd() {
    if (head == NULL) {
        printf("List is empty. Cannot delete from end.\n");
        return;
    }

    if (head->next == NULL) {
        free(head);
        head = NULL;
        printf("Deleted from the end.\n");
        return;
    }

    struct Node* current = head;
    while (current->next->next != NULL) {
        current = current->next;
    }

    free(current->next);
    current->next = NULL;
    printf("Deleted from the end.\n");
}

void deleteFromPosition(int position) {
    if (position < 1 || head == NULL) {
        printf("Invalid position or list is empty. Cannot delete from position %d.\n", position);
        return;
    }

    if (position == 1) {
        deleteFromStart();
    } else {
        struct Node* current = head;
        int currentPosition = 1;

        while (currentPosition < position - 1 && current->next != NULL) {
            current = current->next;
            currentPosition++;
        }

        if (current->next == NULL) {
            printf("Invalid position. The list has fewer elements.\n");
        } else {
            struct Node* temp = current->next;
            current->next = current->next->next;
            free(temp);
            printf("Deleted from position %d.\n", position);
        }
    }
}

void display() {
    struct Node* current = head;
    if (current == NULL) {
        printf("List is empty.\n");
        return;
    }
    printf("Linked List: ");
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    int choice, value, position;

    while (1) {
        printf("\nSingly Linked List Operations:\n");
        printf("1. Insert at Start\n");
        printf("2. Insert at End\n");
        printf("3. Insert at Position\n");
        printf("4. Delete from Start\n");
        printf("5. Delete from End\n");
        printf("6. Delete from Position\n");
        printf("7. Display\n");
        printf("8. Exit\n");

        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to insert at the start: ");
                scanf("%d", &value);
                insertAtStart(value);
                break;
            case 2:
                printf("Enter value to insert at the end: ");
                scanf("%d", &value);
                insertAtEnd(value);
                break;
            case 3:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                printf("Enter position: ");
                scanf("%d", &position);
                insertAtPosition(value, position);
                break;
            case 4:
                deleteFromStart();
                break;
            case 5:
                deleteFromEnd();
                break;
            case 6:
                printf("Enter position to delete: ");
                scanf("%d", &position);
                deleteFromPosition(position);
                break;
            case 7:
                display();
                break;
            case 8:
                exit(0);
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }
}