The List, Stack, and Queue Adts Abstract Data Type (Adt)
The List, Stack, and Queue Adts Abstract Data Type (Adt)
remove insert
Programming Details in Linked List Implementation:
• Use three classes for the List ADT:
(1) ListNode (for the individual nodes, or objects, in
the list);
(2) LinkedList (for the list object itself consisting of
the nodes); and (3)
LinkedListItr (for the current location in a
LinkedList object, in a particular application).
• Each class consists of one or more constructor functions.
• The ListNode class contains two member variables:
element for the “data” field and next for the link field lining
to the next node in the list.
Linked List Implementation (cont’d):
• The LinkedListItr class contains functions isPastEnd,
retrieve, advance; and contains a member variable current
for the current location in a LinkedList object.
• The LinkedList class may use a dummy header node to be
the first node in the list which somewhat simplifies the insert
function (thus, an “empty” list contains the header node as
the only node in the list). The functions included in the class
are find, remove, findPrevious, and insert.
Variations of linked list implementations include
• doubly linked lists – each node uses two link fields to link
to the two neighboring nodes in the list, respectively; and
• circular linked lists – the last node in the list is linked to
the front of the list, eliminating the header node.
Two Applications of Linked Lists:
(1) Polynomials (in a single variable x) – use nodes to
represent the non-zero terms arranged in decreasing order
of the exponents; typical operations include: constructor
(initialize the zero polynomial in constant time); add (in
O(m + n) time for two polynomials of m and n terms,
respectively); and multiply (in O(mn(min(m,n)) time for
two polynomials of m and n terms, respectively).
(2) Radix sort – sort n numbers in the range of [0..m–1],
where m bp for some constant p (of moderate size),
based on a multi-pass bucket sort technique, resulting in
O(p(n + b)) time using O(b+ n) space; this is a linear time
O(n) algorithm when p is a constant and b = O(n). For
example, m = 232 for 32-bit unsigned integers. Choose b =
211 = 2048, then m b3 and n numbers can be sorted in
O(3(n + 211) = O(n) time.
Algorithm for adding two polynomials in linked lists p, q:
set p, q to point to the two first nodes (no headers)
initialize a linked list r for a zero polynomial while
p != null and q != null if p.exp >
q.exp create a node
storing p.coeff and p.exp, then insert at the
end of list r; advance p else if q.exp >
p.exp create a node storing
q.coeff and q.exp, then insert at the end of
list r; advance q else if p.exp == q.exp
if p.coeff + q.coeff !
=0 create a node storing
p.coeff + q.coeff and p.exp, then insert at the
end of list r advance p, q
if p != null copy the remaining terms of p to end of
r else if q != null copy the remaining terms of q to end of r
Each iteration of the while loop takes O(1) time, moving at least
one of the two pointers p and q one position over. Any remaining
terms will be copied after the loop. Thus the total time is O(m+n)
because there are a total of m+n terms in the two polynomials.
Algorithm for multiplying two polynomials in linked lists p, q:
set p to the first node (no header) of one polynomial
initialize a linked list r for a zero polynomial
while p != null
copy list q to a new list t
multiply the term pointed by p to each term of t by
set t.exp += p.exp
set t.coeff *= p.coeff
add polynomial t to polynomial r (then discard t)
advance p
If polynomial p, q have m, n terms, respectively, the while loop
runs m iterations. In each iteration, producing the polynomial t
(copy and multiply) costs O(n) time since the length of t is O(n).
Adding polynomial t to r runs in time O(n + length of r). Since
the length of r in the beginningm of iteration k is O((k –1)n), so the
total time of the algorithm is O(k1(n (k 1)n)) O(m 2n). The choice of m
and n is arbitrary, so we can achieve O(mn(min(m,n)) time.
An Example demonstrating Radix Sort:
Given n = 10 numbers: 64, 8, 216, 512, 27, 729, 0, 1, 343, 125.
Use b = 10 buckets and sort the numbers using the least
significant digits in the first pass (i.e., distribute the numbers
into10 buckets based on the last digits), then sort the list of
numbers resulting from the first pass using the next significant
digits; finally, sort the list (of pass two) using the most
significant digits.
10 buckets 0 1 2 3 4 5 6 7 8 9
list after first pass 000 001 512 343 064 125 216 027 008 729
008 729
list after second pass 001 216 027
000 512 125 343 064
064
027
list after the third pass 008
001
000 125 216 343 512 729
The Stack ADT:
• A stack is a list with two operations insert and delete, such that
the most recently inserted item is deleted first (hence the name
LIFO, for a last-in-first-out structure).
• A stack is typically pictured as a “stack of items” arranged
vertically where the insertion and deletion take place at the “top”
pop (delete)
push (insert)
top
(deleted object)
New object
A circular (wrap-around) array implementation of queues:
When a fixed sized array is used to store the list of objects in a
queue, as objects come and leave, the portion of the array that
contains the current objects “drifts” towards the end of the
array, demonstrated in the following snapshots:
front back
wrap-around