Pointer Applications

Uses of Pointers

•     pointers can be used to save on memory space and also copying of large data

•     useful when amount of data varies during program execution, memory wastage avoided

•     allows building complex relationships amongst data items

–   helps in locating desired data more efficiently

–   updating can be done more easily

Sorting Records

•     a record usually contains large amount of data

•     sort records according to different criteria

•     a single array used to keep records

•     instead of swapping records, keep an array of pointers and swap pointers when sorting

•     movement of large records avoided

•     only one copy of records needs to be maintained

•     array of pointers for each sorting criterion

Sorting Long Strings

program sort

implicit none

integer, parameter :: max_length=500, max_no = 100

type :: char_pt

character(len=max_length), pointer :: ptr

end type char

type(char_pt), dimension(max_no) :: strings

type(char_pt) :: swap

integer :: i, j, n

! instead of using an array of character strings, use an

! array of pointers to character strings

print *, “enter number of strings, maximum  “, max_no

read *, n

do i = 1, n

allocate(strings(i)%ptr)

read *, strings(i)%ptr

end do

do i = 1, n-1

do j = 1, n-i

if ( strings(j)%ptr > strings(j+1)%ptr) then

swap%ptr => strings(j)%ptr

strings(j)%ptr => strings(j+1)%ptr

strings(j+1)%ptr => swap%ptr

! inefficient  bubble sort

endif

end do

end do

print “(a)”, (trim(strings(i)%ptr), i = 1,n)

end program sort

Lists

•     pointers are especially useful for keeping lists of data values

•     a list is a sequence but the size of the list can keep changing during program execution

–   list of students present

–   list of students currently in the library

•     items are inserted or deleted one at a time from a list – arrays are unsuitable for such operations

•     can also be used to represent sets of data values

Lists With Pointers

•     lists implemented conveniently with pointers

•     list identified by a pointer pointing to the first element of the list

•     each element of the list contains a pointer to the next element in the list

•     all elements can be reached starting from the first element

•     elements can be inserted or deleted easily

•     space used is proportional to actual size of list

List Module

module list_of_integers

implicit none

! this module implements simple operations on a list of

! sorted integers

type :: element

integer :: value

type(element), pointer :: next

end type element

! note that type(element) is used in defining type element

! this is possible if a pointer to element is a component of

! type element

type(element), pointer :: list

public :: insert, delete, initialize, make_empty, print

private :: element, list

contains

subroutine insert(i)

! inserts integer i in list if not already present

integer, intent(in) :: i

type(element), pointer :: current, previous

nullify(previous)

current => list

do

if (.not. associated(current)) exit

if (current%value > i) exit

previous => current

current => current%next

end do

if (.not. associated(previous)) then

allocate(previous)

previous%value = i

previous%next => current

list => previous

elseif (previous%value < i) then

allocate(current)

current%value = i

current%next => previous%next

previous%next => current

endif

end subroutine insert

 

subroutine delete(i)

! deletes i from list (if present)

integer, intent(in) :: i

type(element), pointer :: previous

previous => list

if (associated(list)) then

if (list%value == i) then

list => list%next

deallocate(previous)

elseif (list%value < i) then

do

if (.not. associated(previous%next)) exit

if (previous%next%value < i) then

previous => previous%next

elseif (previous%next%value > i) then

exit

else

current => previous%next

previous%next => current%next

deallocate(current)

endif

end do

endif

endif

end subroutine delete

 

subroutine initialize()

nullify(list)

end subroutine initialize

 

subroutine make_empty()

type(element), pointer :: current, previous

current => list

do

if (.not. associated(current)) exit

previous => current

current => current%next

deallocate(previous)

end do

nullify(list)

end subroutine make_empty

 

subroutine print_list()

type(element), pointer :: current

current => list

do

if (.not. associated(current)) exit

print *, current%value

current => current%next

end do

end subroutine print_list

end module list_of_integers

Comments

•     add more functionality to list module

•     search for an integer in list

•     search for an integer satisfying a given property in the list – required property can be a function argument

•     possible to organize lists more efficiently

•     extend to several collections of lists

•     operations on sets like union, intersection can be defined – module can contain all of these

•     more complex patterns can be created than a single linear sequence

•     an element may point to more than one element and may be pointed to by more than one

•     leads to more complex structures like trees and graphs – example world wide web

•     useful in many applications and also as abstract objects – pointers allow efficient implementation