1 / 15

CS1001 Lecture 26

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

esme
Download Presentation

CS1001 Lecture 26

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 (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

  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 Prev_ptr new RICE data next data next data next list Brown CMU WPI null data Prev_ptr new 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 Character(8) NextPtr

  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 => 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.

  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 => School2 Stanford School1 School2 Cornell Cornell School1 School2 Cornell Stanford School1 School2 Cornell

  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) 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

  14. 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

  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

More Related