Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reminder: Course Policy

Similar presentations


Presentation on theme: "Reminder: Course Policy"— Presentation transcript:

1 Reminder: Course Policy
Please be very careful not to cause distractions in the classroom and be respectful of others around you, especially considering the very large size of the class. Phone calls, messaging, discussion on a game you played recently, etc. Before OR After class

2 The Class chain

3 Reminder: Chain firstNode NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL pointer.

4 next (datatype chainNode<T>*)
The Class chain a b c d e NULL firstNode listSize = number of elements Use chainNode next (datatype chainNode<T>*) element (datatype T)

5 The Class chain template<class T>
class chain : public linearList<T> { public: // constructors and destructor defined here // ADT methods bool empty() const {return listSize == 0;} int size() const {return listSize;} // other ADT methods defined here protected: void checkIndex(int theIndex) const; chainNode<T>* firstNode; int listSize; };

6 Constructor template<class T>
chain<T>::chain(int initialCapacity = 10) {// Constructor. if (initialCapacity < 1) {ostringstream s; s << "Initial capacity = " << initialCapacity << " Must be > 0"; throw illegalParameterValue(s.str()); } firstNode = NULL; listSize = 0;

7 The Destructor template<class T> chain<T>::~chain()
{// Chain destructor. Delete all nodes // in chain. while (firstNode != NULL) {// delete firstNode chainNode<T>* nextNode = firstNode->next; delete firstNode; firstNode = nextNode; }

8 The Method get firstNode a b c d e template<class T>
NULL a b c d e template<class T> T& chain<T>::get(int theIndex) const {// Return element whose index is theIndex. checkIndex(theIndex); // move to desired node chainNode<T>* currentNode = firstNode; for (int i = 0; i < theIndex; i++) currentNode = currentNode->next; return currentNode->element; }

9 The Method indexOf template<class T>
int chain<T>::indexOf(const T& theElement) const { // search the chain for theElement chainNode<T>* currentNode = firstNode; int index = 0; // index of currentNode while (currentNode != NULL && currentNode->element != theElement) // move to next node currentNode = currentNode->next; index++; }

10 The Method indexOf // make sure we found matching element
// make sure we found matching element if (currentNode == NULL) return -1; else return index; }

11 Erase An Element erase(0) deleteNode = firstNode;
b c d e NULL firstNode erase(0) First do a checkIndex. deleteNode = firstNode; firstNode = firstNode->next; delete deleteNode;

12 Remove An Element template<class T>
void chain<T>::erase(int theIndex) { checkIndex(theIndex); chainNode<T>* deleteNode; if (theIndex == 0) {// remove first node from chain deleteNode = firstNode; firstNode = firstNode->next; }

13 Find & change pointer in beforeNode
erase(2) firstNode null a b c d e beforeNode Find & change pointer in beforeNode beforeNode->next = beforeNode->next->next; delete deleteNode;

14 Remove An Element else { // use p to get to beforeNode
chainNode<T>* p = firstNode; for (int i = 0; i < theIndex - 1; i++) p = p->next; deleteNode = p->next; p->next = p->next->next; } listSize--; delete deleteNode;

15 firstNode = new chainNode<char>(‘f’, firstNode);
One-Step insert(0,’f’) a b c d e NULL firstNode f newNode firstNode = new chainNode<char>(‘f’, firstNode);

16 Insert An Element template<class T>
void chain<T>::insert(int theIndex, const T& theElement) { if (theIndex < 0 || theIndex > listSize) {// Throw illegalIndex exception } if (theIndex == 0) // insert at front firstNode = new chainNode<T> (theElement, firstNode);

17 Two-Step insert(3,’f’) beforeNode = firstNode->next->next;
a b c d e NULL firstNode f newNode beforeNode beforeNode = firstNode->next->next; beforeNode->next = new chainNode<char> (‘f’, beforeNode->next);

18 Inserting An Element else { // find predecessor of new element
chainNode<T>* p = firstNode; for (int i = 0; i < theIndex - 1; i++) p = p->next; // insert after p p->next = new chainNode<T> (theElement, p->next); } listSize++;

19 50,000 operations of each type
Performance 50,000 operations of each type Start with an empty list and insert elements. Get each element once.

20 Performance 50,000 operations of each type
Operation FastArrayLinearList Chain get ms sec best-case inserts ms ms average inserts sec sec worst-case inserts sec sec best-case removes ms ms average removes sec sec worst-case removes sec sec 1.7GHz Pentium 4 with 512MB. Worst-case times less than average because of cache effects (see explanation in text). Average inserts  inserts at random positions

21 Performance – Updated Estimates
50,000 operations of each type Operation ArrayList LinkedList get ms sec best-case inserts ms ms worst-case inserts ms sec i7 with 16GB of RAM

22 Indexed AVL Tree (IAVL)
Performance Indexed AVL Tree (IAVL) We will study this linked structure when we get to Chapter 16. This structure does not have an effective array representation.

23 Chain With Header Node a b c d e headerNode NULL
Now insert/erase at left end (I.e., index = 0) are no different from any other insert/erase. So insert/erase code is simplified.

24 Empty Chain With Header Node
NULL

25 Circular List a b c d e firstNode
Useful, for example, when each node represents a supplier and you want each supplier to be called on in round-robin order. Other applications seen later. A variant of this has a header node—circular list with header node. When you don’t have a header node, it is often useful to have a last node pointer rather than a first node pointer. By doing this, additions to the front and end become O(1).

26 Doubly Linked List a b c d e firstNode lastNode NULL
Can be used to reduce worst-case run time of a linear list add/remove/get by half. Start at left end if index <= listSize/2; otherwise, start at right end.

27 Doubly Linked Circular List
firstNode

28 Doubly Linked Circular List With Header Node

29 Empty Doubly Linked Circular List With Header Node

30 The STL Class list Linked implementation of a linear list.
Doubly linked circular list with header node. Has many more methods than chain. Similar names and signatures.

31 To-do Practice (Easy Challenges): Read more on list:
Read more on list:


Download ppt "Reminder: Course Policy"

Similar presentations


Ads by Google