Program simple

Implicit None

TYPE :: int_list  ! a list of integers
   Integer :: value
   TYPE (int_list), POINTER :: p
END TYPE
TYPE (int_list), POINTER :: head, current, tail ! only pointers
Integer :: i, num, status

! create the first cell and put 1 in it.
allocate(head, stat=status) ! creates a "blank" cell pointed to by head
if (status /= 0) stop  ! allocation has failed. no space available
Nullify(head%p) ! ensures first cell does not point to arbitrary location
head%value = 1  ! now we have  head ---> <1,null>

tail => head  ! now tail also points to same list <1,null>

! add more elements to the list (ask user howmany)
write (*,10,ADVANCE='no') 'How many elements to add?-> '
10 Format(A)
read (*,*) num
do i = 2, num
  allocate(tail%p, stat=status) ! create one more cell
  if (status /= 0) stop  ! allocation has failed. no space available
  tail => tail%p  ! make tail point to new cell
  Nullify(tail%p) ! ensures new cell's pointer is dissociated (not valid)
  tail%value = i  ! add i as new cell's value
end do

! now show the list on the screen.
write (*,*) 'The list created is: '
current => head  ! make current point to the first element of list
do
   write(*,11, ADVANCE='no') current%value, ' '
   11 Format(I3,A)
   current => current%p  ! advance the pointer
   if (.not. associated(current)) exit  !we have reached the end
   end do
write (*,*) ' '
End Program simple
