Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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”

3 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?

4 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

5 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

6 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

7 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!

8 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)

9 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:

10 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

11 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)

12 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

13 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

14 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

15 Stacks with arrays isFull isEmpty return top == stackSize – 1;

16 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


Download ppt "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."

Similar presentations


Ads by Google