Good evening Continue to receive various advice from everyone Two weeks are about to pass.
Thank you so much. m (_ _) m
First, prepare a stack, modify it and bring it to the queue I will take an approach.
stack.py
class Stack:
class full(Exception):
pass
def __init__(self,size):
#Stack body
self.str = []
#Specify the number of data that can be stacked
self.size = size
def push(self,value):
#Stack body>Exception handling for the specified number of data
if len(self.str) >= self.size:
raise Stack.full
#Push if there is no problem
self.str.append(value)
def pop(self):
print(self.str.pop())
def view(self):
print(self.str)
x = int(input("stack size is "))
test = Stack(x)
while True:
num = int(input("1.push 2.pop: "))
if num == 1:
x = int(input("push data is "))
try:#Normal processing
test.push(x)
test.view()
except:#Exception handling
print("Full!")
elif num == 2:
try:#Normal processing
test.pop()
test.view()
except:#Exception handling
print("Empty!")
else:
break
No, it's a description I couldn't think of two weeks ago (; ´ ・ ω ・) Now, how do you get it to the queue from here? What was the difference between a stack and a queue in the first place? .. Basically, the movement of pop is different. As shown in the figure, in the case of a queue, the data saved first is taken out. If you do push / pop randomly, the contents will be messed up, so I don't know where the first data is.
There is a concept of ring buffer as a countermeasure, Queues are realized by managing where is the first. .. .. I'm using python Isn't it easy? ?? ??
So, first, let's review the nature of pop.
test.py
num = [3,2,1]
for i in range(3):
print(f"num[{i}] = {num[i]}")
##Execution result##
#num[0] = 3#
#num[1] = 2#
#num[2] = 1#
############
Let's put pop in. I want to queue, so I set it to pop (0) Let's retrieve the First data.
test.py
num = [3,2,1]
num.pop(0)
for i in range(2):
print(f"num[{i}] = {num[i]}")
##Execution result##
#num[0] = 2#
#num[1] = 1#
############
[Wall] * ゚) Hmm? Pop the first num [0], that is, take it out, As a result of the deletion, the remaining elements are left-justified.
That's right, the elements that remain after deleting the beginning are It will also be relocated from 0. What, if you do it yourself, you don't need a ring buffer !?
Queues are basically the same as stacks, fetching data, You only need to write once per address. Therefore, pop () defined in the above stack, the description to be extracted from the end If you change to pop (0), the description that is always taken out from the beginning, it will be a queue.
Queue??.py
class Stack:
class full(Exception):
pass
def __init__(self,size):
self.str = []
self.size = size
def push(self,value):
if len(self.str) >= self.size:
raise Stack.full
self.str.append(value)
def pop(self):
print(self.str.pop(0)) #Change only here!!Change before) pop()After change) pop(0)
def view(self):
print(self.str)
x = int(input("stack size is "))
test = Stack(x)
while True:
num = int(input("1.push 2.pop: "))
if num == 1:
x = int(input("push data is "))
try:
test.push(x)
test.view()
except:
print("Full!")
elif num == 2:
try:
test.pop()
test.view()
except:
print("Empty!")
else:
break
Below are the execution results.
push data is 4 #Push 4
[1, 2, 3, 4] #Memory contents after push
1.push 2.pop: 2#2.Select pop
1 #str[0]Pop the 1 stored in
[2, 3, 4] #Memory contents after pop
1.push 2.pop: 2#2.Select pop
2 #str[0]Pop the 1 stored in
[3, 4] #Memory contents after pop
1.push 2.pop: 2
3
[4]
1.push 2.pop: 2
4
[]
1.push 2.pop: 2
Empty!
With this kind of feeling, I was able to easily realize the cue. Hmmm, but make a link buffer properly and explain I feel that it is better for me to put it on. Alright, let's do it! !! Someday. ..
Recommended Posts