Download presentation
Presentation is loading. Please wait.
Published byErick Parks Modified over 9 years ago
1
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it back when it begins to rain. Robert Frost (1874-1963)
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 sharing between structures? 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
COSC 1P03 Data Structures and Abstraction 5.4 Representation nodes are objects dynamic creation let entity objects be nodes? add reference field disadvantages object must “know” it is on list multiple lists must modify class
5
COSC 1P03 Data Structures and Abstraction 5.5 Node Wrapper Class separate class references objects wrapper class, mixin 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 objects
6
COSC 1P03 Data Structures and Abstraction 5.6 Operations insertion where? deletion which node? traversal “visit” all nodes like array traversal search special traversal simple vs exhaustive
7
COSC 1P03 Data Structures and Abstraction 5.7 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 pattern O(1) repeated application reverse order
8
COSC 1P03 Data Structures and Abstraction 5.8 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 pattern O(1)
9
COSC 1P03 Data Structures and Abstraction 5.9 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 ) pattern O(n) vs array traversal sequential traversal pattern
10
COSC 1P03 Data Structures and Abstraction 5.10 Search search key two outcomes success failure variant of traversal two exits end of list found item pattern O(n)
11
COSC 1P03 Data Structures and Abstraction 5.11 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 pattern traverse updating p & q insert 2 cases q == null (empty list) q != null (at end) O(n)
12
COSC 1P03 Data Structures and Abstraction 5.12 Deletion of Last Node must modify second last node find second last node traversal 2 travelling pointers test pattern pre test error traverse delete 2 cases q == null (only node) q != null (last node) O(n)
15
Student Object The Student object pointer, Points to a object of type student Points to the next node in the list. Constructor, creates the node assigning the data and where the node is to point. No Class modifier, package-private, can be seen by all members within the same package. But not outside. should be omitted, Package Private or Protected
20
COSC 1P03 Data Structures and Abstraction 5.20 Insert at Front class Node { public StudenttheStudent; public Nodenext; public Node ( Student s, Node n ) { theStudent = s; next = n; };// constructor }// Node Assume Students A B C D are to be entered. A BCD List is initially empty List Constructor is called and a new node is created List Student A is assigned to the Data Area Next will point to null since the list is initially null.Once the constructor returns the object is assigned to ListThis process will repeat until there is no more data to enter
23
COSC 1P03 Data Structures and Abstraction 5.23 Deletion from front if ( list == null ) { throw new EmptyStructureException(); } else { entity = list.contents; list = list.next; }; A B C D entity List entity List entity List entity List If the list is not empty, set a pointer to the nodes contents Move the head of the list to the next node by following nextThe entity is return to the caller as data Since nothing is pointing to the node, the garbage collector reclaims the memory. This process can be repeated until the list is exhausted. A reference to an empty list should throw an exception, else crash!
24
List A BCD Initialize pointer p to the beginning of the list p Check to see if p != null i.e. the end of the list, it is not so continue.Process contents, E.g. Update something in the object pointed to by p.contents p Increment the pointer p, by following the next pointer Go back to the start of the loop and continue the process ppp p.next = null, So following p.next means p is now null. P!=null is now true, so we exit the algorithm Traversal of a List
25
List TraversalArray Traversal By comparison, these two algorithms are the same, the difference lies in the data structure type. Initialize the index to the first element of the array. Initialize a pointer to the first node of the list Stopping condition is the length of the array Stopping condition is the end of the list, or null pointer. Increment the array index. Increment the pointer
27
Searching a list List A BCD A search is a traversal with 1 extra condition. Assume we are searching for "E" p Set p to the beginning of the list. Start traversing the listCheck is p.contents = 'E', No Advance the pointer p and continue to loop p P.contents != 'E' p p p P== null, 'E' not in list Lets now assume our search criteria is 'B' p P.contents != 'B' p p P.contents == 'B' FOUND!!!!! The algorithm exits.At this point we would process the node pointed to by p
29
Insertion at the End Case 1 List The list is empty During initialization, q = null, p = null because list points to nullWhile loop is skipped since p == nullSince q has never been updated q == null, hence list is null i.e. empty This is the same as inserting at the front so call the constructor A
30
Insertion at the End Case 2 List A BCD We have a none empty list, and want to insert 'E' at the end. p q Pointers p and q are initialized While p != null we walk the pointers down the list. q always trailing p q p q p qpqp We found the end of the list, p == null so loop exits q != null, it is pointing to the last node, so do the else part Constructor is called to create a new node with 'E', it is linked to q.next E A
32
Deletion from the End List A BCD q p q p qp p q Initialize p and q as before. Walk down the list, except this time stop when p.next == nullP now points to the last node, the contents of the entity is removed.If q is null then there was only 1 node in the list.Setting the list = null effectively would remove the node. Otherwise, there is at least 2 nodes in the list, q pointing to the second lastSetting q.next == null will remove the last node. B
33
COSC 1P03 Data Structures and Abstraction 5.33 The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.