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.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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 are Stacks? Stack implementation using arrays. Stack application.
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,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
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.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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 (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.
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.
Topic 15 Implementing and Using Stacks
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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.
Stacks and Queues Introduction to Computing Science and Programming I.
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.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
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.
CHP-3 STACKS.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
BCA II Data Structure Using C
Chapter 4 Stacks
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.
Stacks.
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
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, Queues, and Deques
CSC 205 – Java Programming II
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks.
Stacks, Queues, and Deques
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik.
Introduction to Stacks
Stack.
Introduction to Stacks
Stacks, Queues, and Deques
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 9: Stack and Queue
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

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 removed from the stack is taken from the “top” of the stack Things are removed in the reverse order from that in which they were inserted

Constructing a stack To use stacks, you need import java.util.*; There is just one stack constructor: Stack stack = new Stack();

Stack operations I stack.push(object) Adds the object to the top of the stack; the item pushed is also returned as the value of push object = stack.pop(); Removes the object at the top of the stack and returns it object = stack.peek(); Returns the top object of the stack but does not remove it from the stack

Stack operations II stack.empty() Returns true if there is nothing in the stack int i = stack.search(object); Returns the 1-based position of the element on the stack. That is, the top element is at position 1, the next element is at position 2, and so on. Returns -1 if the element is not on the stack

Stack s as Vector s The Stack class extends the Vector class Hence, anything you can do with a Vector, you can also do with a Stack However, this is not how stacks are intended to be used A “stack” is a very specific data structure, defined by the preceding operations; it just happens to be implemented by extending Vector Use only the stack operations for Stacks !

A balancing act ([]({()}[()])) is balanced; ([]({()}[())]) is not Simple counting is not enough to check balance You can do it with a stack: going left to right, If you see a (, [, or {, push it on the stack If you see a ), ], or }, pop the stack and check whether you got the corresponding (, [, or { When you reach the end, check that the stack is empty

Performing calculations To evaluate an expression, such as 1+2*3+4, you need two stacks: one for numbers, the other for operations: going left to right, If you see a number, push it on the number stack If you see an operation, If the operation stack is empty, push the operation If the top of the operation stack holds a lower precedence operation, push the operation If the top of the operation stack holds a higher precedence operation, pop it, perform the operation on the top two values in the number stack, push the result on the number stack, and push the new operation on the operation stack At the end, perform any remaining operations

Example: 1+2*3+4 1 : push 1 on number stack + : push + on op stack 2 : push 2 on number stack * : compare * to +, then push * onto op stack 3 : push 3 onto number stack + : compare + to *, pop 3, 2, and *, compute 2*3=6, push 6 onto number stack, push + onto op stack 4 : push 4 onto number stack end : pop 4, 6 and +, compute 6+4=10, push 10 ; pop 10, 1, and +, compute 1+10=11, push (top of stack) is the answer

Stacks in Java Stacks are used for local variables void methodA() { int x, y; // puts x, y on stack y = 0; methodB(); y++; } void methodB() { int y, z; // puts y, z on stack y = 5; return; // removes y, z } x y y z

The End