Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.

Similar presentations


Presentation on theme: "Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II."— Presentation transcript:

1 Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II

2 Linked List  Made up of Nodes  Each node has  An item  A reference to next node in the list CS2336: Computer Science II item next l1 item next l2 item next l3 706

3 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

4 Inserting an item  Insert a new item immediately after “this” node.  insertAfter method CS2336: Computer Science II item next l1 item next l2 item next l3 706 item next 3

5 Disadvantages over array  Finding the n th 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

6 Find n th 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 4 th This 3 th

7 Linklist ContactName List Example CS2336: Computer Science II

8 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

9 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


Download ppt "Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II."

Similar presentations


Ads by Google