data structures leon andretti abdillah · a stack is a data structure that allows data to be...

21
05 Stack (Tumpukan) Data Structures Leon Andretti Abdillah

Upload: others

Post on 17-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

05

Stack

(Tumpukan)

Data Structures

Leon Andretti Abdillah

Page 2: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Introduction 1/4

A stack is a list of homogenous elements in which the

addition and deletion of elements occurs only at one end,

called the top of the stack.

A stack is a data structure of ordered items such that items can be

inserted and removed only at one end.

10/04/2013 18:34:362 LeonAbdillah - DS - Stack

Page 3: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Introduction 2/4

10/04/2013 18:34:36LeonAbdillah - DS - Stack3

A stack is a data structure that allows data to be inserted (a

'push' operation), and removed (a 'pop' operation). Many

stacks also support a read ahead (a 'peek' operation), which

reads data without removing it. A stack is a LIFO-queue,

meaning that the last data to be inserted will be the first data

to be removed.

When we insert data into a stack, it is placed at the head of a

queue. This means that when we remove data, it will be in

reverse order. Adding 1,2,3,4 will return 4,3,2,1. Stacks

aren't the most frequently used data structure, but they are

extremely useful for certain tasks.

Page 4: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Introduction 3/4

10/04/2013 18:34:36LeonAbdillah - DS - Stack4

For example, in a cafeteria, the second tray in a stack of trays can be removed only if the first tray has been removed.

For another example, to get to your favorite computer science book, which is underneath your math and history books, you must first remove the math and history books. After removing these books, the computer science book becomes the top book—that is, the top element of the stack.

The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack.

Another application is an "undo" mechanism in text editors; this operation is accomplished by keeping all text changes in a stack.

Page 5: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Introduction 4/4

Various examples of stack

10/04/2013 18:34:365 LeonAbdillah - DS - Stack

Page 6: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Stack ADT (Abstract Data Structure)

6

stack: a list with the restriction that insertions/deletions can only be performed at the top/end of the list Last-In, First-Out ("LIFO") The elements are stored in order of insertion,

but we do not think of them as having indexes. The 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.

10/04/2013 18:34:37LeonAbdillah - DS - Stack

Page 7: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Stack only defines the default constructor, which creates an empty stack.

To put an object on the top of the stack, call push( ). To remove and return the top element, call pop( ). An EmptyStackException is thrown if you call pop( )when the invoking stack is empty. You can use peek( ) to return, but not remove, the top object.

The empty() method returns true if nothing is on the stack. The search() method determines whether an object exists on the stack, and returns the number of pops that are required to bring it to the top of the stack.

10/04/2013 18:34:377 LeonAbdillah - DS - Stack

Page 8: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Stack’s methods/operations

A Stack adds and removes elements on a First-In First-Out

basis.The key methods of the Stack class are push(), peek(),

pop(), empty() and search().

Operations Notes

push() adds an element to the top of the stack.

peek() returns the first element from the top of the stack without

removing it from the stack.

pop() as peek() but removes it from the stack.

empty() checks wheater there are elements in the stack or not.

search() returns the position of an element in the stack

10/04/2013 18:34:378 LeonAbdillah - DS - Stack

Page 9: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java for stack

10/04/2013 18:34:37LeonAbdillah - DS - Stack9

Java provides a stack implementation, in the form of

java.util.Stack. Stack's are a subclass of

java.util.Vector, and share some similarities. A

Vector, after all, is a queue, and a Stack is an ordered LIFO

queue.

Stacks are quite easy to use.

Here's a sample application that uses a stack of numbers.

Page 10: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 1/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack10

Pada java project yg sdh ada (StrukturData)

Create new Package (;package04)

Pada package yg baru tercipta, create new class

(;StackDemo)

Page 11: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 2/9a

10/04/2013 18:34:37LeonAbdillah - DS - Stack11

package package04;

public class StackDemo {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

Page 12: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 2/9b

10/04/2013 18:34:37LeonAbdillah - DS - Stack12

package package04;

// add two libraries after package and before ClassName

import java.util.EmptyStackException;

import java.util.Stack;

public class StackDemo {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

Page 13: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 3/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack13

Define a method showpush before main()

static void showpush(Stack st, int a) {

st.push(new Integer(a));

System.out.println("push(" + a + ")");

System.out.println("stack: " + st);

}

Page 14: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 4/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack14

Define a method showpop before main()

static void showpop(Stack st) {

System.out.print("pop -> ");

Integer a = (Integer) st.pop();

System.out.println(a);

System.out.println("stack: " + st);

}

Page 15: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 5/5

10/04/2013 18:34:37LeonAbdillah - DS - Stack15

Define a method showpeek before main()

static void showpeek(Stack st) {

System.out.print("peek -> ");

Integer a = (Integer) st.peek();

System.out.println(a);

System.out.println("stack: " + st);

}

Page 16: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 6/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack16

Define a method showpeek before main()

static void showsearch(Stack st, int b) {

int pos = st.search(b);

System.out.println("Location of " + b + " is : " + pos);

}

Page 17: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 7/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack17

Add statements in main()

public static void main(String[] args) {

// TODO Auto-generated method stub

Stack st = new Stack(); // create Stack

System.out.println("stack: " + st);

showpush(st, 42);

showpush(st, 66);

showpush(st, 99);

System.out.println("stack size: " + st.size());

Page 18: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 8/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack18

showsearch(st, 42);

showsearch(st, 66);

showsearch(st, 99);

showpeek(st);

showpop(st);

showpop(st);

showpop(st);

Page 19: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Java Example 9/9

10/04/2013 18:34:37LeonAbdillah - DS - Stack19

try {

showpop(st);

} catch (EmptyStackException e) {

System.out.println("empty stack");

}

// Or

System.out.println("Is the stack empty? "+ st.empty());

} // main()

} // class

Page 20: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Result

10/04/2013 18:34:37LeonAbdillah - DS - Stack20

Page 21: Data Structures Leon Andretti Abdillah · A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support

Summary

Stack:

LIFO

Operations

push

pop

top /peek

search

Applications (Homework)

Depth-first search

Recursive evaluation

10/04/2013 18:34:3721 LeonAbdillah - DS - Stack