According this key may be part of the data applied

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 204 template Most lists, however, are organised in a more Most complex manner. complex IndColl<int> intarr(isize); intarr(isize IndColl<float> floatarr(fsize); <float> floatarr(fsize Applied Programming The items in a generalised linked list are ordered The according to some key. according This key may be part of the data Applied Programming If the list is ordered by some key • additions • removals can occur anywhere in the list. W e need a search process to find where such We 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 simple data may be included as part of the data. data Applied Programming To generalise the ADT so that one such class can To be used for any data, we'll use T for the datatype for datatype – to be replaced by the actual type, or we can use to templates. templates. Applied Programming Thus, the private data of the class will be struct node; typedef node *nodePtr; W e've already seen a diagram to represent a We've node in a linked list. node data next struct node { int data; nodePtr next; }; For the purpose of illustration, we'll assume the For data is an int. So a list with the data 1, 2, 3, 4, int So and 6 in that order is and 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 Earlier, the end (ready to add a new item). the Unfortunately, data does not normally come presorted. So how would we proceed through the list, So 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! Sound Insertion What's this in C++? Applied Programming Applied Programming nodePtr curr; For example, here's one for int. For int. curr = head; while (...
View Full Document

Ask a homework question - tutors are online