Linked List Flashcards
Define link list
An abstraction of a list
Arrays use ______ memory
Static memory
- Can’t grow/shrink (fixed)
- Contiguous Memory!
What is the BEST aspect of Arrays/ArrayLists
Contiguous Memory! (direct access)
What is the WORST aspect of Arrays/ArrayLists
Shifting cost! O(n)
What is the BEST aspect(s) of Linked List
Dynamic & Shifting cost! O(1)
What is the WORST aspect of Linked List
Not contiguous (no direct access)
What is the ONE job of the head of the list?
to POINT to the first part of the linked list!! If we ever lose the head- we will lose the list.
What does the class LinkedList do?
Controls the list via methods! And holds the head!
What does the class LLnode do?
Creates the object to be stored (the data) and the next field that links the nodes (the next)
QUICK! How do we traverse a linked list if we want to adjust/print every node?
LLnode helpPtr = head;
while(helpPtr != null){
System.out.println(helpPtr.getData());
helpPtr = helpPtr.getNext();
}
How do we insert a new node into a linked list?
- Make the new LLnode object you want to insert.
- Determine the insertion point
- helpPtr will point to the insertion points predecessor *
- Point to the new nodes successor
- Point to the new nodes predecessor
*This means helpPtr.next would access the successor!
How do we delete a node from a linked list?
- Identify what node you are looking to delete
- if its the first node- we can just point head to head.next
- else? helpPtr should stop at the PREDECESSOR! Meaning when we loop- check helpPtr.next.data!
Describe the differences between a Linked list and a Doubly linked list:
Linked List:
- ONLY points to next
Doubly Linked List:
- Has access to PREVIOUS and NEXT!
In a doubly linked list how would we insert the object Sharron between Harry and Sam?
<- <- helpPtr*
[Harry] -> -> [Sam]*
|
[Sharron]
IN ENGLISH!
- Sharron -> Sam
- Sharron -> Harry
- Harry -> Sharron
- Sam -> Sharron
In a doubly linked list how would we insert the object Sharron between Harry and Sam?
<- <- helpPtr*
[Harry] -> -> [Sam]*
|
[Sharron]
IN pseudo-CODE!
- newNode.next = helpPtr
- newNode.prev = helpPtr.prev
- helpPtr.prev.next = newNode
- helpPtr.prev = newNode