Stack is a collection based on the principle of adding elements and retrieving them in the opposite order
What is STACK?
Stack Operations
Applications
Built-in Stack
Downloadable Resources
4. Stacks
• stack: A collection based on the principle of adding elements
and retrieving them in the opposite order.
– Last-In, First-Out ("LIFO")
– Elements are stored in order of insertion.
• We do not think of them as having indexes.
– Client can only add/remove/examine
the last element added (the "top").
• basic stack operations:
– push: Add an element to the top.
– pop: Remove the top element.
– peek: Examine the top element.
stack
top 3
2
bottom 1
pop, peekpush
5. Stacks in computer science
• Programming languages and compilers:
– method calls are placed onto a stack (call=push, return=pop)
– compilers use stacks to evaluate expressions
• Matching up related pairs of things:
– find out whether a string is a palindrome
– examine a file to see if its braces { } match
– convert "infix" expressions to pre/postfix
• Sophisticated algorithms:
– searching through a maze with "backtracking"
– many programs use an "undo stack" of previous operations
method3
return var
local vars
parameters
method2
return var
local vars
parameters
method1
return var
local vars
parameters
6. Class Stack
Stack() constructs a new stack with elements
push(value) places given value on top of stack
pop() removes top value from stack and returns it;
throws EmptyStackException if stack is empty
peek() returns top value from stack without removing it;
throws EmptyStackException if stack is empty
size() returns number of elements in stack
isEmpty() returns true if stack has no elements
12. Stack limitations/idioms
• You cannot loop over a stack in the usual way.
Stack s = new Stack(10);
...
for (int i = 0; i < s.size(); i++) {
do something with s.get(i);
}
• Instead, you pull elements out of the stack one at a time.
– common idiom: Pop each element until the stack is empty.
// process (and destroy) an entire stack
while (!s.isEmpty()) {
do something with s.pop();
}
16. Different types of Stack
• Stack can be implement for different data types
16
Change the type
Int, float, char , double
etc
17. Object Stack
• Stack can also implement for a class object
– Date
– Item
– Student
• Re implement all the methods
17
18. Example 2: Reversing (palindromic)
• A palindromic number or numeral
palindrome is a number that remains the
same when its digits are reversed. Like
16461 Write a Java program to check given
number is palindrome or NOT.
• Create new Stack class (char Stack) and
improve your program to check given word
is palindrome or NOT.
18
19. Example 3: Delimiter Matching
• The delimiters are the braces { and }, brackets [ and
], and parentheses ( and ).
• Each opening or left delimiter should be matched by
a closing or right delimiter; that is, every { should be
followed by a matching } and so on.
• Example
– c[d] // correct
– a{b[c]d}e // correct
– a{b(c]d}e // not correct; ] doesn’t match
– a[b{c}d]e} // not correct; nothing matches final
– a{b(c) // not correct; nothing matches opening {
19
24. Sample Questions
a) Discuss the Abstract Data Type with considering a Stack
b) What are the practical examples for the usage of Stack, and
Leaner List.
c) Write suitable C++ code (functions) to implement the
following stack Operations (Use Array based stack to
represent integer values )
– Stack()
– Puch(value)
– Pop()
– isEmpty()
– isFull()
– [5 marks]
• Use the above Stack class and write a C++ program to read
decimal number and convert it into Octal.
24