This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 204
template Most lists, however, are organised in a more
complex IndColl<int> intarr(isize);
<float> floatarr(fsize Applied Programming The items in a generalised linked list are ordered
according to some key.
This key may be part of the data Applied Programming If the list is ordered by some key
can occur anywhere in the list.
W e need a search process to find where such
additions or deletions will occur.
additions – videos ordered by title
or it may be constructed from the data.
A simple (say, integer) key constructed from the
data may be included as part of the data.
Applied Programming To generalise the ADT so that one such class can
be used for any data, we'll use T for the datatype
– to be replaced by the actual type, or we can use
Applied Programming Thus, the private data of the class will be
typedef node *nodePtr; W e've already seen a diagram to represent a
node in a linked list.
next struct node
}; For the purpose of illustration, we'll assume the
data is an int. So a list with the data 1, 2, 3, 4,
and 6 in that order is
Head nodePtr head; 1 2 3 Applied Programming If the data was provided in order
just add the next node to the tail 4 6
0 Applied Programming Earlier, we had to proceed through a list to get to
the end (ready to add a new item).
the Unfortunately, data does not normally come presorted. So how would we proceed through the list,
displaying the contents?
displaying Thus, we need a two-step process:
• find where the new node should go set the current node pointer to head
while current node pointer points at something
display the data for the current node
set current node pointer to next node pointer • put it there
Sound familiar? It's Insertion Sort!
Insertion What's this in C++?
Applied Programming Applied Programming nodePtr curr; For example, here's one for int.
int. curr = head;
View Full Document
- Spring '14
- Computer Science