Today’s Agenda Linked Lists Double ended Linked Lists Doubly Linked Lists CS2336: Computer...
-
Upload
damian-cole -
Category
Documents
-
view
218 -
download
0
Transcript of Today’s Agenda Linked Lists Double ended Linked Lists Doubly Linked Lists CS2336: Computer...
Today’s Agenda
Linked Lists
Double ended Linked Lists
Doubly Linked Lists
CS2336: Computer Science II
Linked List
Made up of Nodes
Each node has An item A reference to next node in the list
CS2336: Computer Science II
item
nextl1
item
nextl2
item
nextl3
7 0 6
Advantages over array
Inserting item into middle of linkedList takes constant time if you have reference to the node
The list can keep growing until memory runs out
CS2336: Computer Science II
Inserting an item
Insert a new item immediately after “this” node. insertAfter method
CS2336: Computer Science II
item
nextl1
item
nextl2
item
nextl3
7 0 6
item
next
3
Disadvantages over array
Finding the nth item of a linkedList takes O(n) because it is proportional to size of the list. But in array you can do it in constant time
A lot of data structures that we will see in this course are trying to find a solution that works quickly in all the different cases both finding and inserting new items
CS2336: Computer Science II
Find nth position
First we check , if the position is equal to 1 then we find the solution and return this
Then we check if the position is less than 1 or our list is empty (next is null) , so return null
If none of the above, then we use recursion. The idea is that if I jump forward one node or I jump
to the next reference then I can call nth recursively on that node and the position is one less.
CS2336: Computer Science II
This
4th
This 3th
Linklist
ContactName List Example
CS2336: Computer Science II
Double ended linked list
Think about a neighborhood with neighbors
First we create neighbor object
Double ended has both reference to the first and the last link in the list
Firstlink and lastlink
Add new neighbor to the beginning of the list
Add new neighbor to the end of the list
CS2336: Computer Science II
Doubly linked list
Unlike the other list that they only had value next and they could only go forward, we have value previous so we can go backward too.
Add after a specific key
Add on order
CS2336: Computer Science II