Python 2.7 Single Linked list 구현
참고자료 : starmooc.kr kaist 문일철 교수 데이터구조 및 분석 인강
class Node:
value = ''
nextnode = ''
binhead = False
bintail = False
def __init__(self, value='', nextnode='', binhead=False, bintail=False):
self.value = value
self.nextnode = nextnode
self.binhead = binhead
self.bintail = bintail
def getValue(self):
return self.value
def setValue(self, value=''):
self.value = value
def getNext(self):
return self.nextnode
def setNext(self, nextnode=''):
self.nextnode = nextnode
def isTail(self):
return self.bintail
def isHead(self):
return self.binhead
class SinglyLinkedlist:
nodehead = ''
nodetail = ''
size = 0
def __init__(self):
self.nodetail = Node(bintail = True)
self.nodehead = Node(binhead = True, nextnode = self.nodetail)
def insertAt(self, objInsert, idxInsert):
nodeNew = Node(value=objInser)
nodePrev = self.get(idxInsert - 1)
nodeNext = nodePrev.getNext()
nodePrev.setNext(nodeNew)
nodeNew.setNext(nodeNext)
self.size = self.size+1
def removeAt(self, idxRemove):
nodePrev = self.get(idxRemove - 1)
nodeRemove = nodePrev.getNext()
nodeNext = nodeRemove.getNext()
nodePrev.setNext(nodeNext)
self.size = self.size - 1
return nodeRemove.getValue()
def get(self, idxRetrieve):
nodeReturn = self.nodeHead
for itr in range(idxRetreve + 1):
nodeReturn = nodeReturn.getNext()
return nodeReturn
def printStatus(self):
nodeCurrent = self.nodehead
while nodeCurrent.getNext().isTail() == False:
nodeCurrent = nodeCurrent.getNext()
print nodeCurrent.getValue(),
def getSize(self)
return self.size
'프로그래밍 > Python' 카테고리의 다른 글
[Python]sqlalchemy 사용법 (0) | 2022.12.07 |
---|---|
[PYTHON] 파이썬 함수 매개변수 (0) | 2022.08.11 |
[Python] flask_restful fields (0) | 2021.10.08 |
창원시 버스 API 사용하기 (1) | 2016.02.22 |