Download presentation
Presentation is loading. Please wait.
Published byRoy Pitts Modified over 9 years ago
1
Week 3 - Wednesday
2
What did we talk about last time? Started linked lists
4
Bitmap Manipulator
6
Impromptu Student Lecture
7
You are given a singly linked list It may have a loop in it, that is, a node that points back to an earlier node in the list If you try to visit every node in the list, you’ll be in an infinite loop How can you see if there is a loop in a linked list?
9
Let’s try a simple definition for a linked list: public class LinkedList { private static class Node { public int data; public Node next; } private Node head = null; … }
13
Linked lists can be made circular such that the last node points back at the head node This organization is good for situations in which we want to cycle through all of the nodes in the list repeatedly tail 23 47 58
14
Insert at front (or back) O(1) Delete at front O(1) Delete at back costs O(n) unless we used doubly linked lists Search O(n)
15
We can design linked lists with multiple pointers in some nodes We want ½ of the nodes to have 1 pointer, ¼ of the nodes to have 2 pointers, 1/8 of the nodes to have 3 pointers… head 14 5 5 3 3 29 28 41 58 X X X
16
If ordered, search is O(log n) Go to index is O(log n) Insert at end O(log n) Delete Totally insane, at least O(n) Trees end up being a better alternative
17
We want to make items that are used frequently easy to get at Several different approaches, mostly based on finding items repeatedly Move to front: After finding an item, put it in the front Transpose: After finding an item, move it up by one Count: Keep the list ordered by how often you get a particular item (requires a counter in each node) Ordering: Sort the list according to some feature of the data
20
Generic linked list implementation JCF List interface ArrayList class LinkedList class
21
Keep reading Chapter 3 Keep working on Project 1 Due Friday, September 19 by 11:59pm Finish Assignment 2 Due Friday by 11:59pm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.