You are on page 1of 5

Stack

Introduction

A stack is a data structure that stores a linear collection of items with access limited to a last-in
�rst-out order. Adding and removing items is restricted to one end known as the top of the
stack. An empty stack is one containing no items.

1. Stack(): Creates a new empty stack.

2. isEmpty(): Returns a boolean value indicating if the stack is empty.

3. length (): Returns the number of items in the stack.

4. pop(): Removes and returns the top item of the stack, if the stack is not empty. Items
cannot be popped from an empty stack. The next item on the stack becomes the new top
item.

5. peek(): Returns a reference to the item on top of a non-empty stack without removing it.
Peeking, which cannot be done on an empty stack, does not modify the stack contents.

�. push( item ): Adds the given item to the top of the stack.

PUSH into a Stack

from collections import deque


myStack = deque()
myStack.append('a')
myStack.append('b')
myStack.append('c')
myStack

deque(['a', 'b', 'c'])

myStack.append('d')
myStack

deque(['a', 'b', 'c', 'd'])

!pip install pythonds

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/


Requirement already satisfied: pythonds in /usr/local/lib/python3.8/dist-packages (1.2.1)
 0s completed at 8:15 PM
from pythonds.basic import Stack

s=Stack()
print(s.isEmpty())

True

s.push(4)
s.push('dog')
print(s.peek())

dog

s.push(True)
print(s.size())

print(s.isEmpty())

False

s.push(8.4)
print(s.pop())

8.4

print(s.pop())

True

print(s.size())

#ringkasan

s=Stack()
print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())
True
dog
3
False
8.4
True
2

class Stack:

def __init__(self):
self.stack = []

def add(self, dataval):


# Use list append method to add element
if dataval not in self.stack:
self.stack.append(dataval)
return True
else:
return False
# Use peek to look at the top of the stack
def peek(self):
return self.stack[-1]

AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.peek()
print(AStack.peek())
AStack.add("Wed")

( )
AStack.add("Thu")
print(AStack.peek())

Tue
Thu

POP from a Stack

from collections import deque


myStack = deque()
myStack.append('a')
myStack.append('b')
myStack.append('c')
myStack

deque(['a', 'b', 'c'])

myStack.pop()

'c'
c

myStack.pop()

'b'

myStack.pop()

'a'

class Stack:

def __init__(self):
self.stack = []

def add(self, dataval):

# Use list pop method to remove element

if dataval not in self.stack:


self.stack.append(dataval)
return True
else:
return False

# Use list pop method to remove element

def remove(self):
if len(self.stack) <= 0:
return ("No element in the Stack")
else:
return self.stack.pop()

BStack = Stack()
BStack.add("Mon")
BStack.add("Tue")
BStack.add("Wed")
BStack.add("Thu")
print(BStack.remove())
print(BStack.remove())

Thu
Wed

EXERCISE

Create python code with the following output:

initial stack = ['N', 'O', 'F', 'T', 'P']

Elemen poppoed from stack: P T F


Stack after elements are popped: ['N', 'O']

stack = ['N', 'O', 'F', 'T', 'P']

# Print the initial stack


print("Initial stack:", stack)

# Pop three elements from the stack


for i in range(3):
popped_element = stack.pop()
print("Element popped from stack:", popped_element)

# Print the stack after elements are popped


print("Stack after elements are popped:", stack)

Initial stack: ['N', 'O', 'F', 'T', 'P']


Element popped from stack: P
Element popped from stack: T
Element popped from stack: F
Stack after elements are popped: ['N', 'O']

saya manulis coding langsung menggunakan from i hingga langsung mengeluarkan variabel P,
T, F yang terletak pada bagian paling atas stack

Colab paid products - Cancel contracts here

You might also like