Python Language
연결된 목록
수색…
소개
링크 된 목록은 각 노드가 참조 및 값으로 구성된 모음입니다. 노드는 참조를 사용하여 시퀀스로 연결됩니다. 링크 된 목록을 사용하여 목록, 스택, 대기열 및 연관 배열과 같은 더 복잡한 데이터 구조를 구현할 수 있습니다.
단일 연결된 목록 예제
이 예제는 내장리스트 오브젝트와 같은 많은 메소드를 가진 링크리스트를 구현한다.
class Node:
def __init__(self, val):
self.data = val
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, val):
self.data = val
def setNext(self, val):
self.next = val
class LinkedList:
def __init__(self):
self.head = None
def isEmpty(self):
"""Check if the list is empty"""
return self.head is None
def add(self, item):
"""Add the item to the list"""
new_node = Node(item)
new_node.setNext(self.head)
self.head = new_node
def size(self):
"""Return the length/size of the list"""
count = 0
current = self.head
while current is not None:
count += 1
current = current.getNext()
return count
def search(self, item):
"""Search for item in list. If found, return True. If not found, return False"""
current = self.head
found = False
while current is not None and not found:
if current.getData() is item:
found = True
else:
current = current.getNext()
return found
def remove(self, item):
"""Remove item from list. If item is not found in list, raise ValueError"""
current = self.head
previous = None
found = False
while current is not None and not found:
if current.getData() is item:
found = True
else:
previous = current
current = current.getNext()
if found:
if previous is None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
else:
raise ValueError
print 'Value not found.'
def insert(self, position, item):
"""
Insert item at position specified. If position specified is
out of bounds, raise IndexError
"""
if position > self.size() - 1:
raise IndexError
print "Index out of bounds."
current = self.head
previous = None
pos = 0
if position is 0:
self.add(item)
else:
new_node = Node(item)
while pos < position:
pos += 1
previous = current
current = current.getNext()
previous.setNext(new_node)
new_node.setNext(current)
def index(self, item):
"""
Return the index where item is found.
If item is not found, return None.
"""
current = self.head
pos = 0
found = False
while current is not None and not found:
if current.getData() is item:
found = True
else:
current = current.getNext()
pos += 1
if found:
pass
else:
pos = None
return pos
def pop(self, position = None):
"""
If no argument is provided, return and remove the item at the head.
If position is provided, return and remove the item at that position.
If index is out of bounds, raise IndexError
"""
if position > self.size():
print 'Index out of bounds'
raise IndexError
current = self.head
if position is None:
ret = current.getData()
self.head = current.getNext()
else:
pos = 0
previous = None
while pos < position:
previous = current
current = current.getNext()
pos += 1
ret = current.getData()
previous.setNext(current.getNext())
print ret
return ret
def append(self, item):
"""Append item to the end of the list"""
current = self.head
previous = None
pos = 0
length = self.size()
while pos < length:
previous = current
current = current.getNext()
pos += 1
new_node = Node(item)
if previous is None:
new_node.setNext(current)
self.head = new_node
else:
previous.setNext(new_node)
def printList(self):
"""Print the list"""
current = self.head
while current is not None:
print current.getData()
current = current.getNext()
사용법은 빌트인 목록과 비슷합니다.
ll = LinkedList()
ll.add('l')
ll.add('H')
ll.insert(1,'e')
ll.append('l')
ll.append('o')
ll.printList()
H
e
l
l
o
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow