Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.

Similar presentations


Presentation on theme: "COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures."— Presentation transcript:

1 COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures

2 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

3 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

4

5 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

6

7 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 Node s, same entity objects

8

9

10

11 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

12 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

13

14

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

16

17

18 COSC 1P03 Data Structures and Abstraction 5.18 Traversal  sequential processing  to get to n th 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

19

20

21

22

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

24

25

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

27

28

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

30

31

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

33

34

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

36

37

38 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

39

40

41

42

43

44

45

46 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

47

48

49

50 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

51

52

53

54 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

55

56

57

58

59

60

61 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

62

63

64

65

66 COSC 1P03 Data Structures and Abstraction 5.66 Working with Linked 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

67


Download ppt "COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures."

Similar presentations


Ads by Google