본문 바로가기

공부/TIL-D

머지소트, 스택,큐 ,연결리스트 코드

# adapter pattern
class Stack:
    def __init__(self):
        self.container=list()
        
    def empty(self):
        if not self.container:
            return True
        return False
    
    #래퍼 함수(wrapeer function)
    def push(self, data):
        self.container.append(data)
    
    def pop(self):
        return self.container.pop()
    
    def peek(self):
        return self.container[-1]
# adapter pattern
class Queue:
    def __init__(self):
        self.container=list()
        
    def empty(self):
        if not self.container:
            return True
        return False
    
    #래퍼 함수(wrapeer function)
    def enqueue(self, data):
        self.container.append(data)
    
    def dequeue(self):
        return self.container.pop(0)
    
    def peek(self):
        return self.container[0]
# adapter pattern
class Stack:
    def __init__(self):
        self.container=list()
        
    def empty(self):
        if not self.container:
            return True
        return False
    
    #래퍼 함수(wrapeer function)
    def push(self, data):
        self.container.append(data)
    
    def pop(self):
        return self.container.pop()
    
    def peek(self):
        return self.container[-1]
# class Queue:
#     def __init__(self):
#         self.first=Stack()
#         self.second=Stack()
        
#     def empty(self):
#         if not self.first:
#             return True
#         elif not self.second:
#             return True
#         else:
#             return False
    
#     def enqueue(self, data):
#         self.first.push(data)
    
#     def dequeue(self):
#         a = empty(self)
#         if self.second == a:
#             shift()
#         else:
#             return self.second.pop(data)
    
#     def peek(self):
#         b = empty(self)
#         if self.second == b:
#             shift()
#         else:
#             return self.second[0]
#     def shift(self):
#         self.second.push(data)
#         self.first.pop(data)
class Queue:
    def __init__(self):
        self.first=Stack()
        self.second=Stack()
        
    def empty(self):
        if self.first.empty() and self.second.empty():
            return True
        return False
    
    def enqueue(self, data):
        self.first.push(data)
    
    def dequeue(self):
        if self.empty():
            return None
        if self.second.empty(): # 이것의 포인트
            while not self.first.empty():
                self.second.push(self.first.pop())
            
        return self.second.pop()


 

반응형

'공부 > TIL-D' 카테고리의 다른 글

bottom-up의 괴로움  (0) 2019.04.29
프로와 아마추어의 차이  (0) 2019.04.28
오늘배운거 깊게  (0) 2019.04.25
하노이타워  (0) 2019.04.24
프로세스/스레드 CONTEXT SWITCHING  (0) 2019.04.23