Title: Linked Data Structures
1Topic 4
2Chapter Objectives
- describe the use of references to create linked
structures - compare linked structures to array-based
structures - explore the techniques for managing a linked list
- discuss the need for a separate node to form
linked structures - implement a bag collection using a linked list
3Array Limitations
- what are the limitations of an array, as a data
structure? - fixed size
- physically stored in consecutive memory locations
- to insert or delete items, need to shift data
4Linked Data Structures
- a linked data structure consists of items that
are linked to other items - how? each item points to another item
- singly linked list each item points to the next
item - doubly linked list each item points to the next
item and to the previous item - a linked structure is a dynamic data structure
during execution, its size grows and shrinks as
needed
5 Singly Linked List
6Advantages of Linked Lists
- the items do not have to be stored in consecutive
memory locations the successor can be anywhere
physically - so, can insert and delete items without shifting
data - can increase the size of the data structure
easily - can grow dynamically (at run time) the amount
of memory space allocated can grow and shrink as
needed
7Singly Linked List
- a linked list is an ordered sequence of items
called nodes - a node is the basic unit of representation in a
linked list - a node in a singly linked list consists of two
fields - a data portion
- a reference (link, pointer) to the next node in
the structure - the first item is accessed via a front or head
pointer
8Diagram
- Of singly linked list with nodes
9Singly Linked List (contd)
- note we will henceforth refer to a singly linked
list just as a linked list - traversing the linked list
- how is the second item accessed?
- the third?
- the last?
- what does the last item point to?
- we call this the null link
10Discussion
- How do we get to an items successor?
- How do we get to an items predecessor?
- How do we access the, say, 5th item in the linked
list? - How does this differ from an array?
11Linked List Operations
- add an item to the list
- insert a node at the front
- insert a node in the middle
- insert a node at the end
- delete an item from the list
- delete the node at the front
- delete an interior node
- delete the last node
- what management issues arise when performing
these operations on a linked list? - note the use of the pointers to the current and
previous nodes
12Inserting a Node at the Front
13Inserting a Node in the Middle
14Deleting the First Node
15Deleting an Interior Node
16References As Links
- recall that in Java, a reference variable stores
the address of an object, i.e. a reference is a
pointer to an object - a linked structure uses reference variables to
link one object to another
17An object reference variable pointing to an object
18Linked List Implementation
- in Java, a linked list is a list of node objects,
each of which consists of two references - a reference to the data object
- a reference to the next node
- the head pointer is the reference to the linked
list (i.e. to the first node in the linked list) - the last node has the null value as the reference
to the next node
19Linked List of Node Objects
20Doubly Linked Lists
- in a doubly linked list, each node has two links
- a reference to the next node in the list
- a reference to the previous node in the list
- what is the advantage of a doubly linked list?
- what is a disadvantage?
21 Doubly Linked List
22Another Bag Implementation
- we will now explore a linked implementation of a
bag collection - it will implement the same interface (BagADT) as
the array-based implementation only the
underlying structure changes
23The LinkedBag Class
- the elements of the bag are stored in nodes of
the linked list - variables needed
- count a count of the current number of elements
in the bag - contents a reference to the linked list (i.e. to
its first node) - the nodes of the linked list are defined by the
LinearNode class (why is this a good idea?)
24The LinearNode Class
- note that it is called LinearNode to avoid
confusion with a different class that will define
nodes for non-linear structures - see LinearNode.java
- what are the attributes?
- what do the constructors do?
- what will the methods be used for?
- can this class be used other than for the linked
implementation of the Bag collection?
25Linked Implementation of Bag Collection
26The LinkedBag Class
- note that it is called LinkedBag.java only to
differentiate it for us from the array
implementation, ArrayBag.java - how do we know what methods need to be in the
LinkedBag class?
27UML description of the LinkedBag class
28LinkedBag Constructor
//------------------------------------------------
----------------- // Creates an empty
bag. //-------------------------------------------
---------------------- public LinkedBag()
count 0 contents null
29The add Operation
//------------------------------------------------
----------------- // Adds the specified element
to the bag. //------------------------------------
----------------------------- public void add
(Object element) LinearNode node new
LinearNode (element) node.setNext(contents)
contents node count
30The removeRandom Operation
//------------------------------------------------
----------------- // Removes a random element
from the bag and returns it. Throws // an
EmptyBagException if the bag is
empty. //-----------------------------------------
------------------------ public Object
removeRandom() throws EmptyBagException
LinearNode previous, current Object result
null if (isEmpty()) throw new
EmptyBagException() int choice
rand.nextInt(count) 1 if (choice 1)
result contents.getElement()
contents contents.getNext()
31removeRandom contd
else previous contents for
(int skip2 skip lt choice skip)
previous previous.getNext() current
previous.getNext() result
current.getElement() previous.setNext(curre
nt.getNext()) count-- return
result
32The remove Operation
//------------------------------------------------
----------------- // Removes one occurrence of
the specified element from the bag // and
returns it. Throws an EmptyBagException if the
bag is // empty and a NoSuchElemetnException if
the target is not in // the bag. //--------------
--------------------------------------------------
- public Object remove (Object target) throws
EmptyBagException,
NoSuchElementException boolean
found false LinearNode previous, current
Object result null if (isEmpty())
throw new EmptyBagException() if
(contents.getElement().equals(target))
result contents.getElement() contents
contents.getNext()
33remove contd
else previous contents
current contents.getNext() for (int
look0 look lt count !found look)
if (current.getElement().equals(target)
found true else
previous current current
current.getNext() if (!found)
throw new NoSuchElementException()
result current.getElement()
previous.setNext(current.getNext())
count-- return result
34The iterator Operation
//------------------------------------------------
----------------- // Returns an iterator for the
elements currently in this bag. //----------------
-------------------------------------------------
public Iterator iterator() return new
LinkedIterator (contents, count)
See LinkedIterator.java
35Exercise
- write the code for the operations
- size
- isEmpty
- addAll
- union
- contains
- equals
- toString
36Analysis of Linked Operations
- adding an element to the bag
- why were we able to always add a new element at
the front of the linked list? - are the steps involved in adding a new element
dependent on the number of elements in the bag? - so, the time complexity for adding an element is
O(1) - removing a particular element is O(n) why?
- removing a random element is O(n) why?
37Array vs Linked List
- have seen advantages of a linked list
- any disadvantages?