Week 4 - Friday CS221.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS Data Structures II Review COSC 2006 April 14, 2017
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
TCSS 342, Winter 2005 Lecture Notes
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Iterators CS 367 – Introduction to Data Structures.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Week 15 – Monday.  What did we talk about last time?  Tries.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
CSE 1342 Programming Concepts
Elementary Data Structures
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Elementary data structures
Week 2 - Wednesday CS221.
Week 4 - Monday CS221.
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
QueueStack CS1020.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Practicum 1: - Persistent vs. destructive lists - Java interfaces
UNIT – I Linked Lists.
Queues Rem Collier Room A1.02
Heaps And Priority Queues
CS 1114: Implementing Search
Week 3 - Friday CS221.
Week 2 - Friday CS221.
Problems with Linked List (as we’ve seen so far…)
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
Week 11 - Friday CS221.
LINKED LISTS CSCD Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
CMSC 341 Lecture 5 Stacks, Queues
THURSDAY, OCTOBER 17 IN LAB
Lecture 2: Implementing ADTs
Stacks, Queues, and Deques
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
Lesson Objectives Aims
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Data Structures and Algorithms for Information Processing
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Introduction to Data Structure
Lecture 16 Stacks and Queues CSE /26/2018.
Fundaments of Game Design
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Presentation transcript:

Week 4 - Friday CS221

Last time What did we talk about last time? Doubly linked list implementation

Questions?

Project 1 Bitmap Manipulator

Interview question You are given a reference to a node in a singly linked list and some data You need to insert the data into the node in front of the node you are given a reference to You do not have the head reference, you do not have a reference to the previous node How do you do the insertion?

Generic Linked List with Iterator

Things aren't that different! The generic part is really easy once you get the syntax set up You use T instead of int (or whatever type you designed your linked lists to hold) The trickier thing is to define an iterator class that can keep track of where you are inside the list It has to be a non-static inner class!

Definition public class LinkedList<T> implements Iterable<T> { private class Node { public T data; public Node next; public Node previous; } private class ListIterator implements Iterator<T> { public Node current; … private Node head = null; private Node tail = null; public int size = 0;

Constructor Create a new iterator that points at the head of the list

boolean hasNext() Whether or not there is something in the current spot in the list

T next() Get the current thing in the list

void remove() Remove the current item from the list (optional)

Other kinds of linked lists

Circular linked lists 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

Performance of a circular linked list Insert at front (or back) Θ(1) Delete at front Delete at back costs Θ(n) unless we used doubly linked lists Search Θ(n)

Skip lists 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 X 28 X 5 41 X 3 14 29 58

Performance of skip lists If ordered, search is Θ(log n) Go to index is Insert at end Delete Totally insane, at least Θ(n) Trees end up being a better alternative

Self organizing lists 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

Linked List Stack

Stack Implementations Dynamic array Advantages: pop and top are O(1) Disadvantages: limited size, making push O(n) in the worst case (still O(1) amortized) Linked list Advantages: push, pop, and top are O(1) Disadvantages: slightly slower than the array version, considerably more memory overhead

Linked list implementation public class ListStack { private static class Node { public String data; public Node next; } private Node top = null; private int size = 0; public void push(String value) {} public String pop() {} public String peek() {} //instead of top public int size() {}

Linked List Push

Linked List Pop

Linked List Peek

Linked List Size

Queue Implementations Circular array Advantages: dequeue and front are O(1) Disadvantages: limited size, making enqueue O(n) in the worst case (still O(1) amortized) Linked list Advantages: enqueue, dequeue, and front are O(1) Disadvantages: slightly slower than the array version, considerably more memory overhead

Linked list implementation class ListQueue { private class Node { public String data; public Node next; } private Node head = null; private Node tail = null; private int size = 0; public void enqueue(String value) {} public String dequeue() {} public String front() {} public int size() {}

Linked List Front

Linked List Size

Linked List Enqueue

Linked List Dequeue

Review

Week 1 Programming model Java Java Collections Framework OOP Polymorphism Interfaces Threads Exceptions Generics Java Collections Framework

Week 2 Big Oh Notation Big Omega and Big Theta Abstract Data Types Formal definition: f(n) is O(g(n)) if and only if f(n) ≤ c∙g(n) for all n > N for some positive real numbers c and N Worst-case, asymptotic, upper bound of running time Ignore lower-order terms and constants Big Omega and Big Theta Abstract Data Types Array-backed list

Week 3 Stacks Queues FILO data structure Operations: push, pop, top, empty Dynamic array implementation Queues FIFO data structure Operations: enqueue, dequeue, front, empty Circular (dynamic) array implementation JCF implementations: Deque<T> interface ArrayDeque<T> LinkedList<T>

Week 4 Linked lists Special lists Linked list implementation of stacks Performance issues Single vs. double Insert, delete, find times Special lists Circular Skip Self-organizing Linked list implementation of stacks Linked list implementation of queues

Sample Problems

Running time Let M and N be two integers, where M is no larger than N Use Big Θ notation to give a tight upper bound, in terms of N, on the time that it will take to Add M and N (by hand, using the normal algorithm) Multiply M and N (by hand, using the normal algorithm) Use Big Θ notation to give the same bounds but this time in terms of n, where n is the number of digits in N

What's the running time in Θ? int end = n; int count = 0; for( int i = 1; i <= n; i++ ) { end /= 2; for( int j = 1; j <= end; j++ ) count++; }

What's the running time in Θ? int end = n; int count = 0; for( int i = 1; i <= n*n; i+=2) count++;

What's the running time in Θ? int count = 0; for( int i = 1; i <= n; i++ ) { for( int j = 1; j <= n/j; j++ ) count++; }

Lighten If we increase the R, G, and B values of every pixel by 25%, the image will get lighter Let pixels be a byte[][] array with height rows and width*3 bytes in each row Write the code to lighten the image by 25% To do the math, you've got to change the range from -128 – 127 to 0 – 255 Then cap the value of each color component at 255 and shift back to -128 – 127

Reverse a linked list Assume the following: public class List { private static class Node { public int data; public Node next; } private Node head = null; … Write a method in List that reverses the linked list.

Code to reverse a linked list public void reverse() { if( head != null ) { Node reversed = head; Node temp = head; Node rest = head.next; temp.next = null; while( rest != null ) { temp = rest; rest = rest.next; temp.next = reversed; reversed = temp; } head = reversed;

Palindrome Write a method that takes a CharBuffer object The CharBuffer object has two methods: nextChar() which extracts a char from the input stream hasNextChar() which returns true as long as there is another char to extract The method should return true if the input stream is a palindrome (the same backwards as forwards) and false otherwise Use no String objects or arrays (other than the one embedded in the stack) Hint: Use at least 3 JCF Deque (stack) objects

Upcoming

Next time… Exam 1!

Reminders Finish Project 1 Exam 1 is Monday Due tonight, September 22 by 11:59pm Exam 1 is Monday