1 / 39

Linked List Containers

Linked List Containers. Linked Lists. List consists of multiple listnodes Each listnode consists of Data Pointer to another node Traditional view of data:. Data. Data. Data. Linked Lists. Insertion at a specific point: Get a new node (allocate from memory) Set data pointer

Download Presentation

Linked List Containers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked List Containers

  2. Linked Lists • List consists of multiple listnodes • Each listnode consists of • Data • Pointer to another node • Traditional view of data: Data Data Data

  3. Linked Lists Insertion at a specific point: Get a new node (allocate from memory) Set data pointer Set link of new node to previous node link Set link of previous node link to new node FAT MAT RAT HAT

  4. FAT MAT RAT Linked Lists Deletion at a specific point: Set the link of the previous node to the link of the node to delete. FAT RAT

  5. Linked List Representations • Can locate data in any place in memory, not required to be sequential • No requirements on computing size of list beforehand • No more space than need • No bounds on space • No resizing • Still fairly trivial to manage and understand

  6. Linked Lists in C++ • Need to define a ListNode Class that represents: • Data • Pointer to another ListNode • Need to define a LinkedList Class that provides container operations (Add, Delete, etc), using ListNodes to implement the storage for the container

  7. Linked Lists in C++ • Node definition: class nodeName class ThreeLetterListNode { { private: private: type dataName; char data[3]; nodeName *link; ThreeLetterListNode * link; } }

  8. Linked Lists in C++ • Requirements for LinkedList construct: • Want to preserve encapsulation of nodes and ensure that updating the data and link pointers are only accomplished by the ListNode itself or the LinkedList • Arbitrary and unlimited amount of memory • Characterize the LinkedList as: A LinkedList consists of zero or more objects of type ListNode.

  9. Linked Lists • Linked List definition suggests that the List object actually physically contains lots of ListNodes. • Contains a pointer, called first, to one ListNode, from which the rest of the ListNodes can be found by following links. • All ListNode objects are not physically contained within the LinkedList object • To access private data members of ListNode class, without making the data members public or having public functions to set data and links, • make the LinkedList class a friend of the ListNode class:

  10. Linked Lists class LinkedList; // forward declaration template <class Type> class ListNode { friend class LinkedList<Type>; private: Type data; ListNode *link; }; template<class Type> class LinkedList { public: // manipulation operations private: ListNode<Type> *first; } ListNode.h LinkedList.h

  11. Forward Declarations • Forward Declarations: • Used when defining classes that rely on each other. • Indicates to compiler that the class in the forward declaration is going to be defined later and is a valid class. • Similar to use of function signatures to ensure compiler sees all available functions.

  12. Linked Lists • Linked List Node Constructor • Set Data Value, Set Pointer ListNode<Type>::ListNode<Type>(Type inputData, ListNode<Type>* inputLink) { data = inputData; link = inputLink; }

  13. List Operations • What operations do we want or need for the LinkedList? Constructor Destructor isEmpty() isFull() // doesn’t make sense in this context add() (element?, position?) delete() (element?, position?)

  14. Linked Lists • Linked List Constructor • Creates an empty list • Essence of list is “first” pointer, so that should be set to zero if empty LinkedList<Type>::LinkedList<Type>() { first = 0; }

  15. Linked List Insertion Function Interface: void add(Type & toAdd); Passed in a variable of type Type • 1st step: Generate a new ListNode • Should hold value toAdd of type Type • Should point to nothing

  16. Linked List Insertion • Node Creation: Node<Type> *node = new Node<Type>(toAdd, 0); • Then need to insert in “right” place in list. What is right place? • Depends on problem of interest • Sorted list? First in, first out list? • Let’s look at four cases: • Empty list • Non-empty, Front of list • Non-empty, Back of list • Non-empty, Arbitrary position in middle of list

  17. Insertion into Empty List • Empty List First First value 0 0 void LinkedList:<Type>:Add(Type & toAdd) { ListNode<Type> *node = new ListNode<Type>(toAdd, 0); if (first == 0) first = node; }

  18. Insertion • Non-empty list, insert at front first value1 value2 0 value3 0 node value1 value2 0 first value3

  19. Linked List Insertions • Insert at front method: ListNode<Type> *node = new ListNode<Type>(toAdd, first); first = node;

  20. Insertion • Non-empty list, insert at back first value1 value2 0 value3 0 node first value1 value2 value3 0

  21. Linked List Insertions • Insert at back method: // get to last node ListNode<Type>* current = first; while (current-> link != 0) { current = current->link; } // add ListNode<Type>* node = new ListNode<Type>(toAdd, 0); current->link = node;

  22. Insertion • Non-empty list, insert in middle current first value1 value2 0 value3 0 node value1 value2 0 first value3

  23. Linked List Insertions • Insert at arbitrary place: ListNode<Type>* current = first; while (someExpression holds) // current->value < 5 { // for example current = current-> link; } ListNode<Type>* node = new ListNode<Type>(value, current->link); current->link = node;

  24. Linked List Insertions • Insertion at front, insertion at end usually encapsulated into two linked list methods: • Insert (at front), Append (onto back) • Insertion in middle could be encapsulated as insertAtNth() • Usually seen instead as part of more complicated methods (insertSorted for example)

  25. Linked List: Deletion • How about deleting nodes? • Very similar to addition – 3 cases • Case 1: Delete from front value1 value2 0 first value3 first value3 value2 0

  26. Linked Lists: Deletion • Deletion from front: ListNode<Type>* node = first; first = first->link; delete node;

  27. Linked Lists: Deletion • Case 2: Delete from back current value1 value2 0 first previous value3 first value1 value3 0

  28. Linked Lists: Deletion • Deletion from back: // get to just before last node ListNode<Type>* previous =0; ListNode<Type>* current = first; while (current-> link != 0) { previous = current; current = current->link; } previous->link = 0; delete current;

  29. Linked Lists: Deletion • Case 3: Delete from arbitrary location previous value1 value2 0 first value3 current first value1 value2 0

  30. Linked Lists: Deletions ListNode<Type>* previous =0; ListNode<Type>* current = first; while (someExpression holds) //current->value != { // value3 for example previous = current; current = current->link; } previous->link = current->link; delete current;

  31. Iterators

  32. Implementing List Operations • There are a whole slew of operations that rely on iterating through the elements in a container: • Printing all values • Finding maximum and minimum • Finding average of numbers in container • Finding elements that satisfy a particular property • ….

  33. LinkedList Iterator • Assume we are staying within just the List and Nodes framework • We’ve used encapsulation to hide the Node data from us – we can only read it through the List itself. • To implement these features that have to look at every Node, we would need to add max, min, average, … functions to the List class • This is both unwieldy and prevents easy reuse – We are putting too much domain specific information in the List class • Let’s build one more class: An iterator: An object used to traverse the elements of a container class which also has privileged rights to read Nodes

  34. Properties of List Iterator • Define a class LinkedListIterator<Type> which: • Is a friend of both LinkedList<Type> (to find the front) and ListNode<Type> (to be able to read individual nodes) • Initialize with LinkedList<Type> object it will be iterating over • Define a pointer, current, in the iterator which is the pointer to a position in the LinkedList, thus a pointer to a ListNode • Define methods useful for iteration:

  35. Properties of List Iterator • Functions useful for iterating: • notNull() // make sure not past last data • nextNotNull() // make sure more data • first() // get data at front of list • next() // get data ahead one node from current

  36. List Iterator Example Iterator 1 -current first List1 Data1 List1 Data2 List1 Data3 Iterator 2 -current Iterator 3 –current (nextNotNull =false) Can have multiple iterators on the same List (Iterators 1 and 2 here) Iterator 3 is about to reach the end of its List (nextNotNull is false, meaning on last node) first List1 Data1 List1 Data2

  37. List Iterator Definition template <class Type> class LinkedListIterator { public: LinkedListIterator(const LinkedList<Type> & inputList); bool notNull(); bool nextNotNull(); Type* first(); Type* next(); private: const LinkedList<Type>& list; // list working on // this syntax above is a lot like storing a pointer to the list ListNode<Type>* current; // points to a node in list }

  38. List Iterator Definition • Need to add friend class LinkedListIterator<Type> to both LinkedList.h and ListNode.h These two classes have to grant friendship to the linked list iterator. • Implementation in LinkedListIterator.h

  39. Linked List Destructor • ~LinkedList() function [Destructor] Need to delete all nodes in the list ListNode<Type>* next; while (first != 0) { next = first->link; delete first; first = next; }

More Related