IT 4043 Data Structures and Algorithms Budditha Hettige Department of Computer Science 1
Syllabus Introduction to DSA Abstract Data Types List Operation Using Arrays Stacks Queues Recursion Link List Sorting Searching Algorithms Analysis 2
STACK retrieves elements in reverse order as added 3
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"). push pop, peek basic stack operations: push: Add an element to the top. pop: Remove the top element. peek: Examine the top element. top 3 2 bottom 1 stack
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 method3 method2 method1 return var local vars parameters return var local vars parameters return var local vars parameters Sophisticated algorithms: searching through a maze with "backtracking" many programs use an "undo stack" of previous operations
Class Stack Stack() constructs a new stack with elements push(value) places given value on top of stack pop() peek() size() isempty() removes top value from stack and returns it; throws EmptyStackException if stack is empty returns top value from stack without removing it; throws EmptyStackException if stack is empty returns number of elements in stack returns true if stack has no elements
Stack Operations 7
Array Based Stack 6 1 9 8
Stack Class 9
Stack Class 10
Stack Class 11
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(); }
Application 13
Example 1: Binary Conversion Write a Java Program to read decimal number and convert it into binary. (Use Stack and print the binary value ) 14
Model Answer 15
Different types of Stack Stack can be implement for different data types Change the type Int, float, char, double etc 16
Object Stack Stack can also implement for a class object Date Item Student Re implement all the methods 17
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
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
Delimiter Matching Let s see what happens on the stack for a typical correct string: a{b(c[d]e)f} 20
Answer 21
Built-in Stack class 22
Stack Example (String) 23
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