COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks, Queues & Deques CSC212.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks  Introduction  Applications  Implementations  Complex Applications.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Stacks (and Queues).
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Revised based on textbook author’s notes.
Homework 4 questions???.
CSE 373: Data Structures and Algorithms
Stacks Chapter 4.
Data Structures – Week #3
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture No.07 Data Structures Dr. Sohail Aslam
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSC 143 Stacks [Chapter 6].
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.
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik.
Introduction to Stacks
Stack.
Introduction to Stacks
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

public class Stack { private Object[] items; private int top; public Stack (int size) . . . public void push ( Object obj ) public Object pop() public boolean isEmpty() public boolean isFull() }

Additional Operations? Peek? As an implementer As a user

User Method peek Given a Stack with the methods isEmpty isFull push pop How can a user have a look at the element before calling pop? Can NOT access private data! User peek method

// User method outside Stack class public Object peek ( Stack s ); Stack myStack = new Stack( 1000 ); ... Object x = peek( myStack ); if (x == null) . . . else if (x instanceof FixedPoint) else if (x instanceof Golfer) else

public Object peek ( Stack s ) { if ( s.isEmpty() ) return null; // Don’t change the stack Object obj = s.pop(); s.push( obj ); // peek the stack return obj; }

Passing Object Reference public Object peek ( Stack s ); // Pass by value or pass by reference? Java has only passing by value No passing reference Each class variable is a reference The value of s is the address of a Stack object

Passing Object Reference Stack myStack; myStack = new Stack( 1000 ); ... Object x = peek( myStack ); // public Object peek ( Stack s ) // passing by value myStack Activation record return point s

Passing Object Reference Stack myStack; . . . Object x = peek( myStack ); Can function peek change the object referenced by myStack? Yes! myStack stores the reference to the stack object. All objects are passed by reference in Java! Can function peek modify the value of myStack to reference a different stack? No! Passing by value! myStack Activation record return point s

User cannot access private data. Peek by a User public Object peek ( Stack s ) { if ( s.isEmpty() ) return null; Object x = s.pop(); s.push( x ); return x; } It works. Has to pop and push! User cannot access private data.

User versus Implementer Implementer can provide a peek method More efficient Implementer has access to all private data Upgrade

public class Stack { private Object[] items; private int top = 0; ... public Object peek() if ( top == 0 ) return null; return items[top - 1]; // Do not modify the stack (top). // return items[top --]; // top is modified! // top = top – 1; }

What can you do with a Stack? As User Call the public methods! As Implementer Get count of items Search Insert item at any position Remove item from any position …

Binary Arithmetic Operations Conversion programs Infix  Prefix, Postfix Prefix  Infix, Postfix Postfix  Infix, Prefix Evaluation programs Using operator stack Using operand stack Using both Easiest program Evaluate postfix expressions Operand stack

Evaluating RPN Using Stack 4 / 5 1 3 + * - ------------ ------------ 3 5 4 * - ------------------------ 3 20 - -17 Process tokens in order If the token is an operand Push it If the token is an operator Pop two operands Perform the operation Push the result Stop if invalid at any time

Evaluating RPN Stack: LIFO Just what we need here! 6 4 7 5 10 7 5 -3 5 5 7 4 6 + - 8 3 / * 9 * + 7 4 6 + - 8 3 / * 9 * + 4 6 + - 8 3 / * 9 * + 6 + - 8 3 / * 9 * + + - 8 3 / * 9 * + 8 3 / * 9 * + / * 9 * + * 9 * + 9 * + * + + -49 6 4 7 5 10 7 5 -3 5 3 8 -3 5 2 -3 5 -6 5 9 -6 5 -54 5 -49 4 + 6 7 - 10 8 / 3 -3 * 2 -6 * 9 5 + (-54) Stack: LIFO Just what we need here!

Initialize an operand stack, s Set valid  true While valid and more tokens to process read token if token is operand s.push(token) else if token is operator if s empty then valid  false else op2  s.pop() if s empty then valid  false else op1  s.pop() if divide by 0 then valid  false else s.push( op1 <operator> op2 ) else valid  false If s empty then valid  false Else Answer  s.pop() if s not empty then valid  false

Invalid Expression 5 + Second pop fails – check for empty before calling pop + First pop fails – check for empty before before calling pop 4 0 / Divide by 0 3 4 Stack not empty at the end – check for empty at the end 3 4 B Bad token Empty expression Popping answer fails – check for empty at the end

Invalid Expression What about an expression consisting of a single operand? 243 It is valid! An expression can be put on the right hand side of an assignment. X = 243; // valid! X = ; // Invalid!

Quiz 3 Monday, October 22

Lab 6 Can assume the commands you need Specify input and the corresponding output No assert!

Test Case 1 Add a Golfer to an Empty League Description Command Expected result / output 1.1 Add a golfer to empty golf league Print Add Frank Regular 75 All members of the League: Regular Member Frank was added Regular Member Frank: 0,75,75,75,75,75 Test Case Description Command Expected result / output 1.2 Add a senior golfer to empty golf league . . . Test Case Description Command Expected result / output 1.3 Add a youth golfer to empty golf league . . .

Lab 6 Submission UserName_Lab6.doc(x) To make corrections, Only second submission accepted K:\Courses\CSSE\yangq\cs2430\1Dropbox