96 lines
2.4 KiB
Python
96 lines
2.4 KiB
Python
class queue :
|
|
|
|
def __init__(self,size) :
|
|
self.__size = size
|
|
self.__data = [0]*size
|
|
self.__front = -1
|
|
self.__rear = -1
|
|
|
|
def isEmpty(self) :
|
|
return self.__front == -1
|
|
|
|
def isFull(self) :
|
|
return (self.__rear + 1)%(self.__size) == self.__front
|
|
|
|
def enqueue(self, value) :
|
|
if self.isFull() :
|
|
print("Queue is Full!!")
|
|
return
|
|
|
|
if self.isEmpty() :
|
|
self.__front = 0
|
|
|
|
self.__rear = (self.__rear + 1) % self.__size
|
|
self.__data[self.__rear] = value
|
|
print("삽입 완료")
|
|
|
|
def dequeue(self) :
|
|
if self.isEmpty() :
|
|
print("Queue is Empty!")
|
|
return -1
|
|
|
|
value = self.__data[self.__front]
|
|
|
|
if self.__front == self.__rear :
|
|
self.__front = -1
|
|
self.__rear = -1
|
|
else :
|
|
self.__front = (self.__front + 1) % self.__size
|
|
|
|
print(value)
|
|
|
|
return value
|
|
|
|
def peek(self) :
|
|
if self.isEmpty() :
|
|
print("Queue is Empty!")
|
|
return -1
|
|
|
|
value = self.__data[self.__front]
|
|
|
|
print(value)
|
|
|
|
return value
|
|
|
|
def info(self) :
|
|
if self.isEmpty() :
|
|
print("Queue is Empty!, Max Size is : {0}".format(self.__size))
|
|
return
|
|
|
|
i = self.__front
|
|
result = "Queue List : [ "
|
|
while True:
|
|
result += str(self.__data[i])
|
|
if i == self.__rear :
|
|
result += " ]"
|
|
break
|
|
|
|
result += ", "
|
|
i = (i + 1) % self.__size
|
|
|
|
print("Max Size is : {0}".format(self.__size))
|
|
print(result)
|
|
|
|
q = queue(int(input("원형큐의 최대사이즈를 입력해주세요 : ")))
|
|
|
|
endFlag = True
|
|
|
|
while endFlag :
|
|
opcode = input("명령어 목록 : enqueue [num] | dequeue | peek | info | exit\n")
|
|
|
|
if "enqueue" in opcode :
|
|
if opcode.split()[0] != "enqueue" :
|
|
print("제대로 입력해주세요 enqueue와 정수를 입력받습니다.")
|
|
continue
|
|
q.enqueue(int(opcode.split()[1]))
|
|
elif opcode == "dequeue" :
|
|
q.dequeue()
|
|
elif opcode == "peek" :
|
|
q.peek()
|
|
elif opcode == "info" :
|
|
q.info()
|
|
elif opcode == "exit" :
|
|
endFlag = False
|
|
print("Exit")
|
|
else :
|
|
print("제대로 입력해주세요") |