Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.

Slides:



Advertisements
Similar presentations
Linked Lists Mohammed Almashat CS /03/2006.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
CS Data Structures II Review COSC 2006 April 14, 2017
COMP 103 Linked Stack and Linked Queue.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Topic 11 Linked Lists -Joel Spolsky
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Iterators CS 367 – Introduction to Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Linked Lists Tonga Institute of Higher Education.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
CS 367 Introduction to Data Structures Lecture 5.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
April 27, 2017 COSC Data Structures I Review & Final Exam
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
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.
Linked List ADT used to store information in a list
Week 4 - Monday CS221.
Chapter 12 – Data Structures
QueueStack CS1020.
Week 4 - Friday CS221.
UNIT – I Linked Lists.
CS 1114: Implementing Search
Week 3 - Friday CS221.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
Week 11 - Friday CS221.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lesson Objectives Aims
Presentation transcript:

Week 4 - Friday

 What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators

Bitmap Manipulator

 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?

 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!

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

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

Whether or not there is something in the current spot in the list

Get the current thing in the list

Remove the current item from the list (optional)

 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

 Insert at front (or back)  Θ(1)  Delete at front  Θ(1)  Delete at back costs Θ(n) unless we used doubly linked lists  Search Θ(n)Θ(n)

 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 X X

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

 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

 Dynamic array  Advantages: pop and top are O(1)  Disadvantages: limited size, making enqueue 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

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() {} } 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() {} }

 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

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() {} } 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() {} }

 Recursion

 Finish Project 1  Due tonight, September 18 by 11:59pm  Exam 1 is next Friday  My office hours today from 3:30-5pm are canceled due to travel  Anyone want to work for ITS?  For women in CS:  Speaking-Lancaster/