CS1001%20Lecture%2026 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001%20Lecture%2026

Description:

Static Data Structure -- structures whose sizes are fixed once memory is ... ALLOCATE (list, STAT = status_variable) DEALLOCATE (list, STAT = status_variable) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 16
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:
Tags: 20lecture | cs1001 | stat

less

Transcript and Presenter's Notes

Title: CS1001%20Lecture%2026


1
CS1001 Lecture 26
  • Static vs Dynamic Data Structures
  • Array vs Link List
  • Pointers
  • Link List Operations
  • Example

2
Static vs Dynamic
  • Static Data Structure -- structures whose sizes
    are fixed once memory is allocated, e.g., arrays
  • type, DIMENSION (lu) 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

3
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, ...
4
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
5
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
new
Prev_ptr
RICE
data next
data next
data next
list
Brown
CMU
WPI null
data
new
Prev_ptr
RICE
6
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
7
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
8
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
NextPtr
Character(8)
9
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

10
Pointer Assignment
  • Form pointer1 gt pointer2
  • if pointer1 and pointer2 have the same type

Pointer1 Pointer2
Need another pointer ?
Pointer1 gt Pointer2
Pointer1 Pointer2
ASSOCIATED ( Pointer1, Pointer2) .TRUE. If
pointer1 and pointer2 point to the same
target. .FALSE. Otherwise.
11
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
12
Pointers in expressions
  • CHARACTER (8), POINTER School1, School2
  • ALLOCATE (School1)
  • ALLOCATE (School2)
  • School1 Stanford
  • School2 Cornell
  • School1 School2
  • School1 gt School2

Stanford
School1
Cornell
School2
Cornell
School1
Cornell
School2
Stanford
School1
Cornell
School2
13
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)
  • tempName Stanford
  • NULLIFY(tempnext)
  • list gt temp
  • ALLOCATE(temp)
  • tempName WPI
  • NULLIFY(tempnext)
  • listnext gt temp

CHARACTER(8) Pointer to School_node
Stanford Null
temp
list
list
Stanford Null
WPI Null
temp
temp
list
Stanford
WPI Null
14
Traversing a linked list
WPI
Cornell Null
list
Stanford
Curr_ptr
Curr_ptr gt list Curr_ptr gt Curr_ptrnext Curr_pt
r gt Curr_ptrnext Curr_ptr gt Curr_ptrnext
Curr_ptr
Curr_ptr
Curr_ptr is null -- end of list
Curr_ptr gt list DO IF (.NOT. ASSOCIATED
(Curr_ptr) EXIT PRINT , Curr_ptrname
Curr_ptr gt Curr_ptrnext END DO
15
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
Write a Comment
User Comments (0)
About PowerShow.com