repl.it
Python

No description

fork
loading
Files
  • main.py
main.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#Define stacks
class myStack:
  def __init__(self, maxsize):
    self.maxsize = maxsize
    self.array = []
    for i in range(maxsize): #Creates the array
      self.array.append("")
    self.size = 0
    self.start = 0
    self.end = 0
    
  #Check if empty
  def isempty(self):
    if self.size == 0:
      return True
    else:
      return False

  #Check if full
  def isfull(self):
    if self.size == self.maxsize:
      return True
    else:
      return False
  
  #Push (add) to the end of the stack
  def push(self, item):
    if not self.isfull():
      self.array[self.end] = item
      self.start += 1
      self.size += 1
    else:
      print("/nI regret to inform that the stack is full.")

  #Pop (remove) from the start of the stack
  def pop(self, item):
    if not self.isempty():
      print("\nRemoving", self.array[self.start], "from the stack.")
      self.array[self.start] = ("")
      self.start -= 1
      self.size -= 1
      if self.size != 0:
        self.start += 1
    else:
      print("\nI regret to inform that the stack is empty.")

  def peek(self, item):
    if self.array[self.start] == 0:
      print("The top value is empty.")
    else:
      print("The top value is", self.array[self.start])

  #Display stack
  def display(self):
    print("Stack: ")
    for obj in self.array:
      print(obj)
      
stack = myStack(6)
stack.push("Riesssss")
stack.push("Ruscc")
stack.push("iDoc")
stack.push("Jeffe")
stack.push("DylMan")
stack.push("Feegs")
stack.pop("")
stack.display()
Fetching token
?