CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.

Slides:



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

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Data Structures and Applications Hao Jiang Computer Science Department Boston College.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
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.
1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Listen, Stacks und Queues headtail p x.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Introduction to Data Structures and Algorithms
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
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,
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Linked Data Structures
QueueStack CS1020.
Efficiency of in Binary Trees
Plan for the Week Review Big-Oh
Doubly Linked List Review - We are writing this code
What’s the Difference Here?
Stacks and Queues.
Priority Queue.
Programmazione I a.a. 2017/2018.
8-1.
8-1.
Linked List (Part I) Data structure.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Recall: stacks and queues
COMPUTER 2430 Object Oriented Programming and Data Structures I
Header and Trailer Sentinels
Recall: stacks and queues
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

CS 2430 Day 35

Agenda Introduction to linked lists Bag as linked list Stack as linked list

Growing slowly Do you remember the grow() method? What is its big O complexity? O(N), where N is the number of elements in the Bag, Queue, etc. Therefore, add(), enqueue(), etc., had worst case big O of O(N)

Growing faster Can we make a growable Bag class with O(1) worst case add() method? Yes, we can! We cannot use an array We will use a “linked list”

Introduction to linked lists

Linked lists Arrays are random access, i.e., access to array element is O(1) Linked lists can have data scattered all over memory Therefore, linked lists are (mostly) sequential access

Nodes Each element of a linked list is a Node Nodes have “info” and “next” fields The “next” field points to the next node in the list

The Node class class Node { public Object info; public Node next;// Note the type! public Node(Object theInfo, Node theNext) { info = theInfo; next = theNext; } }

An empty list Node list; // points to null... ► ► Ølist

Insert x at the back list = new Node(x, null);... What is the big O? O(1) ► ► xlistØØ

Insert y at the back list.next = new Node(y, null);... ► ► listxØyØ

Insert z at the back list.next.next = new Node(z, null);... ► ► listxØyØz

Delete from the front list = list.next;... What is the big O? O(1) ► ► listxyzØ

Insert at front list = new Node(w, list);... What is the big O? O(1) ► ► list w yzØ

Print everything out Node p = list; while (p != null) { System.out.println(p.info); p = p.next; } ► ► ► ► ► ► list p xyzØ

Always draw pictures when working linked list problems

Any questions?

Go to THE THING:

Growable Bag as linked list public class Bag { private Node list; public Bag() {... } public boolean add(Object obj) {... } // insert at front public boolean isEmpty() {... } public int size() {... } public void reset() {... } public boolean contains(Object target) {... } public boolean remove(Object obj) {... } } We will do some of the methods

public class Bag { private Node list; public boolean add(Object obj) { list = new Node(obj, list); return true; } }

public class Bag { private Node list;... public boolean isEmpty() { return list == null; } public void reset() { list = null; } }

public class Bag { private Node list;... public int size() { int count = 0; Node p = list; while (p != null) { count++; p = p.next; } return count; } } What is the big O? O(N) I think we can do better than this!

public class Bag { private Node list; private int count = 0; public void reset() { count = 0; list = null; }... }

public class Bag { private Node list; private int count;... public boolean add(Object obj) { list = new Node(obj, list); count++; return true; } public int size() { return count; } } Now size(), add(), reset() and isEmpty() are all O(1)