# เทคนิคการเขียนโค้ดเพื่อการจัดการข้อมูลในภาษา Fortran โดยใช้ Queue
การจัดการข้อมูลเป็นหัวใจสำคัญของการเขียนโปรแกรม ไม่ว่าจะเป็นภาษาไหนก็ตาม สำหรับภาษา Fortran ที่เรียกได้ว่าเป็นภาษาโบราณ แต่ความสามารถของมันในการจัดการข้อมูลทางวิทยาศาสตร์และวิศวกรรมยังคงปฏิเสธไม่ได้ ในบทความนี้ เราจะมาพูดถึงเทคนิคในการใช้โครงสร้างข้อมูลแบบ Queue เพื่อการจัดการข้อมูลในภาษา Fortran รวมถึงการ insert, update, find และ delete ข้อมูล พร้อมทั้งข้อดีและข้อเสียในการใช้งาน
Queue คือโครงสร้างข้อมูลประเภทหนึ่งที่ทำงานตามหลัก First-In First-Out (FIFO) คือข้อมูลที่เข้ามาก่อนจะเป็นข้อมูลที่ออกไปก่อนเสมอ คิดเหมือนกับคิวของคนที่ยืนรอบริการ คนที่มาก่อนคือคนที่จะได้รับบริการก่อน นั่นคือแนวคิดหลักของ Queue
Fortran ไม่ได้มี library มาตรฐานสำหรับ Queue เหมือนบางภาษาสมัยใหม่ อย่าง JavaScript หรือ Python ดังนั้นเราจะต้องสร้างโครงสร้างข้อมูลนี้ด้วยตัวเอง โดยการกำหนดประเภทข้อมูล (data type) และสร้างโมดูลสำหรับการจัดการ Queue
สร้างโมดูลสำหรับ Queue
module queue_mod
    implicit none
    private
    public :: queue, enqueue, dequeue, update, find, delete
    type :: node
        integer :: data
        type(node), pointer :: next => null()
    end type node
    type :: queue
        type(node), pointer :: front => null()
        type(node), pointer :: rear => null()
    end type queue
contains
    subroutine enqueue(Q, element)
        ! ใส่ข้อมูลเข้า Queue
    end subroutine enqueue
    subroutine dequeue(Q, element)
        ! นำข้อมูลออกจาก Queue
    end subroutine dequeue
    subroutine find(Q, target, found, position)
        ! ค้นหาข้อมูลใน Queue
    end subroutine find
    subroutine update(Q, position, newElement)
        ! อัปเดตข้อมูลใน Queue
    end subroutine update
    subroutine delete(Q, position)
        ! ลบข้อมูลใน Queue
    end subroutine delete
    ! โค้ดเพิ่มเติมสำหรับการทำงานอื่นๆ ตามการตั้งค่าข้างต้น
end module queue_mod
Insert (Enqueue)
ฟังก์ชัน `enqueue` จะทำการเพิ่มข้อมูลไปยังท้าย queue:
subroutine enqueue(Q, element)
    type(queue), intent(inout) :: Q
    integer, intent(in) :: element
    type(node), pointer :: newNode
    allocate(newNode)
    newNode%data = element
    newNode%next => null()
    if (.not. associated(Q%front)) then
       Q%front => newNode
       Q%rear => newNode
    else
       Q%rear%next => newNode
       Q%rear => newNode
    end if
end subroutine enqueue
Dequeue
การถอดข้อมูลจาก queue (`dequeue`) กระทำโดยการลบ node ที่อยู่หน้าสุดของ queue:
subroutine dequeue(Q, element)
    type(queue), intent(inout) :: Q
    integer, intent(out) :: element
    type(node), pointer :: tempPtr
    if (associated(Q%front)) then
        tempPtr => Q%front
        element = tempPtr%data
        Q%front => Q%front%next
        if (.not. associated(Q%front)) then
            Q%rear => null()
        end if
        deallocate(tempPtr)
    else
        print *, "Queue is empty"
    end if
end subroutine dequeue
Find
การค้นหาข้อมูล (`find`) ใน queue สามารถทำได้โดยการตรวจทุก node จนกว่าจะพบข้อมูลที่ต้องการ:
subroutine find(Q, target, found, position)
    type(queue), intent(in) :: Q
    integer, intent(in) :: target
    logical, intent(out) :: found
    integer, intent(out) :: position
    type(node), pointer :: currentNode
    integer :: count
    count = 0
    found = .false.
    currentNode => Q%front
    do while (associated(currentNode) .and. .not. found)
        count = count + 1
        if (currentNode%data == target) then
            found = .true.
            position = count
        end if
        currentNode => currentNode%next
    end do
end subroutine find
Update
การอัปเดตข้อมูลใน queue (`update`) เป็นกระบวนการที่หา node ด้วย position แล้วเปลี่ยนแปลงข้อมูล:
subroutine update(Q, position, newElement)
    type(queue), intent(inout) :: Q
    integer, intent(in) :: position, newElement
    type(node), pointer :: currentNode
    integer :: count
    currentNode => Q%front
    count = 0
    do while (associated(currentNode) .and. count < position)
        count = count + 1
        if (count == position) then
            currentNode%data = newElement
            exit
        end if
        currentNode => currentNode%next
    end do
    if (count /= position) then
        print *, "Position out of range"
    end if
end subroutine update
Delete
การลบข้อมูล (`delete`) นั้นเกิดขึ้นโดยการหา node ตาม position จากนั้นจึงทำการหลายข้อมูลนั้นออกจาก queue:
subroutine delete(Q, position)
    type(queue), intent(inout) :: Q
    integer, intent(in) :: position
    type(node), pointer :: currentNode, prevNode
    integer :: count
    prevNode => null()
    currentNode => Q%front
    count = 0
    do while (associated(currentNode) .and. count < position)
        count = count + 1
        if (count == position) then
            if (associated(prevNode)) then
                prevNode%next => currentNode%next
            else
                Q%front => currentNode%next
            end if
            if (.not. associated(Q%front)) then
                Q%rear => null()
            end if
            deallocate(currentNode)
            exit
        end if
        prevNode => currentNode
        currentNode => currentNode%next
    end do
    if (count /= position) then
        print *, "Position out of range"
    end if
end subroutine delete
ข้อดีของการใช้งาน Queue
- ความเรียบง่าย: โครงสร้างแบบ Queue เข้าใจง่ายและสามารถใช้ได้กับหลากหลายชนิดข้อมูลในการจัดการอคิว - ประสิทธิภาพ: ในการจัดการประเภทการทำงานตามลำดับเช่นการประมวลผลข้อมูลทางวิทยาศาสตร์ข้อเสียของการใช้งาน Queue
- ความซับซ้อน: ต้องสร้างโครงสร้างด้วยตัวเองใน Fortran ที่ไม่มี library สำเร็จรูป - การเข้าถึงข้อมูล: Queue ไม่สามารถเข้าถึงข้อมูลที่อยู่ตรงกลางได้ง่าย ๆ ต้องทำการลบหรือเพิ่มใหม่เพื่อเข้าถึงข้อมูลการเขียนโปรแกรมไม่ใช่เพียงการทำให้โค้ดนั้นทำงานได้ แต่ยังควรมองถึงการออกแบบที่มีประสิทธิภาพและการทำงานที่เหมาะสมกับปัญหาที่เราพยายามแก้ไข การใช้ Queue ใน Fortran อาจจะมีความท้าทายในบางด้าน แต่ก็สามารถช่วยในการจัดการกับงานที่ต้องการการประมวลผลที่เรียบง่ายและมีความต่อเนื่อง
สำหรับคุณที่อาจจะสนใจจะศึกษาภาษาโปรแกรมมิ่งและโครงสร้างข้อมูลอื่น ๆ แบบลึกซึ้งและเข้าใจคอนเซปต์ในการใช้งานอย่างถ่องแท้ ณ Expert-Programming-Tutor (EPT) เรามีหลักสูตรและตัวอย่างโค้ดที่จะทำให้คุณมั่นใจในการเขียนโปรแกรมของคุณ เรียนรู้กับเราและพัฒนาทักษะการเขียนโปรแกรมของคุณให้ไปถึงระดับต่อไป!
หมายเหตุ: ข้อมูลในบทความนี้อาจจะผิด โปรดตรวจสอบความถูกต้องของบทความอีกครั้งหนึ่ง บทความนี้ไม่สามารถนำไปใช้อ้างอิงใด ๆ ได้ ทาง EPT ไม่ขอยืนยันความถูกต้อง และไม่ขอรับผิดชอบต่อความเสียหายใดที่เกิดจากบทความชุดนี้ทั้งทางทรัพย์สิน ร่างกาย หรือจิตใจของผู้อ่านและผู้เกี่ยวข้อง
Tag ที่น่าสนใจ: fortran queue data_structure programming insert update find delete enqueue dequeue search array algorithm efficiency implementation
หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor
085-350-7540 (DTAC) 
    084-88-00-255 (AIS) 
    026-111-618 
    หรือทาง EMAIL:  NTPRINTF@GMAIL.COM