Linked Lists Flashcards
1
Q
How are linked list items structured?
A
Contains a value and a pointer to the next item in the list
2
Q
What variables are needed for a linked list class?
A
- Head: Points to the start of the linked list
- Counter: How many items are in the list
- maxSize: Limits the size of the linked list
3
Q
Benefits of linked lists compared to arrays
A
- Size is only limited by the system’s memory
- Adding and removing data is much easier
4
Q
How to addToHead() in a linked list?
A
- Create a new item
- Assign the pointer of the new item to the head
- Assign the head pointer to the new item
5
Q
How to addToTail() in a linked list?
A
- Create a new item
- Assign the last item’s pointer to the new item
6
Q
How to use addItemAt() to add an item in between the 4th and 5th element in a linked list?
A
- Create a new item
- Assign the pointer of the new item to the 5th element
- Assign the pointer of the 4th element to the new element
7
Q
How to use removeItemAt() to remove the 7th item in a list?
A
- Assign the 6th item’s pointer to the 8th item
- The garbage collector will delete the 7th item as it’s been dereferenced