MST

星途 面试题库

面试题:Fortran变量声明与使用技巧之专家难度题

在Fortran中,如何通过指针和动态内存分配来灵活处理复杂的数据结构,比如链表。请编写一个完整的程序,实现链表的创建、插入节点和删除节点的功能,同时要注意内存管理,避免内存泄漏。请详细注释每一步的操作。
11.0万 热度难度
编程语言Fortran

知识考点

AI 面试

面试题答案

一键面试
! 程序用于演示Fortran中通过指针和动态内存分配实现链表操作
program linked_list
    implicit none

    ! 定义链表节点类型
    type :: node
        integer :: data
        type(node), pointer :: next => null()
    end type node

    type(node), pointer :: head => null()
    integer :: choice, value

    do
       ! 显示菜单
       write(*,*) '1. 创建链表'
       write(*,*) '2. 插入节点'
       write(*,*) '3. 删除节点'
       write(*,*) '4. 退出'
       write(*,*) '请选择操作: '
       read(*,*) choice

       select case (choice)
       case (1)
           call create_list(head)
       case (2)
           write(*,*) '请输入要插入的值: '
           read(*,*) value
           call insert_node(head, value)
       case (3)
           write(*,*) '请输入要删除的值: '
           read(*,*) value
           call delete_node(head, value)
       case (4)
           call free_list(head)
           exit
       case default
           write(*,*) '无效的选择,请重新输入。'
       end select
    end do

contains

    ! 创建链表
    subroutine create_list(head)
        type(node), pointer :: head
        integer :: n, i, value

        write(*,*) '请输入链表节点的数量: '
        read(*,*) n

        do i = 1, n
            write(*,*) '请输入第', i, '个节点的值: '
            read(*,*) value
            call insert_node(head, value)
        end do
    end subroutine create_list

    ! 插入节点
    subroutine insert_node(head, value)
        type(node), pointer :: head
        integer :: value
        type(node), pointer :: new_node, current

        ! 分配新节点的内存
        allocate(new_node)
        new_node%data = value
        new_node%next => null()

        if (head == null()) then
            ! 如果链表为空,新节点成为头节点
            head => new_node
        else
            current => head
            do while (current%next /= null())
                current => current%next
            end do
            ! 将新节点插入到链表末尾
            current%next => new_node
        end if
    end subroutine insert_node

    ! 删除节点
    subroutine delete_node(head, value)
        type(node), pointer :: head
        integer :: value
        type(node), pointer :: current, prev

        if (head == null()) then
            write(*,*) '链表为空,无法删除。'
            return
        end if

        if (head%data == value) then
            ! 如果要删除的是头节点
            current => head
            head => head%next
            deallocate(current)
            return
        end if

        prev => head
        current => head%next

        do while (current /= null())
            if (current%data == value) then
                prev%next => current%next
                deallocate(current)
                return
            end if
            prev => current
            current => current%next
        end do

        write(*,*) '未找到要删除的值。'
    end subroutine delete_node

    ! 释放链表内存
    subroutine free_list(head)
        type(node), pointer :: head
        type(node), pointer :: current, next_node

        current => head
        do while (current /= null())
            next_node => current%next
            deallocate(current)
            current => next_node
        end do
        head => null()
    end subroutine free_list
end program linked_list

代码说明:

  1. 定义链表节点类型:使用type定义链表节点,包含一个整数值data和一个指向下一个节点的指针next
  2. 主程序部分:通过一个循环显示菜单,用户可以选择创建链表、插入节点、删除节点或退出程序。根据用户选择调用相应的子例程。
  3. create_list子例程:提示用户输入链表节点的数量,并逐个提示输入节点的值,然后调用insert_node子例程将节点插入链表。
  4. insert_node子例程:为新节点分配内存,设置其值,并将其插入链表的末尾。如果链表为空,则新节点成为头节点。
  5. delete_node子例程:查找要删除的节点。如果是头节点,直接更新头节点指针并释放内存。如果是其他节点,调整前一个节点的指针并释放要删除节点的内存。
  6. free_list子例程:遍历链表,逐个释放节点的内存,确保不会发生内存泄漏。