DOC PREVIEW
Penn CIT 591 - Stacks

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

StacksWhat is a stack?Constructing a stackStack operations IStack operations IIStacks as VectorsA balancing actPerforming calculationsExample: 1+2*3+4Stacks in JavaThe EndJan 14, 2019StacksWhat 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 insertedConstructing 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 stackStack 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 stackStacks as Vectors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 emptyPerforming 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 operationsExample: 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 1111 (top of stack) is the answerStacks in JavaStacks are used for local variablesvoid 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}xyyzThe


View Full Document

Penn CIT 591 - Stacks

Documents in this Course
Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Stacks
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Stacks and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Stacks 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?