#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;
}
