Linked List Flashcards
What is a linked list?
linked list diagram
ordered collection of data = node with data and reference to the next node;
order won’t change
head, node, data, reference to the next node, tail
Create 2 separate nodes and point one to next.
Images from Algorithms - linked lists doc
Coding exercise - linked lists.
Use new Node to set up node one and node 2
Use a constructor to set up class Node class Node {
}
class LinkedList {
}
Why set to null instead of undefined?
undefined means the data hasn’t been set
null means it has been set to undefined
linkedList only knows about the first node = head, if you want info about other nodes, you have to crawl up the linked list
Use a constructor to create a class LinkedList and set the head to point to a node
Images from Algorithms - linked lists doc
Linked List’s InsertFirst
create a head
create a node
break link on head with the class
move new node to be first and link it to tail (stick it to the front) - meaning before the former head
link the class with the head (update the head reference)
Images from Algorithms - linked lists doc
sizing a list
Images from Algorithms - linked lists doc
linkList’s methods: getFirst, getLast, clear, removeFirst
getFirst{
return this.head
{
getLast{}
Images from Algorithms - linked lists doc
clear(){
This.head = null
}
removeFirst()
Remove last
Images from Algorithms - linked lists doc
Insert last
Images from Algorithms - linked lists doc