Upload
margarita-wheadon
View
237
Download
2
Embed Size (px)
Citation preview
COSC 1P03
Data Structures and Abstraction 5.1
Linear Linked Structures
COSC 1P03
Data Structures and Abstraction 5.2
Dynamic Structures
· collections· limitations of static structures (i.e. arrays)
- fixed size° waste space
- rearrangement of entries· dynamic data structures
- change size over time- storage proportional to amount of information
· linked structures- nodes (objects) connected together by references- dynamic allocation/deallocation- in array proximity implicit, in linked structure it is
explicit- linear linked structures
° aka linked lists
COSC 1P03
Data Structures and Abstraction 5.3
Sequentially-linked Structures
· each node indicates its successor (via a reference)· first node located via reference· last node has no successor (null)· each node represents one entity· empty collection has null reference
list
Figure 15.1 Sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.5
Representation
· dynamic creation- nodes are objects
· let entity objects be nodes?- add reference field- disadvantages
° object must “know” it is on list° multiple lists° must modify class
public class Student { : private Student nextStudent; // next student in class : } // Student
COSC 1P03
Data Structures and Abstraction 5.7
Node Wrapper Class
· separate class that references entities- wrapper class, mixin, decorator (GoF)
· fields- reference (p.theStudent)- link (p.next)
· constructor· visibility
- class- fields
· as sequentially-linked structure- general case- initial (empty) state
· multiple lists- different sequence of Nodes, same entity objects
class Node { public Student theStudent; // the wrapped student public Node next; // next node in sequence public Node ( Student s, Node n ) { theStudent = s; next = n; }; // constructor } // Node
Figure 15.2 Example—Node wrapper class
Student
Student
Student
Student
list
Figure 15.3 Sequentially-linked structure with wrapper class
list Figure 15.4 Sequentially-linked structure: initial state (empty)
COSC 1P03
Data Structures and Abstraction 5.11
Operations
· insertion- where?
· deletion- which node?
· traversal- “visit” all nodes- like array traversal
· search- special traversal- simple vs exhaustive
COSC 1P03
Data Structures and Abstraction 5.12
Insertion
· insert where?- front- end- in some order (e.g. sorted)
· at front- new entry points to previous front- list reference points to new entry
· algorithm- O(1)
· repeated application- reverse order
list
list = new Node(entity,list); Figure 15.5 Insertion at front of sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.15
Deletion
· delete which node?- first- last- matching a key
· only if not empty· delete first
- move list pointer to second node· garbage collection
- node- entity
· algorithm- O(1)
list
if ( list == null ) { throw new NoItemException(); } else { entity = list.contents; list = list.next; };
Figure 15.7 Deletion at front of sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.18
Traversal
· sequential processing- to get to nth node must first get to (n-1)st node
· travelling pointer- start at first node- move to node's successor
° p = p.next- termination
° no more nodes (p == null)· algorithm
- O(n)· vs array traversal
- sequential traversal pattern
list
p = list; while ( p != null ) { process p.contents; p = p.next; }; Figure 15.9 Traversal of sequentially-linked structure
for ( int i=0 ; i<a.length ; i++ ) { process a[i] };
p = list; i = 0; while ( p != null ) { while ( i < a.length ) { process p.contents; process a[i] p = p.next; i = i + 1; }; };
Figure 15.10 Comparison of traversal algorithms
start at first entry; while ( more entries ) { process entry; on to next entry; }; Figure 15.11 Sequential traversal programming pattern
COSC 1P03
Data Structures and Abstraction 5.23
Search
· sequential structure- sequential search
· variant of traversal- two exit conditions
° found° not found
· algorithm- O(n)
list
p = list; for ( ; ; ) { if ( p == null ) { not found; break; }; if ( search key matches key in p.contents ) { found; break; }; p = p.next; };
Figure 15.12 Search of sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.26
Insertion at End of List
· for insertion in order· find end of list
- traversal- 2 travelling pointers
° initial state×q is predecessor of p
- insert· algorithm
- traverse° updating p & q
- insert° 2 cases
×q == null (empty list)×q != null (at end)
· O(n)
list
q = null; p = list; while ( p != null ) { q = p; p = p.next; }; if ( q == null ) { list = new Node(entity,null); } else { q.next = new Node(entity,null); }; Figure 15.13 Insertion at end of sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.29
Deletion of Last Node
· must modify second last node· find second last node
- traversal- 2 travelling pointers- test
· algorithm- pre test
° error- traverse- delete
° 2 cases×q == null (only node)×q != null (last node)
· O(n)
list
if ( list == null ) { throw new NoItemException; } else { q = null; p = list; while ( p.next != null ) { q = p; p = p.next; }; entity = p.contents; if ( q == null ) { list = null; } else { q.next = null; }; }; Figure 15.15 Deletion at end of sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.32
Insertion in Sorted Order
· insert between 2 nodes- criteria
· find insertion point- search- 2 travelling pointers
° insert between p & q- combination of termination conditions (order)
· algorithm- search
° first entity with greater key° termination on found or end of list
- insert° 2 cases
×q == null (front of list or empty list)×q != null (after q)
· O(n)
list
q = null; p = list; while ( p != null && insertion key is greater or equal to key of p.contents ) { q = p; p = p.next; }; if ( q == null ) { list = new Node(entity,p); } else { q.next = new Node(entity,p); };
Figure 15.17 Sorted insertion into a sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.35
Deletion of Node by Key
· criterion- match key
· find deletion point- search- 2 travelling pointers- termination and subsequent test- exception
· algorithm- search- delete if found
° 2 cases×q == null (delete first node)×q != null (delete q’s successor)
· O(n)
list
q = null; p = list; while ( p != null && deletion key not equal to key of p.contents ) { q = p; p = p.next; }; if ( p == null ) { throw new NotFoundException; } else { entity = p.contents; if ( q == null ) { list = p.next; } else { q.next = p.next; }; };
Figure 15.19 Keyed deletion from a sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.38
Insertion at End with Tail reference
· O(n) since must find end- remember where end is then O(1)
· pair of pointers (head, tail)- must be sure to update both appropriately- insertion at end
° empty list insertion- deletion at front
° last node deletion· performance
- extra cost is O(1)· deletion of last in O(1)?
- not possible
head tail
Figure 15.21 Sequentially-linked structure with tail reference
head tail
if ( tail == null ) { head = new Node(entity,null); tail = head; } else { tail.next = new Node(entity,null); tail = tail.next; }; Figure 15.22 Example—Insertion at end of sequentially-linked structure
with tail reference
head tail
head tail
if ( head == null ) { throw new EmptyStructureException(); } else { entity = head.contents; head = head.next; if ( head == null ) { tail = null; }; };
Figure 15.24 Example—Deletion at front of sequentially-linked structure with tail reference
head tail
COSC 1P03
Data Structures and Abstraction 5.46
Symmetrically-linked
Structures
· each node points to successor and predecessor· traversal in both directions· arbitrary insertion/deletion· extra work in insertion and deletion (extra pointers)
- e.g. sorted insertion- e.g. keyed deletion
class Node { public Node prev; // previous node in sequence public Student theStudent; // the wrapped student public Node next; // next node in sequence public Node ( Node p, Student s, Node n ) { prev = p; theStudent = s; next = n; }; // constructor } // Node
Figure 15.25 Example—Symmetrically-linked Node wrapper class
list
Figure 15.26 Symmetrically-linked structure
q = null; p = list; while ( p != null && insertion key is greater or equal to key of p.contents ) { q = p; p = p.next; }; if ( q == null ) { list = new Node(null,entity,p); } else { q.next = new Node(q,entity,p); if ( p != null ) { p.prev = q.next; } };
Figure 15.27 Example—Sorted insertion into a symmetrically-linked structure
list q p
p = list; while ( p != null && key of p.contents not equal to deletion key ) { p = p.next; }; if ( p == null ) { throw new NotFoundException; } else { entity = p.contents; if ( p.prev == null ) { list = p.next; } else { p.prev.next = p.next; }; if ( p.next != null ) { p.next.prev = p.prev; }; };
Figure 15.29 Example—Keyed deletion from a symmetrically-linked structure
list p
COSC 1P03
Data Structures and Abstraction 5.50
Circular Linked Structures
· sequentially-linked or symmetrically-linked· last node points back to first
- single node· traversal from any point· treating each node as head in turn· potential infinite loop in traversal
list
Figure 15.31 Circular sequentially-linked structure
list
Figure 15.32 Circular symmetrically-linked structure
list list
Figure 15.33 Single node circular structures
if ( list != null ) { p = list; do { process p.contents; p = p.next; } while ( p != list ); };
Figure 15.34 Example—Traversal of a circular sequentially-linked structure
COSC 1P03
Data Structures and Abstraction 5.54
Header/Sentinel Nodes
· operations at front cause special cases· header node
- extra node at front of list- never deleted- doesn’t contain data
· empty list· traversal
- from successor of header· e.g. insertion in sorted order· e.g. keyed deletion· sentinel node
- mark end of list- high key value stops search
· cost
list
Figure 15.35 Sequentially-linked structure with header node
list
Figure 15.36 Symmetrically-linked structure with header node
list list
Figure 15.37 Empty linear linked structures with header nodes
list = new Node(null,null); list = new Node(null,null,null);
Figure 15.38 Example—Initial (empty) states of linear linked structures with header nodes
list
q = list; p = list.next; while ( p != null && insertion key is greater or equal to key of p.contents ) { q = p; p = p.next; }; q.next = new Node(entity,p); Figure 15.39 Example—Sorted insertion into a sequentially-linked structure with
header node
list
q = list; p = list.next; while ( p != null && deletion key not equal to key of p.contents ) { q = p; p = p.next; }; if ( p == null ) { throw new NotFoundException; } else { entity = p.contents; q.next = p.next; };
Figure 15.40 Example—Keyed deletion from a sequentially-linked structure with header node
COSC 1P03
Data Structures and Abstraction 5.61
Lists-of-lists and Multi-lists
· lists whose entries are lists- first nodes differ- SubList wrapper
· multi-lists- nodes on multiple lists in a lattice- access in various dimensions- e.g. sparse matrices
list
Figure 15.42 List of lists structure
class SubList { public SubList nextList; public SomeType content; public Node head; public SubList ( SubList n, Sometype c, Node l) { nextList = n; content = c; head = l; }; // constructor } // SubList
Figure 15.43 Example—Sub-list node structure
0 0 0 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 4 0 0 0 0
Figure 11.44 Sparse matrix
0 row
0
2
3
4
1 3 col
4 0 4
2 1 2
0 3 1
3 3 3
Figure 15.44 Sparse array as a multi-list
COSC 1P03
Data Structures and Abstraction 5.66
Working withLinked
Structures· encapsulating algorithms into methods
- passing list reference as parameter° parameters are by-value?
- return new list pointer as function result° other result?
- use header node° creation of header?
- use instance variable instead of parameter° multiple lists?
- encapsulate as list object° list pointer is instance variable° list object can be passed as parameter
· comparison of array and linked representations
operation array sequentially-linked structure
insertion at front O(n) O(1) insertion at rear O(1) O(1) sorted insertion O(n) O(n) deletion at front O(n) O(1) deletion at rear O(1) O(n) keyed deletion O(n) O(n) traversal O(n) O(n) search (unsorted) O(n) O(n) search (sorted) O(lg n) O(n)
Table 15.1 Comparison of array and sequentially-linked structure algorithms