-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.py
More file actions
30 lines (21 loc) · 758 Bytes
/
stack.py
File metadata and controls
30 lines (21 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
class Stack(object):
"""Implements a stack data structure"""
def __init__(self):
self.top = None
def push(self, data):
"""Take a data element and put it on the top of the stack"""
self.top = Item(data, self.top)
def pop(self):
"""Remove the top item from the stack, and return the value of it"""
prevTop = self.top
try:
self.top = self.top.next_item
except AttributeError:
raise ValueError("The stack is empty")
return prevTop.data
class Item(object):
"""Wrapper for a data item that get pushed on the stack"""
def __init__(self, data, next_item=None):
self.data = data
self.next_item = next_item