module book_module
type book_type
character(len=100) :: title
character(len=50) :: author
integer :: year
real :: price
end type book_type
type node_type
type(book_type) :: data
type(node_type), pointer :: next
end type node_type
contains
subroutine insert_node(head, new_book)
type(node_type), pointer :: head
type(book_type), intent(in) :: new_book
type(node_type), pointer :: new_node, current
allocate(new_node)
new_node%data = new_book
new_node%next => null()
if (.not. associated(head)) then
head => new_node
else
current => head
do while (associated(current%next))
current => current%next
end do
current%next => new_node
end if
end subroutine insert_node
subroutine delete_node(head, target_title)
type(node_type), pointer :: head
character(len=100), intent(in) :: target_title
type(node_type), pointer :: current, prev
current => head
prev => null()
do while (associated(current))
if (current%data%title == target_title) then
if (.not. associated(prev)) then
head => current%next
else
prev%next => current%next
end if
deallocate(current)
return
end if
prev => current
current => current%next
end do
end subroutine delete_node
subroutine find_node(head, target_title, found_book)
type(node_type), pointer :: head
character(len=100), intent(in) :: target_title
type(book_type), intent(out) :: found_book
type(node_type), pointer :: current
current => head
do while (associated(current))
if (current%data%title == target_title) then
found_book = current%data
return
end if
current => current%next
end do
found_book%title = ''
found_book%author = ''
found_book%year = 0
found_book%price = 0.0
end subroutine find_node
end module book_module
program main
use book_module
implicit none
type(node_type), pointer :: head => null()
type(book_type) :: new_book, found_book
new_book%title = 'Fortran Programming'
new_book%author = 'John Doe'
new_book%year = 2020
new_book%price = 30.0
call insert_node(head, new_book)
call find_node(head, 'Fortran Programming', found_book)
if (found_book%title /= '') then
write(*,*) 'Book found:'
write(*,*) 'Title:', found_book%title
write(*,*) 'Author:', found_book%author
write(*,*) 'Year:', found_book%year
write(*,*) 'Price:', found_book%price
else
write(*,*) 'Book not found.'
end if
call delete_node(head, 'Fortran Programming')
call find_node(head, 'Fortran Programming', found_book)
if (found_book%title /= '') then
write(*,*) 'Book found after deletion (should not happen).'
else
write(*,*) 'Book not found after deletion (correct).'
end if
end program main