CSC 205 – Java Programming II

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Stacks Chapter 11.
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.
CMPT 225 Stacks.
Chapter 3 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 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 removed.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 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 removed.
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 removed.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
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.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
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,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Data Structures & Algorithms
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CC 215 Data Structures Stack ADT
Stacks.
Stacks.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Data Structures
structures and their relationships." - Linus Torvalds
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Stacks.
Java Collections Framework
Stacks.
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks.
structures and their relationships." - Linus Torvalds
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CSC 205 – Java Programming II Lecture 30 April 3, 2002

Stack A stack is an abstract data type (ADT) that contains a sequence of elements and honors the last-in-first-out (LIFO) rule. Conceptually, the only element that can be removed, accessed, modified is the one which was most recently inserted. Pop and push are the two basic stack operations

Stack PUSH top POP last-in-first-out

Examples Green Red Yellow Red Yellow Green Red Yellow pop push green

Java Stack Class The Java Stack class represents a stack of objects. It outdated the Java Collections Framework Available since JDK1.0 It extends class Vector with 5 additional operations. With its top at the index size()-1 top

Java Stack API boolean empty() Tests if this stack is empty. Object peek()           Looks at the object at the top of this stack without removing it from the stack. Object pop()           Removes the object at the top of this stack and returns that object as the value of this function. Object push(Object item)           Pushes an item onto the top of this stack. int search(Object o)           Returns the 1-based position where an object is on this stack.

Example Checking for balanced braces The braces are balanced if abc{defg{ijk}{l{mn}}op}qr balanced abc{def}}ghij{kl}m not balanced The braces are balanced if Each time you encounter a }, it matches an already encountered { When you reach the end of the string, you have matched each {

Pseudocode Solution A simplified solution in pseudocode while (not at the end of the string) { if (the next character is a ‘{’) { stack.push(‘{’) } else if (the character is a ‘}’) openBrace = stack.pop()

Examples {a{b}c} {a{bc} {ab}c} { { { { { { { push “}” pop Stack empty balanced {a{b}c} { { { {a{bc} push “}” pop Stack not empty not balanced { push “}” pop Stack empty balanced {ab}c}

Problems w/ The Stack Class All the Vector methods can be used For examples myBookStack.get(7); myBookStack.remove(0); //the element right at the “bottom” can be removed!

Convenient or Risky? myBookStack.get(7); myBookStack.remove(0);

Alternative Designs Implement a RealStack class Use a LinkedList object as its only field Provide push and pop methods public Object push(Object o) { list.add(o); return o; } public Object pop(Object o) { return list.removeLast(o);

Activation Stack Stacks are used almost exclusively by computer systems An activation stack is used by a compiler /interpreter to save the method information when a method is called The stack is part of the main memory. Other parts include a heap to hold data

Activation Records Each activation record includes The return address The value of each argument The values of the method’s other local variables

Decimal To Binary public void processInput(String s) { writeBinary(Integer.parseInt(s)); //RA1 } public void writeBinary(int n) { if (n<=1) throw new IllegalArgumentException(); gui.println(n); else { writeBinary(n/2); //RA2 gui.println(n%2);

Example RA2 1 RA2 RA2 RA2 3 3 3 RA1 RA1 RA1 RA1 RA1 6 6 6 6 6