Title: Ways to Organize a Collection of Like Items
1Ways to Organize a Collection of Like Items
Set
2ADT List
Data A finite sequence of data items.
Each item occupies a position in the List.
Positions are from 0 to number of items
1. Operations Construction Create an
empty list Empty Check if list
is empty Insert Add an item at
any position in the list Delete
Remove an item from the list at any
position in the list
Traverse Go through the list accessing
and processing the
elements in order
3Possible List Data Structures
- Array
- capacity determined at compile-time
- capacity determined at run-time
- good choice if
- capacity known before list is constructed
- insertions/deletions mainly at the end
- Linked List
- capacity grows and shrinks as size changes
- insertions/deletions do not require shifting
- access to an item by position is not efficient
4Linked Lists
- another use of pointer variables
- and the heap
5a linked list consists of nodes
- each item is stored in a node
- space is dynamically allocated (and returned) one
node at a time - items are not stored in contiguous memory
locations
6a linked list node
- each node contains a data item and a pointer to
the next node - the last node has no next node
- an external pointer gives access to the first node
data next
7accessing nodes in a linked list
first
- no direct access to individual nodes
- first node accessed via first
- other nodes accessed via pointer in previous node
- is a sequential access data structure
- an array is a direct access data structure
8a node class
typedef _______DataType class Node
public DataType data Node
next Node( ) next
NULL Node(const DataType
item, Node nextNode NULL)
data item next
nextNode
9Evaluate
first first (first).data first -gt data first -gt
next -gt data temp -gt next NULL temp -gt
data temp -gt next new Node('Z')
10Basic operations
- Constructing an empty linked list
- Adding an item at some position
- position gt 0 position lt listSize
- Removing the item at some position
- position gt 0 position lt listSize
- Traversing a linked list
- "process" each item stored in the linked list
11An empty linked list
- Has no items so there are no nodes
- External pointer to first item has the value NULL
first
12Adding an item
- three steps
- allocate space for a node (using new)
- store the data item in that node
- link the new node into the existing linked list
- two cases for linking the node into the existing
linked list - adding an item at position 0
- adding an item at any other position, including
at the end
13Inserting at position 0
// allocate space for a node store item in
it Node addedNode new Node (item) // make
the new node the first node of the list addedNode
-gt next first first addedNode
14Inserting after a node
// allocate space for a node store item in
it Node addedNode new Node (item) // link
the node into the existing list addedNode -gt
next temp -gt next temp -gt next addedNode
15Removing an item
first
- Requires a pointer to the node BEFORE the node to
be deleted - change value stored in prev-gtnext
- return node containing element being deleted to
the heap - deleting first node is a special case
- first must be changed
16Removing a node
Node del prev -gt next prev -gt next del -gt
next delete del
17Removing the first node
Node del prev -gt next prev -gt next del
-gt next delete del
18A traversal loop
19Dealing with "process"
- Code in a container class method should not be
specific to the type of items contained - Client/user of a container class knows what
"process" means - Container class knows how to traverse
- Three solutions
- container class provides a method to return the
item at a specified position - container class supplies iterators to the
client/user - client/user passes a "process" function to the
container's traverse method
20Method to access an item
/ --- getAt retrieves the item at a specified
position in a LinkedList Receive
pos the position of the item to be retrieved
Precondition pos is between 0 and the number
of elements - 1 Returns the item at
postion pos / DataType getAt (int pos) const
21a LinkedList class
What data members? Node first int
size
22LinkedList class uses the heap
- heap space is allocated node by node
- new statement appears in the insert method
- delete statement appears in the remove method
- class needs
- destructor
- loop to delete node by node
- copy constructor
- loop to allocate new nodes and copy items
- operator
- combines the job of destructor and copy
constructor - private destroyList method
- private copyList method
23linked list as Stack and Queue data structure
24More on Linked Lists
- header nodes
- doubly linked lists
- the STL list class
25Using a header node
- inserting or removing a node at the beginning of
a linked list is a "special case" - requires changing the data member first
- first new Node(item)
- first del -gt next
- special case avoided by using a "dummy" header
node - gives first node a "predecessor"
26Doubly linked lists
- singly linked lists make finding the successor of
an item easy - temp temp-gtnext // equivalent of i
- how about finding the predecessor?
- no equivalent of i--
- need loop to traverse from first node to the
predecessor of the current node - Node pred headwhile (pred-gtnext !
current) pred pred-gtnext - doubly linked lists make finding successor and
finding predecessor equally efficient
27A doubly linked list