Queue in Linked Structure - Python

이세진·2022년 4월 3일
0

Computer Science

목록 보기
36/74

생성일: 2021년 10월 28일 오후 8:38

QueType.py

class NodeType:
    """ Node Type """
    def __init__(self, item):
        self.info = item
        self.next = None

class QueType:
    def __init__(self):
        self.front = None
        self.rear = None

    def make_empty(self):
        while self.front != None:
            tempPtr = self.front
            self.front = self.front.next
            del tempPtr
        self.rear = None

    def is_full(self):
        try:
            location = NodeType("test")
            del location
            return False
        except:
            return True

    def is_empty(self):
        return self.front == None

    def enqueue(self, item):
        '''[7]'''
        if(self.is_full()):
            return "Queue is full"
        newNode = NodeType(item)
        if(self.rear == None):
            self.front = newNode
        else:
            self.rear.next = newNode
        self.rear = newNode

    def dequeue(self):
        '''[8]'''
        if(self.is_empty()):
            return "Queue is empty"
        tempPtr = self.front
        item = tempPtr.info
        self.front = self.front.next
        if(self.front == None):
            self.rear = None
        del tempPtr
        return item

testQueue.py

import os
from QueType import *

if __name__ == '__main__':
    my_queue = QueType()
    
    for i in range(5):
        number = int(input("enter the number: "))
        my_queue.enqueue(number)
    
    your_queue = QueType()
    
    for i in range(5):
        number = int(input("enter the number: "))
        your_queue.enqueue(number)
        
    print()
    print(my_queue.is_full())
    print()
    
    while (True):
        if (my_queue.is_empty() == True):
            break
        else:
            print(my_queue.dequeue())
    
    print()
    print(my_queue.is_full())
    print(my_queue.is_empty())
    print()
    print()
    
    
    print(your_queue.is_full())
    print()
    
    your_queue.make_empty()
    
    print(your_queue.is_full())
    print(your_queue.is_empty())
profile
나중은 결코 오지 않는다.

0개의 댓글