CHAP16 - Chapter 16 Linked Structures Dale/Weems/Headington...

Info iconThis preview shows pages 1–19. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 16 Linked Structures Dale/Weems/Headington
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Chapter 16 Topics Meaning of a Linked List Meaning of a Dynamic Linked List Traversal, Insertion and Deletion of Elements in a Dynamic Linked List Specification of a Dynamic Linked Sorted List Insertion and Deletion of Elements in a Dynamic Linked Sorted List
Background image of page 2
3 What is a List? A list is a varying-length, linear collection of homogeneous elements. linear means each list element (except the first) has a unique predecessor , and each element (except the last) has a unique successor
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 To implement the List ADT The programmer must 1) choose a concrete data representation for the list, and 2) implement the list operations
Background image of page 4
Array-Based Sorted List Operations Transformers Insert Delete Observers IsEmpty IsFull Length IsPresent Print change state observe state 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Array-based class SortedList IsFull Length IsPresent Delete IsEmpty Insert Print Private data: length data [ 0 ] [ 1 ] [ 2 ] [MAX_LENGTH-1] SortedList
Background image of page 6
7 // SPECIFICATION FILE ARRAY-BASED SORTED LIST ( slist.h ) const int MAX_LENGTH = 50 ; typedef int ItemType ; class SortedList { public : // public member functions SortedList ( ) ; // constructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int Length ( ) const ; // returns length of list void Insert ( ItemType item ) ; void Delete ( ItemType item ) ; bool IsPresent( ItemType item ) const ; void Print ( ) ; private : // private data members int length ; // number of values currently stored ItemType data[MAX_LENGTH] ; void BinSearch ( ItemType item, bool& found, int& position ) const ; 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 How to Implement a List use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed use a linked list (to avoid excessive data movement from insertions and deletions) not necessarily stored in contiguous memory locations
Background image of page 8
9 Implementation Possibilities for a List ADT List Linked list Built-in array Built-in dynamic data and pointers Built-in array of structs
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 A Linked List a linked list is a list in which the order of the components is determined by an explicit link member in each node the nodes are struct s--each node contains a component member and also a link member that gives the location of the next node in the list head ‘X’ ‘C’ ‘L’
Background image of page 10
11 Dynamic Linked List head “Ted” “Irv” “Lee” in a dynamic linked list, nodes are linked together by pointers, and an external pointer (or head pointer) points to the first node in the list
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Nodes can be located anywhere in memory the link member holds the memory address of the next node in the list head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000
Background image of page 12
13 // Type DECLARATIONS struct NodeType { char info; NodeType* link; } typedef NodeType* NodePtr; // Variable DECLARATIONS NodePtr head; NodePtr ptr; 13 Declarations for a Dynamic Linked List . info . link ‘A’ 6000
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 14 Pointer Dereferencing and Member Selection . info . link ‘A’ 6000 ptr ptr ptr . info . link ‘A’ 6000 *ptr ptr . info . link (*ptr).info ptr->info ‘A’ 6000
Background image of page 14
1515 ptr is a pointer to a node . info . link ‘A’ 6000 ptr ptr
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1616 *ptr is the entire node pointed to by ptr ptr . info . link ‘A’ 6000 *ptr
Background image of page 16