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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 9: Data Structures I
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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?
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
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 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
Chapter 12: Data Structures
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Queues.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Stacks, Queues, and Deques
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
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Stacks II CS Data Structures I COSC 2006
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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,
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’
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.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Click to edit Master text styles Stacks Data Structure.
Stack: a Linked Implementation
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 12: Data Structures
Stacks.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Data Structures and Database Applications Abstract Data Types
Chapter 12 Collections.
Chapter 12 Collections.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

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 (ADTs) queues stacks dynamic structures linked lists basic programming concepts object oriented programming topics in computer science syllabus

unit 11 2 Abstract Data Types H An abstract data type (ADT) is an organized collection of information and a set of operations used to manage that information H The set of operations define the interface to the ADT H As long as the ADT accurately fulfills the promises of the interface, it doesn't really matter how the ADT is implemented

unit 11 3 Queues H A queue is similar to a list but adds items only to the end of the list and removes them from the front H It is called a FIFO data structure: First-In, First-Out H Analogy: a line of people at a bank teller’s window enqueue dequeue

unit 11 4 Interface for ADT Queue Queue public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws QueueEmptyException; public void enqueue(Object o); public Object dequeue() throws QueueEmptyException; }

unit 11 5 Stacks H A stack ADT is also linear, like a list or queue H Items are added and removed from only one end of a stack H It is therefore LIFO: Last-In, First-Out H Analogy: a stack of plates

unit 11 6 Stacks H Stacks are often drawn vertically: poppush

unit 11 7 Interface for ADT Stack Stack public interface Stack { public int size(); public boolean isEmpty(); public Object peek() throws StackEmptyException; public void push(Object o); public Object pop() throws StackEmptyException; }

unit 11 8 Push Implementation top: 5 elements

unit 11 9 Push Implementation top: 5 elements obj if (isFull()) throw Exception;

unit Push Implementation top: 5 elements obj if (isFull()) throw Exception; elements[top] = obj;

unit Push Implementation top: 6 elements if (isFull()) throw Exception; elements[top] = obj; top++;

unit Queue Implementation - Musical Chairs H Use an array of size n, and a rear index H Elements are always inserted to rear H Elements are removed from front, all other elements move as in musical chairs

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Remove frontrear

unit Circular Queue - Remove frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Remove frontrear

unit Circular Queue - Insert frontrear

unit Circular Queue - Insert frontrear what will happen if we add one more element?

unit Circular Queue - Operations H front and rear are always incremented modulu n H queue is full if (rear + 1) % n = front H queue is empty if rear = front H in an array of size n, only n-1 cells can be utilized

unit Static vs. Dynamic Structures  A static data structure has a fixed size (this meaning is different than those associated with the static modifier) H Arrays are static; once you define the number of elements it can hold, it doesn’t change H A dynamic data structure grows and shrinks as required by the information it contains H Example of dynamic data structures: linked lists

unit Object References H Object reference is a variable that stores the address of an object H A reference can also be called a pointer H They are often depicted graphically: student John Smith

unit References as Links H Object references can be used to create links between objects  Suppose a Student class contained a reference to another Student object John Smith Jane Jones

unit References as Links H References can be used to create a variety of linked structures, such as a linked list: studentList element next } node

unit “Generic” Node Class Node class Node { private Object element; private Node next; // two overloaded constructors public Node() { this(null,null); } public Node(Object element, Node next) { this.element = element; this.next = next; } // functionality methods void setElement(Object element) { //...} void setNext(Node next) { //...} Object getElement() { //...} Node getNext() { //...} }

unit A Linked Node Chain head next element null SingleLinkedList

Stack Implementation: linked list size: 0 top: null

size: 0 top: push(Object obj) obj

size: 0 top: n push(Object obj) { Node n = new Node(); obj push(Object obj)

size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); obj

size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); obj

size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; obj

size: 1 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; size++; } obj

size: 1 top:

size: 1 top: obj push(Object obj)

size: 1 top: n obj push(Object obj) { Node n = new Node();

size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj);

size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top);

size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n;

push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; size++; } size: 2 top: n obj

size: 2 top:

size: 2 top: Object pop() { if (isEmpty()) throw Exception;

Object pop() { if (isEmpty()) throw Exception; Object temp; size: 2 top: temp

Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); size: 2 top: temp

Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size: 2 top: temp

Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size--; size: 1 top: temp

Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size--; return temp; } size: 1 top: temp

size: 1 top: temp

size: 1 top: temp

size: 1 top: temp

interface Stack ArrayStack implements Stack ArrayStack(int size) push() throws StackFullException pop() isFull() size()... SinglyLinkedStack implements Stack push() pop() size()... push() pop() size()...

unit Using a Stack: Balanced Strings H A single character string which is not an opening or a closing character is a balanced string H If the strings s 1 and s 2 are balanced, then s 1 + s 2 (i.e., the concatenation of s 1 and s 2 ) is a balanced string H If a string s is balanced, and c op,c cl is a matching pair of opening and closing characters, then c op + s + c cl is a balanced string

unit Balanced String Verification Algorithm boolean isBalanced(String str) for each character c in str[0]... str[length-1]: if c is an opening character then push it onto the stack; if c is a closing character then if the stack is empty, return false; else // stack not empty pop the topmost character t from the stack; if t and c do not match return false otherwise // c is not a closing character do nothing (skip the character). if the stack is empty, return true, else return false.

unit Using a Queue: ComputationSimulation  public abstract void computeOneStep() - Performs a single computation step  public abstract boolean isCompleted() - Returns true if the computation is completed, false otherwise  public Object getResult() - Returns the result of this computation, null if computation is not completed H …

unit An Iteration of a Time Sharing Machine computation queue

unit computation queue

unit computation queue computeOneStep() K times

unit computation queue isCompleted() ?

unit computation queue No ?

unit computation queue Yes ? getResult()

unit Collection Classes H The Java 2 platform contains a Collections API H This group of classes represent various data structures used to store and manage objects  Their underlying implementation is implied in the class names, such as ArrayList and LinkedList  Several interfaces are used to define operations on the collections, such as List, Set, SortedSet, Map, and SortedMap

unit When do we use dynamic structures? static H insert and remove items from unsorted lists H examine elements of lists dynamic H insert and remove items from sorted lists H length of list can change in orders of magnitude