cs1001 lecture 26 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS1001 Lecture 26 PowerPoint Presentation
Download Presentation
CS1001 Lecture 26

Loading in 2 Seconds...

play fullscreen
1 / 15

CS1001 Lecture 26 - PowerPoint PPT Presentation


  • 157 Views
  • Uploaded on

CS1001 Lecture 26 . Static vs Dynamic Data Structures Array vs Link List Pointers Link List Operations Example. Static vs Dynamic. Static Data Structure -- structures whose sizes are fixed once memory is allocated, e.g., arrays type , DIMENSION ( l : u ) :: list_of _array_names

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS1001 Lecture 26' - esme


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs1001 lecture 26
CS1001 Lecture 26
  • Static vs Dynamic Data Structures
  • Array vs Link List
  • Pointers
  • Link List Operations
  • Example
static vs dynamic
Static vs Dynamic
  • Static Data Structure -- structures whose sizes are fixed once memory is allocated, e.g., arrays
    • type, DIMENSION (l:u) :: list_of _array_names
    • type, DIMENSION (:) , ALLOCATABLE :: list

ALLOCATE (list, STAT = status_variable)

DEALLOCATE (list, STAT = status_variable)

  • Dynamic Data Structure -- expands and contracts as needed during execution
    • collection of elements called nodes
    • e.g., Link list
arrays
Arrays
  • PROs
    • Used for list of data that can be naturally organized into tables
    • Easy and fast to access each element by indices
    • Easy and fast to operate on corresponding data points
  • CONs
    • Fixed size
    • Hard to shift elements

1, 3, 5, 8,10,…, 80, … to insert 2

1, 2, 3, 5, 8, 10, …, 80, ...

1, 2, 3, 5, 8, 10,…, 80, … to delete 3

1, 2, 5, 8, 10, …, 80, ...

link list
Link List
  • Link list -- collection of elements called nodes and each node consists of data and link (or pointer)

data

data

data

list

Brown ptr

CMU ptr

WPI null

adding to the linked list
Adding to the linked list

data next

Data next

data next

list

Brown

CMU

WPI null

data

new

Prev_ptr

RICE

data next

data next

data next

list

Brown

CMU

WPI null

data

Prev_ptr

new

RICE

data next

data next

data next

list

Brown

CMU

WPI null

data

Prev_ptr

new

RICE

delete a node from a linked list
Delete a node from a linked list

data next

data next

data next

list

Brown

CMU

WPI null

data

Prev_ptr

To_delete

RICE

data next

data next

data next

list

Brown

CMU

WPI null

data

To_delete

Prev_ptr

RICE

data next

data next

null

list

Brown

WPI null

To_delete

data

Prev_ptr

RICE

linked list elements
Linked List Elements
  • Pointer to the List
  • Node -- defined as a derived type

e.g., TYPE School_node

CHARACTER(8) :: Name

TYPE (School_node) POINTER :: next

END TYPE School_node

e.g., TYPE School

CHARACTER(8) :: Name

REAL :: Cost

END TYPE School

TYPE School_node1

TYPE School :: data

TYPE (School_node1) POINTER :: next

END TYPE School_node1

School_node

Name next

School_node1

data next

pointers
Pointers
  • Declaration

Type, attribute_list, POINTER :: pointer_variable

e.g., CHARACTER(8), POINTER :: StringPtr

TYPE (School_node1), POINTER :: NextPtr

  • ALLOCATE
    • Used to acquire memory locations to associate with the pointer variable during execution
    • e.g., ALLOCATE(StringPtr)

ALLOCATE (NextPtr)

Memory for data of type

School_node1

Name -- Character(8)

Cost -- Real

Next -- pointer to type

School_node1

StringPtr

Character(8)

NextPtr

pointer
Pointer
  • Pointer variables:
    • undefined
      • initially undefined
    • associated -- when a pointer points to a target
      • intrinsic function ASSOCIATED to test whether a pointer variable is associated with a target. E.g., ASSOCIATED (pointer) returns .TRUE. if association exists, returns .FALSE. otherwise.
    • null or disassociated
      • NULLIFY (list_of_pointers) change pointer variables to null
      • the association between that pointer and the target memory location is broken
      • DEALLOCATE statement is used to free the target memory
pointer assignment
Pointer Assignment
  • Form: pointer1 => pointer2

if pointer1 and pointer2 have the same type

Pointer1

Pointer2

Need another pointer ?

Pointer1 => Pointer2

Pointer1

Pointer2

ASSOCIATED ( Pointer1, Pointer2) = .TRUE. If pointer1 and pointer2

point to the same target. .FALSE. Otherwise.

pointers in expressions
Pointers in expressions

CHARACTER (8), POINTER :: School

TYPE School_node

CHARACTER(8) :: Name

TYPE (School_node) POINTER :: next

END TYPE School_node

TYPE (School_node), POINTER :: list

ALLOCATE (School)

ALLOCATE (list)

School = “Stanford”

Print *,School

School

CHARACTER(8)

list

CHARACTER(8)

Pointer to School_node

School

Stanford

pointers in expressions12
Pointers in expressions

CHARACTER (8), POINTER :: School1, School2

ALLOCATE (School1)

ALLOCATE (School2)

School1 = “Stanford”

School2 = “Cornell”

School1 = School2

School1 => School2

Stanford

School1

School2

Cornell

Cornell

School1

School2

Cornell

Stanford

School1

School2

Cornell

constructing a linked list
Constructing a Linked List

TYPE School_node

CHARACTER(8) :: Name

TYPE (School_node) POINTER :: next

END TYPE School_node

TYPE (School_node), POINTER :: list, temp

ALLOCATE (temp)

temp%Name = “Stanford”

NULLIFY(temp%next)

list => temp

ALLOCATE(temp)

temp%Name = “WPI”

NULLIFY(temp%next)

list%next => temp

CHARACTER(8)

Pointer to School_node

Stanford

Null

temp

list

list

Stanford

Null

WPI

Null

temp

temp

list

Stanford

WPI

Null

traversing a linked list
Traversing a linked list

Cornell

Null

WPI

list

Stanford

Curr_ptr

Curr_ptr => list

Curr_ptr => Curr_ptr%next

Curr_ptr => Curr_ptr%next

Curr_ptr => Curr_ptr%next

Curr_ptr

Curr_ptr

Curr_ptr is null -- end of list

Curr_ptr => list

DO

IF (.NOT. ASSOCIATED (Curr_ptr) EXIT

PRINT *, Curr_ptr%name

Curr_ptr => Curr_ptr%next

END DO

example
Example
  • Get a list of TCP/IP address from a file
  • Use linked list to keep track of how many times the addresses was used
  • traverse the list and display each TCP/IP address and its counts