Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Data Structure Dr. Mohamed Khafagy.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Data Structures Hash Table (aka Dictionary) i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, Andreas Veneris, Glenn Brookshear,
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Objectives of these slides:
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
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.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Review Array Array Elements Accessing array elements
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.
Chapter 12 – Data Structures
Chapter 15 Lists Objectives
Stacks and Queues.
Stacks and Queues.
i206: Lecture 11: Stacks, Queues
i206: Lecture 10: Lists, Stacks, Queues
Stacks and Queues CLRS, Section 10.1.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Presentation transcript:

Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst

John Chuang2 Outline  What is a data structure  Basic building blocks: arrays and linked lists  Data structures (uses, methods, performance): -List, stack, queue -Dictionary -Tree -Graph

John Chuang3 List  An ordered collection of objects  Stacks and queues are special cases of lists Brookshear Figure 8.1

John Chuang4 Stack  Container of objects that are inserted and removed according to the principle of -LIFO: Last-in-first-out  Objects can be inserted at any time, but only the most recently inserted can be removed at any time.  Operations: -Push: enter item onto stack -Pop: remove item from stack

John Chuang5 Why Stacks?  The Undo facility in software applications  The Back button facility in web browsers  In general, stacks are useful for backtracking and recursion

John Chuang6 Stack Methods  push (o) – Insert an item into/onto the stack -Input: an object. Output: none. -If the stack has a fixed size and the stack cannot accept the push, an stack-overflow exception/error is thrown (or returned)  pop() – Returns the most recently inserted object from the stack and removes the object from the stack (an object is removed in last-in-first-out order) -Input: none. Output: an object. -If the stack is empty, a stack-empty exception/error is thrown (or returned)

John Chuang7 Stack Methods  Auxiliary/Support Methods -size() – Returns the number of objects in the stack -Input: none. Output: non-negative integer. -isEmpty() (or empty()) – Returns true if there are no objects in the stack -Input: none. Output: true or false -peek() (or top()) – Returns a reference to (alternatively, a copy of) the most recent item put into the stack -Input: none. Output: reference to an object (or an object if a copy)

John Chuang8 Stack Running Times  What is the running time of each operation?  Push O(1)  Pop O(1)  isEmpty() O(1)

John Chuang9 Stack Implementation  In Python, the list can be used as a stack: -list.append(x) -list.pop()  Let’s try to implement our own stack as an exercise in understanding what it takes to implement a data structure

John Chuang10 Stack Implementation in Python class Stack(list): def push(self, item): self[len(self):] = [item]# add item to end of list def pop(self): if not self: return None# stack is empty else: top = self[-1]# last element of list del self[-1]# delete last element return top def top(self): if not self: return None else: return self[-1] def isEmpty(self): return not self Brookshear Figure 8.10

John Chuang11 Queue  Container of objects that are inserted and removed according to the principle of -FIFO: First-in-first-out  Objects can be inserted at any time, but only the least recently inserted can be removed at any time.  Operations: -Enqueue: put item onto queue -Dequeue: remove item from queue

John Chuang12 Why Queues?  Queues are used extensively in -Computer networking -For keeping storing and sending network packets -Operating systems -For scheduling processes to receive resources -Playlists for your mp3 player -…

John Chuang13 Queue Methods  enqueue(item) – Insert the item into the queue. -If the queue has a fixed capacity, an exception/error will be returned if attempting to enqueue an item into a filled queue. -Input: item. Output: none.  dequeue() – Returns a reference to and removes the item that was least recently put into the queue (first-in- first-out) -If the queue is empty, an exception/error will be returned if attempting to dequeue. -Input: none. Output: item

John Chuang14 Queue Methods  size() (or getSize()) – Returns the number of items in the queue. -Input: none. Output: integer.  isEmpty() – Checks if the queue has no items in it. Returns true if the queue is empty. -Input: none. Output: true or false.  front() – Returns a reference to the “first” item in the queue (the least recent item). If the queue is empty, an exception/error will be returned if attempting to dequeue. -Input: none. Output: item.

John Chuang15 Queue Running Times  What is the running time of each operation?  enqueue O(1)  dequeue O(1)  isEmpty() O(1)

John Chuang16 Queue Implementation  In Python, the list can be used as a queue: -list.append(x) -list.pop(0) Brookshear Figure 8.13

John Chuang17 Circular Queue Brookshear Figure 8.14

John Chuang18 Priority Queues  Similar to queues  Items with the highest priority are removed first  Items with the same priority are removed in FIFO order  How might you implement a priority queue?