Data Structure (Stack)

Preview:

Citation preview

STACK

Adam M.B.

DEFINITION

Stack

Stack is data structure whose its

elemen can be added and taken only

from last position (top).

Components of Stack• Top is a variable which refers to last

position in stack.

• Element is component which has data.

• MaxStack is variable that describes maximum number of elements in a stack.

DECLARATION

Declaration as ArrayKamus:

Const

MaxStack = value {value is integer number}

Type

NamaStack = array [1..MaxStack] of tipedata

Stack : NamaStack

Top : integer {pointer of stack}

Declaration as ListKamus:

Type

NamaPointer = ↑Stack Stack = Record

< MedanData : TipeData,

MedanSambungan : NamaPointer >

EndRecord

Top : NamaPointer {pointer of stack}

OPERATION

Operation• Initialization

• Empty Operation

• Full Operation (array)/One Node

Operation (Linked List)

• Push

• Pop

Main Operation

PUSH

STACK

Take data from element

in stack

POPAdd data to element in

stack.

Kinds of Operation

•Stack Operation in array form

•Stack Operation in Linked list form

STACK OPERATION IN ARRAY FORM

Operation that give a initial value for top pointer in stack with the following rules:• Give 0 if the first element starts

from 1• Give -1 if the first element starts

from 0

Initialization

Operation that returns true if the top pointer have 0 or -1 as its value (depend on initialization) or returns

false to the contrary.

Empty Operation

Operation that returns true if top have reached maximum array or maximum array -1 (depend on

initialization) or returns false if top is not equal to maximum array.

Full Operation

Steps in push operation:• Stack can be added when it’s not

full• Add the top pointer with 1 • Stack element, which was refered

by top pointer, is filled with new data.

Push

Push

1

2

3

4

0Top

Push(Top,Stack,8)Push(Top,Stack,3)Push(Top,Stack,5)Push(Top,Stack,1)Push(Top,Stack,7)

8

3

5

1

“Stack Penuh”

Stack

Steps in pop operation:• Stack can be pop when its

elements is not empty.• Element that have taken out from

stack is saved in a variable.• Substract the top pointer with 1.

Pop

Pop

1

2

3

4

0Top

Pop(Top,Stack,Elemen)Pop(Top,Stack,Elemen)Pop(Top,Stack,Elemen)Pop(Top,Stack,Elemen)Pop(Top,Stack,Elemen)

8

3

5

1

“Stack Kosong”

Stack 1Elemen

538

STACK OPERATION IN LINKED LIST

FORM

Prepare stack by giving null value to the top pointer in stack.

Initialization

Operation that returns true if the top

pointer isn’t null or returns false to the contrary.

Empty Operation

Operation that returns true if right connection field has null value (stack

only has one node) or returns false to the contrary.

One Node Operation

Steps of push operation in linked list

form is similar with front insertion.

Push

Push Push(Top,8)

baru 8

Top

Push(Top,3)

baru 3 8

Top

Push

Push(Top,5)

baru 3 8

Top

5

Steps of pop operation in linked list

form is similar with front deletion.

Pop

Pop Pop(Top,Elemen)

Phapus

3 8

Top

5

Elemen

Pop Pop(Top,Elemen)

Phapus

3 8

Top

Elemen

Pop Pop(Top,Elemen)

Phapus

8

Top

Elemen

Do the task 6.27 until 6.29 in page 210 at Data Structures book, seymour.

Exercise

Contact Person:Adam Mukharil Bachtiar

Informatics Engineering UNIKOMJalan Dipati Ukur Nomor. 112-114 Bandung 40132

Email: adfbipotter@gmail.comBlog: http://adfbipotter.wordpress.com

Copyright © Adam Mukharil Bachtiar 2012

GRACIASTHANK YOU

Recommended