Chapter 3 Topik 02 Operations Stack PDF
Document Details
Uploaded by Deleted User
Willy Prihartono
Tags
Summary
This document presents an introduction to stacks, a data structure in computer science useful in algorithms. It covers the theoretical aspects, with emphasis on Python code examples for stack implementation. The document focuses on core stack operations in Python, including size, top, push, and pop methods.
Full Transcript
STRUKTUR DATA Chapter 2 : Non Primitive Data Structure Topic 2 : Stack in Phyton Presented by : Willy Prihartono Stack ADT: Stack push pop,top Sebuah Stack adalah kumpulan benda di mana hanya benda yang most recently inser...
STRUKTUR DATA Chapter 2 : Non Primitive Data Structure Topic 2 : Stack in Phyton Presented by : Willy Prihartono Stack ADT: Stack push pop,top Sebuah Stack adalah kumpulan benda di mana hanya benda yang most recently inserted dapat diakses. Bayangkan setumpuk koran. Benda yang paling terakhir ditambahkan ditaruh di atas Most tumpukan (top). recent Operasi pada Stack membutuhkan waktu konstan (O(1)). Contoh Interface stack : void push(Benda x); Least Benda pop(); recent Benda top(); Stack Stack Stack in Phyton empty() – Returns whether the stack is empty – Time Complexity: O(1) size() – Returns the size of the stack – Time Complexity: O(1) top() / peek() – Returns a reference to the topmost element of the stack – Time Complexity: O(1) push(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity: O(1) pop() – Deletes the topmost element of the stack – Time Complexity: O(1) Stack Implementation in Phyton # Creating a stack # Creating an empty stack def create_stack(): def check_empty(stack): stack = [] return len(stack) == 0 return stack # Removing an element from # Adding items into the stack def push(stack, item): the stack stack.append(item) def pop(stack): print("pushed item: " + item) if (check_empty(stack)): return "stack is empty" return stack.pop()