Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Linked Lists Mohammed Almashat CS /03/2006.
Stacks, Queues, and Linked Lists
Linked Lists.
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
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.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
M180: Data Structures & Algorithms in Java
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.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Stacks, Queues, and Deques
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.
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
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,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Information and Computer Sciences University of Hawaii, Manoa
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked List by Chapter 5 Linked List by
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
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.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Linked Lists and Generics Written by J.J. Shepherd.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Linked Lists CS 367 – Introduction to Data Structures.
Week 15 – Monday.  What did we talk about last time?  Tries.
Linked Data Structures
CSE 1342 Programming Concepts
Week 4 - Monday CS221.
Week 4 - Friday CS221.
CSCI-255 LinkedList.
UNIT – I Linked Lists.
Week 3 - Friday CS221.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Stacks, Queues, and Deques
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

Week 4 - Monday

 What did we talk about last time?  Queues  Implementing queues with circular arrays

Bitmap Manipulator

 Most people messed up the add() method that takes an index  Most people didn't throw exceptions at quite the right times  There was a lot of inconsistent formatting  I don't specify what your formatting should be too closely, but it should be consistent  Check out the coding standards for the course: ▪

 Don't create a new Node unless you have to:  Never look at the next thing unless you have to: Node temp = new Node(); //wastes memory temp = head; Node temp = new Node(); //wastes memory temp = head; public boolean contains(int element) { Node temp = head; while(temp.next != null){ //two different bugs if( temp.value == element ) return true; temp = temp.next; } return false; } public boolean contains(int element) { Node temp = head; while(temp.next != null){ //two different bugs if( temp.value == element ) return true; temp = temp.next; } return false; }

 Use other methods to simplify code:  Except when it doesn't make sense  Using get() in addAll() is inefficient  Keep it simple:  Test, test, test! public boolean contains(int element) { return indexOf(element) != -1; } public boolean contains(int element) { return indexOf(element) != -1; } public boolean isEmpty() { return size == 0; } public boolean isEmpty() { return size == 0; }

Impromptu student lecture

 What is a linked list?  Why not just use (dynamic) arrays for everything? X head

 Insert at front (or back)  O(1)  Delete at front (or back)  O(1)  Arbitrary amounts of storage with low overhead

 Search  O(n)  Go to index  O(n)  Potentially significant memory overhead if data is small  Much easier to make pointer and memory errors (especially in C/C++)

 Class protecting nodes implementation  Generic class providing nodes with arbitrary type  Generic class with the addition of iterators

 I'm glad you asked  They allow a collection to be used in a foreach loop  So, what's a foreach loop?  It allows you to read (but not change) each value in a list public static int sum( int[] array ) { int total = 0; for( int value: array ) total += value; return total; } public static int sum( int[] array ) { int total = 0; for( int value: array ) total += value; return total; }

 Foreach loops work for any iterable list of any type public static double weigh(LinkedList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(LinkedList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(ArrayList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(ArrayList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(Wombat[] list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(Wombat[] list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; }

 Node consists of data and a single next pointer  Advantages: fast and easy to implement  Disadvantages: forward movement only X head

 Node consists of data, a next pointer, and a previous pointer  Advantages: bi-directional movement  Disadvantages: slower, 4 pointers must change for every insert/delete X head X tail

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

 Let’s try a simple definition for a linked list: public class LinkedList { private static class Node { public int data; public Node next; public Node previous; } private Node head = null; private Node tail = null; … }

 Continued implementation of a linked list  Circular linked lists and skip lists  Implementing a stack with a linked list  Keep reading section 1.3

 Keep working on Project 1  Due this Friday, September 18 by 11:59pm