Chapter 3 Topik 02 Operasi Stack PDF

Summary

This document explains data structures, specifically stacks, using Python examples. It details the operations involved in creating, manipulating, and using stacks in Python, such as checking for emptiness, getting size, adding items, and removing items. The presentation highlights the stack as a way to manage data in a last-in, first-out (LIFO) order, and details how these operations are performed in Python, showcasing code examples.

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()

Use Quizgecko on...
Browser
Browser