-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.f90
More file actions
71 lines (54 loc) · 1.72 KB
/
stack.f90
File metadata and controls
71 lines (54 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module typeStack
implicit none
! Define the data-structure to hold the data
type :: stack
integer, allocatable :: data(:)
integer :: size = 0
contains
procedure :: pop => pop_fn
procedure :: push => push_sub
procedure :: peek => peek_fn
end type stack
! Set the size of allocated memory blocks
integer, parameter :: block_size = 10
public
private :: pop_fn, push_sub, peek_fn
contains
subroutine push_sub(this, e)
implicit none
class(stack) :: this
integer, intent(IN) :: e
integer, allocatable :: wk(:)
if(.not. allocated(this%data))then
! Allocate space if not yet done
allocate(this%data(block_size))
elseif(this%size == size(this%data))then
! Grow the allocated space
allocate(wk(size(this%data)+block_size))
wk(1:this%size) = this%data
call move_alloc(wk,this%data)
end if
! Store the data in the stack
this%size = this%size + 1
this%data(this%size) = e
end subroutine push_sub
integer function pop_fn(this)
implicit none
class(stack) :: this
if(this%size == 0 .or. .not. allocated(this%data))then
pop_fn = 0
return
end if
pop_fn = this%data(this%size)
this%size = this%size - 1
end function pop_fn
integer function peek_fn(this)
implicit none
class(stack) :: this
if (this%size == 0 .or. .not. allocated(this%data)) then
peek_fn = 0
return
end if
peek_fn = this%data(this%size)
end function peek_fn
end module typeStack