Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.

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

Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Stack & Queues COP 3502.
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.”
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Stacks.
Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© 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:
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
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
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.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
 Pearson Education, Inc. All rights reserved Data Structures.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Stacks and Queues Introduction to Computing Science and Programming I.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Queue.
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 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
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.
Data Structures: Linked Lists
Chapter 12 – Data Structures
Chapter 15 Lists Objectives
Stacks and Queues.
8-1.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
8-1.
Lecture 21 Stacks and Queues Richard Gesick.
Stacks and Queues.
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.
More Data Structures (Part 1)
Stacks, Queues, and Deques
Stacks and Queues.
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter 7 Data Structures: Stacks & Queues Chapter 7 Data Structures: Stacks & Queues

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Objectives In this chapter you will learn: To create and manipulate dynamic data structures, such as stacks and queues. Various important applications of linked data structures. How to create reusable data structures with classes, inheritance and composition. In this chapter you will learn: To create and manipulate dynamic data structures, such as stacks and queues. Various important applications of linked data structures. How to create reusable data structures with classes, inheritance and composition.

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Outline 1.Introduction 2. Stack 3.Queue

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub A data structure is organiTes information so that it efficient to access and process. In this chapter we study several dynamic data structures -- stacks and queues. A data structure is organiTes information so that it efficient to access and process. In this chapter we study several dynamic data structures -- stacks and queues. 1. Introduction

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Stacks – Last-in, first-out (LIFO) data structure Method push adds a new node to the top of the stack Method pop removes a node from the top of the stack and returns the data from the popped node – Program execution stack Holds the return addresses of calling methods Also contains the local variables for called methods – Used by the compiler to evaluate arithmetic expressions Stacks – Last-in, first-out (LIFO) data structure Method push adds a new node to the top of the stack Method pop removes a node from the top of the stack and returns the data from the popped node – Program execution stack Holds the return addresses of calling methods Also contains the local variables for called methods – Used by the compiler to evaluate arithmetic expressions 2. The Stack ADT

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub A stack is a list that limits insertions and removals to the front (top) of the list. 2. The Stack ADT

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Stack methods push, pop, isEmpty and print are performed by inherited methods insertAtFront, removeFromFront, isEmpty and print push calls insertAtFront: insert an object onto the top of the stack. pop calls removeFromFront : remove the top object from the stack..Peek : retrieve the top object without removing it. Stack methods push, pop, isEmpty and print are performed by inherited methods insertAtFront, removeFromFront, isEmpty and print push calls insertAtFront: insert an object onto the top of the stack. pop calls removeFromFront : remove the top object from the stack..Peek : retrieve the top object without removing it. Stack class that inherits from List

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub A Stack is very easy to design as an extension of our generic List structure. List # name: String + List () + List(n: String) # insertAtFront(o: T) # insertAtBack(o: T) # removeFromFront():T # removeFromBack():T # isEmpty(): Boolean T data 1 next 1 Node 1 first Stack + Stack() + push(in o:T) + pop(): T + peek(): T Inheritance: Protected elements are inherited by Stack class. Abstract Data Type: We limit access to the data to push() and pop(). Stack class that inherits from List

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub The Generic Stack Class: Implementation public class Stack extends List { public Stack() { super(“ST”); } public Stack(String s) { super(s); } public void push( T obj ) { insertAtFront(obj); } public T pop() { if (isEmpty()) throw new Exception("Stack is empty"); return removeFirst(); } public T peek() { if (isEmpty()) throw new Exception("Stack is empty"); return getFromFront(); } } // Stack

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Testing the Stack Class//==Stack of Student elements = Stack ST= new Stack (); Student s1 =new Student("Saad"); s1.setScore(10,20,15); s1.computeCourseGrade(); Student s2=new Student("Ali"); s2.setScore(10,50,40); s2.computeCourseGrade(); Student s3=new Student("Nabil"); s3.setScore(30,10,15); s3.computeCourseGrade(); Student s4=new Student("Sami"); s4.setScore(32,14,44); s4.computeCourseGrade(); ST.push(s1); ST.push(s2); ST.push(s3); ST.push(s4); // display all inofrmations inside Stack and calculate the average of the totalmarks for all passed students int sum=0, nb=0; Stack LS= new Stack (); Student st; try { while (true) { st=ST.pop(); LS.push(st); System.out.println(st.getName() + st.getCourseGrade()+ st.getTotal()); if (st.getCourseGrade().equals("Pass")) { sum+=st.getTotal(); nb++; } } } catch(Exception e) {} System.out.println("The average of total marks for passed students is : "+ 1.0*sum/nb); try { while (true) {ST.push(LS.pop());} } catch(Exception e) {} }// End of main Testing the Stack Class

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Queue –Similar to a checkout line in a supermarket –First-in, first-out (FIFO) data structure Enqueue inserts nodes at the tail (or end) Dequeue removes nodes from the head (or front) –Used to support print spooling A spooler program manages the queue of printing jobs Queue –Similar to a checkout line in a supermarket –First-in, first-out (FIFO) data structure Enqueue inserts nodes at the tail (or end) Dequeue removes nodes from the head (or front) –Used to support print spooling A spooler program manages the queue of printing jobs 3. The Queue ADT

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub A queue is a list that limits insertions to the back and removals to the front of a list. Operations –Enqueue: insert an object onto the rear of the list. –Dequeue: remove the object at the front of the list. –Empty: return true if the queue is empty. 3. The Queue ADT

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub List # name: String + List () + List(n: String) # insertAtFront(o: T) # insertAtBack(o: T) # removeFromFront():T # removeFromBack():T # isEmpty(): Boolean T data 1 next 1 Node 1 first Queue +Queue() + enqueue(in o:T) + dequeue(): T Queue class that inherits from List Abstract Data Type: We limit access to the data to enqueue() and dequeue(). Inheritance: Protected elements are inherited by Queue class. A Queue is very easy to design as an extension of our generic List structure.

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub Queue methods are performed by inherited methods from a List –Method enqueue calls List method insertAtBack –Method dequeue calls List method removeFromFront Queue methods are performed by inherited methods from a List –Method enqueue calls List method insertAtBack –Method dequeue calls List method removeFromFront The Queue Class

Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub The Queue Class: Implementation We only implement the enqueue() and dequeue() methods, thereby restricting the queue’s behavior. public class Queue extends List { public Queue() { super(“QU”); } public Queue(String s) { super(s); } public void enqueue(T obj) { insertAtBack( obj ); } public T dequeue() { if (isEmpty()) throw new Exception(“Queue is empty"); return removeFirst(); } } // Queue