fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // โครงสร้าง node สำหรับลิงก์ลิสต์
  5. struct datanode {
  6. int data;
  7. struct datanode *link;
  8. };
  9.  
  10. // โครงสร้าง head node
  11. struct headnode {
  12. int count;
  13. struct datanode *head;
  14. };
  15.  
  16. // เพิ่ม node ที่ท้ายลิสต์
  17. void append(struct headnode *list, int value) {
  18. struct datanode *newNode = (struct datanode*)malloc(sizeof(struct datanode));
  19. newNode->data = value;
  20. newNode->link = NULL;
  21.  
  22. if (list->head == NULL) {
  23. list->head = newNode;
  24. } else {
  25. struct datanode *temp = list->head;
  26. while (temp->link != NULL) {
  27. temp = temp->link;
  28. }
  29. temp->link = newNode;
  30. }
  31. list->count++;
  32. }
  33.  
  34. // แสดงผลลิงก์ลิสต์
  35. void display(struct headnode *list) {
  36. struct datanode *temp = list->head;
  37. printf("Linked List: ");
  38. while (temp != NULL) {
  39. printf("%d", temp->data);
  40. if (temp->link != NULL) printf(" -> ");
  41. temp = temp->link;
  42. }
  43. printf("\nTotal nodes: %d\n", list->count);
  44. }
  45.  
  46. // แก้ไขค่าลิสต์ตามที่โจทย์ต้องการ
  47. void modifyList(struct headnode *list) {
  48. if (list->count < 12) return;
  49.  
  50. struct datanode *temp = list->head;
  51.  
  52. // ไปยังตำแหน่ง index 4 (ตำแหน่งที่ 5)
  53. for (int i = 0; i < 4 && temp != NULL; i++) {
  54. temp = temp->link;
  55. }
  56.  
  57. // เปลี่ยนค่าตำแหน่ง 5 และ 6 เป็น 7 และ 3
  58. if (temp != NULL) temp->data = 7;
  59. if (temp->link != NULL) temp->link->data = 3;
  60.  
  61. // ลบ node สุดท้าย (เลข 7 ด้านหลัง)
  62. struct datanode *prev = NULL;
  63. temp = list->head;
  64. while (temp->link != NULL) {
  65. prev = temp;
  66. temp = temp->link;
  67. }
  68. if (prev != NULL) {
  69. prev->link = NULL;
  70. free(temp);
  71. list->count--;
  72. }
  73. }
  74.  
  75. int main() {
  76. long long number = 672432060297LL;
  77. int digits[12];
  78.  
  79. // แยกตัวเลขแต่ละหลักใส่ใน array
  80. for (int i = 11; i >= 0; i--) {
  81. digits[i] = number % 10;
  82. number /= 10;
  83. }
  84.  
  85. // สร้าง linked list
  86. struct headnode *List = (struct headnode*)malloc(sizeof(struct headnode));
  87. List->count = 0;
  88. List->head = NULL;
  89.  
  90. for (int i = 0; i < 12; i++) {
  91. append(List, digits[i]);
  92. }
  93.  
  94. // แก้ไขค่าตามที่กำหนด
  95. modifyList(List);
  96.  
  97. // แสดงผลลัพธ์
  98. display(List);
  99.  
  100. // ล้างหน่วยความจำ
  101. struct datanode *current = List->head;
  102. while (current != NULL) {
  103. struct datanode *next = current->link;
  104. free(current);
  105. }
  106. free(List);
  107.  
  108. return 0;
  109. }
  110.  
Success #stdin #stdout 0.03s 25684KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>

// โครงสร้าง node สำหรับลิงก์ลิสต์
struct datanode {
    int data;
    struct datanode *link;
};

// โครงสร้าง head node
struct headnode {
    int count;
    struct datanode *head;
};

// เพิ่ม node ที่ท้ายลิสต์
void append(struct headnode *list, int value) {
    struct datanode *newNode = (struct datanode*)malloc(sizeof(struct datanode));
    newNode->data = value;
    newNode->link = NULL;

    if (list->head == NULL) {
        list->head = newNode;
    } else {
        struct datanode *temp = list->head;
        while (temp->link != NULL) {
            temp = temp->link;
        }
        temp->link = newNode;
    }
    list->count++;
}

// แสดงผลลิงก์ลิสต์
void display(struct headnode *list) {
    struct datanode *temp = list->head;
    printf("Linked List: ");
    while (temp != NULL) {
        printf("%d", temp->data);
        if (temp->link != NULL) printf(" -> ");
        temp = temp->link;
    }
    printf("\nTotal nodes: %d\n", list->count);
}

// แก้ไขค่าลิสต์ตามที่โจทย์ต้องการ
void modifyList(struct headnode *list) {
    if (list->count < 12) return;

    struct datanode *temp = list->head;

    // ไปยังตำแหน่ง index 4 (ตำแหน่งที่ 5)
    for (int i = 0; i < 4 && temp != NULL; i++) {
        temp = temp->link;
    }

    // เปลี่ยนค่าตำแหน่ง 5 และ 6 เป็น 7 และ 3
    if (temp != NULL) temp->data = 7;
    if (temp->link != NULL) temp->link->data = 3;

    // ลบ node สุดท้าย (เลข 7 ด้านหลัง)
    struct datanode *prev = NULL;
    temp = list->head;
    while (temp->link != NULL) {
        prev = temp;
        temp = temp->link;
    }
    if (prev != NULL) {
        prev->link = NULL;
        free(temp);
        list->count--;
    }
}

int main() {
    long long number = 672432060297LL;
    int digits[12];

    // แยกตัวเลขแต่ละหลักใส่ใน array
    for (int i = 11; i >= 0; i--) {
        digits[i] = number % 10;
        number /= 10;
    }

    // สร้าง linked list
    struct headnode *List = (struct headnode*)malloc(sizeof(struct headnode));
    List->count = 0;
    List->head = NULL;

    for (int i = 0; i < 12; i++) {
        append(List, digits[i]);
    }

    // แก้ไขค่าตามที่กำหนด
    modifyList(List);

    // แสดงผลลัพธ์
    display(List);

    // ล้างหน่วยความจำ
    struct datanode *current = List->head;
    while (current != NULL) {
        struct datanode *next = current->link;
        free(current);
        current = next;
    }
    free(List);

    return 0;
}