Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted list
Inserting into a sorted linked list Similar to before, but we will remove the need to “append” nodes to our list while (cursor != nullptr && cursor->value < num) { previous = cursor; cursor = cursor->next; } Edge case: The “insertNode” method should check for the empty case If the list is not empty, insert the new data just before the first node with value “greater than” the new one Second edge case: if the node should be the new “head”
Sorted List benefits No ambiguity about the structure of the list I.e. no need for separate “append” and “insert” methods Much faster search! How?
Doubly linked lists Idea: same as singly linked list, but each node also points to the previous: Can optionally also have a pointer to the tail, so we can start scanning from the end, and make appending easier Also helps with binary search
Inserting into a DLL Find node before the new one, nextNode Set newNode.prev = nextNode.prev Set newNode.prev.next = newNode Set newNode.next = nextNode Set nextNode.prev = newNode
List-like structures Linked lists are of independent usefulness (replace messiness of arrays) However, they can be used as “backend” for other types of data structures Stacks Queues Heaps Graphs Trees Sometimes need slight modification, depending on program needs
Common needs of data storage Similarity to lists Insert Delete Difference Place in the list may depend on when it is added rather than the actual value Temporal vs. spacial May depend on both!
stacks Stores data in a list, but data can only be removed in the reverse order of which it was added Called “First in, last out” data structure (FILO)
Stack with arrays Adding an item to a stack (to keep with the metaphor) is called “pushing” Removing is called “popping” With an array, pushing would look like:
Stacks with arrays ”Popping” off a stack retrieves the last item added, and removes it from the stack Implemented with arrays, the stack must be a fixed size, enlarged when needed
Stacks with arrays Extra work because of static size Attributes Need “isFull” and “isEmpty” check If we want to resize, need a copy constructor Attributes Array of objects int stackSize; // containing current size int top; // for the index of the top element (starts at -1 when empty and moves up)
Stacks with arrays To push an element: Make sure the array isn’t full, if so, error. Move the top index up (top is at -1 for an empty stack) Copy the new value into the to index
Stacks with arrays To pop an element: Check to make sure the stack isn’t empty Return the value at the top counter Decrement top counter
Stacks with arrays Regular constructor Copy constructor Take size argument Dynamically allocate an array Set “top” to -1 Copy constructor Allocate space equal to old stack Copy attributes Copy array elements
Stacks with arrays isFull isEmpty return top == stackSize – 1;
Stacks with linked list Same principle as with arrays, but instead we adapt the standard list operations to behave as a stack Need a “push” and “pop” method ”Push” puts an element on the head (or tail) “Pop” removes and returns the head (or tail) element Using the head is more efficient, especially with a singly linked list