자료구조와 알고리즘 실습 | Data Structures & Algorithms Lab
노드 5개를 생성하여 단순 연결 리스트를 만듭니다
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "다현" node2 = Node() node2.data = "정연" node1.link = node2 node3 = Node() node3.data = "쯔위" node2.link = node3 node4 = Node() node4.data = "사나" node3.link = node4 node5 = Node() node5.data = "지효" node4.link = node5 print(node1.data, end = ' ') print(node1.link.data, end = ' ') print(node1.link.link.data, end = ' ') print(node1.link.link.link.data, end = ' ') print(node1.link.link.link.link.data, end = ' ')
Creates 5 nodes to build a singly linked list
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "Dahyun" node2 = Node() node2.data = "Jungyeon" node1.link = node2 node3 = Node() node3.data = "Tzuyu" node2.link = node3 node4 = Node() node4.data = "Sana" node3.link = node4 node5 = Node() node5.data = "Jihyo" node4.link = node5 print(node1.data, end = ' ') print(node1.link.data, end = ' ') print(node1.link.link.data, end = ' ') print(node1.link.link.link.data, end = ' ') print(node1.link.link.link.link.data, end = ' ')
연결 리스트를 반복문으로 생성합니다
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "다현" node2 = Node() node2.data = "정연" node1.link = node2 node3 = Node() node3.data = "쯔위" node2.link = node3 node4 = Node() node4.data = "사나" node3.link = node4 node5 = Node() node5.data = "지효" node4.link = node5 current = node1 print(current.data, end = ' ') while current.link != None : current = current.link print(current.data, end = ' ')
Creates a linked list using a loop
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "Dahyun" node2 = Node() node2.data = "Jungyeon" node1.link = node2 node3 = Node() node3.data = "Tzuyu" node2.link = node3 node4 = Node() node4.data = "Sana" node3.link = node4 node5 = Node() node5.data = "Jihyo" node4.link = node5 current = node1 print(current.data, end = ' ') while current.link != None : current = current.link print(current.data, end = ' ')
연결 리스트의 노드를 순회하며 출력합니다
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "다현" node2 = Node() node2.data = "정연" node1.link = node2 node3 = Node() node3.data = "쯔위" node2.link = node3 node4 = Node() node4.data = "사나" node3.link = node4 node5 = Node() node5.data = "지효" node4.link = node5 newNode = Node() newNode.data = "재남" newNode.link = node2.link # 정연의 링크 node2.link = newNode current = node1 print(current.data, end = ' ') while current.link != None : current = current.link print(current.data, end = ' ')
Traverses and prints nodes of a linked list
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "Dahyun" node2 = Node() node2.data = "Jungyeon" node1.link = node2 node3 = Node() node3.data = "Tzuyu" node2.link = node3 node4 = Node() node4.data = "Sana" node3.link = node4 node5 = Node() node5.data = "Jihyo" node4.link = node5 newNode = Node() newNode.data = "Jaenam" newNode.link = node2.link # Jungyeon's link node2.link = newNode current = node1 print(current.data, end = ' ') while current.link != None : current = current.link print(current.data, end = ' ')
연결 리스트에서 특정 노드를 검색합니다
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "다현" node2 = Node() node2.data = "정연" node1.link = node2 node3 = Node() node3.data = "쯔위" node2.link = node3 node4 = Node() node4.data = "사나" node3.link = node4 node5 = Node() node5.data = "지효" node4.link = node5 node2.link = node3.link # 쯔위의 링크를 정연의 링크로 복사 del(node3) # 쯔위 삭제 current = node1 print(current.data, end = ' ') while current.link != None : current = current.link print(current.data, end = ' ')
Searches for a specific node in a linked list
class Node() : def __init__ (self) : self.data = None self.link = None node1 = Node() node1.data = "Dahyun" node2 = Node() node2.data = "Jungyeon" node1.link = node2 node3 = Node() node3.data = "Tzuyu" node2.link = node3 node4 = Node() node4.data = "Sana" node3.link = node4 node5 = Node() node5.data = "Jihyo" node4.link = node5 node2.link = node3.link # Copy Tzuyu's link to Jungyeon's link del(node3) # Delete Tzuyu current = node1 print(current.data, end = ' ') while current.link != None : current = current.link print(current.data, end = ' ')
연결 리스트의 첫 번째 위치에 노드를 삽입합니다
## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None dataArray = ["다현", "정연", "쯔위", "사나", "지효"] ## 메인 코드 부분 ##
Inserts a node at the first position of a linked list
## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
## Global Variables ## memory = [] head, current, pre = None, None, None dataArray = ["Dahyun", "Jungyeon", "Tzuyu", "Sana", "Jihyo"] ## Main Code ## if __name__ == "__main__" : node = Node() # First node node.data = dataArray[0] head = node memory.append(node) for data in dataArray[1:] : # From the second node onward pre = node node = Node() node.data = data pre.link = node memory.append(node) printNodes(head)
연결 리스트의 중간/마지막에 노드를 삽입합니다
## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def insertNode(findData, insertData) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None dataArray = ["다현", "정연", "쯔위", "사나", "지효"] ## 메인 코드 부분 ##
Inserts a node in the middle or at the end of a linked list
## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def insertNode(findData, insertData) :
## Global Variables ##
memory = []
head, current, pre = None, None, None
dataArray = ["Dahyun", "Jungyeon", "Tzuyu", "Sana", "Jihyo"]
## Main Code ##
if __name__ == "__main__" :
node = Node() # First node
node.data = dataArray[0]
head = node
memory.append(node)
for data in dataArray[1:] : # From the second node onward
pre = node
node = Node()
node.data = data
pre.link = node
memory.append(node)
printNodes(head)
insertNode("Dahyun", "Hwasa")
printNodes(head)
insertNode("Sana", "Solar")
printNodes(head)
insertNode("Jaenam", "Moonbyul")
printNodes(head)
연결 리스트의 첫 번째 노드를 삭제합니다
## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def deleteNode(deleteData) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None dataArray = ["다현", "정연", "쯔위", "사나", "지효"] ## 메인 코드 부분 ##
Deletes the first node of a linked list
## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def deleteNode(deleteData) :
## Global Variables ##
memory = []
head, current, pre = None, None, None
dataArray = ["Dahyun", "Jungyeon", "Tzuyu", "Sana", "Jihyo"]
## Main Code ##
if __name__ == "__main__" :
node = Node() # First node
node.data = dataArray[0]
head = node
memory.append(node)
for data in dataArray[1:] : # From the second node onward
pre = node
node = Node()
node.data = data
pre.link = node
memory.append(node)
printNodes(head)
deleteNode("Dahyun")
printNodes(head)
deleteNode("Tzuyu")
printNodes(head)
deleteNode("Jihyo")
printNodes(head)
deleteNode("Jaenam")
printNodes(head)
연결 리스트의 중간/마지막 노드를 삭제합니다
## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def findNode(findData) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None dataArray = ["다현", "정연", "쯔위", "사나", "지효"] ## 메인 코드 부분 ##
Deletes a middle or last node from a linked list
## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def findNode(findData) :
## Global Variables ##
memory = []
head, current, pre = None, None, None
dataArray = ["Dahyun", "Jungyeon", "Tzuyu", "Sana", "Jihyo"]
## Main Code ##
if __name__ == "__main__" :
node = Node() # First node
node.data = dataArray[0]
head = node
memory.append(node)
for data in dataArray[1:] : # From the second node onward
pre = node
node = Node()
node.data = data
pre.link = node
memory.append(node)
printNodes(head)
fNode = findNode("Dahyun")
print(fNode.data)
fNode = findNode("Tzuyu")
print(fNode.data)
fNode = findNode("Jaenam")
print(fNode.data)
연결 리스트의 삽입/삭제 통합 프로그램입니다
## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def makeSimpleLinkedList(namePhone) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None dataArray = [["지민", "010-1111-1111"], ["정국", "010-2222-2222"], ["뷔", "010-3333-3333"], ["슈가", "010-4444-4444"], ["진", "010-5555-5555"]] ## 메인 코드 부분 ##
Complete linked list program with insert and delete operations
## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def makeSimpleLinkedList(namePhone) :
## Global Variables ## memory = [] head, current, pre = None, None, None dataArray = [["Jimin", "010-1111-1111"], ["Jungkook", "010-2222-2222"], ["V", "010-3333-3333"], ["Suga", "010-4444-4444"], ["Jin", "010-5555-5555"]] ## Main Code ## if __name__ == "__main__" : for data in dataArray : makeSimpleLinkedList(data) printNodes(head)
연결 리스트를 활용한 친구 관리 프로그램입니다
## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def makeSimpleLinkedList(nameEmail) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None ## 메인 코드 부분 ##
Friend management program using a linked list
## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def makeSimpleLinkedList(nameEmail) :
## Global Variables ##
memory = []
head, current, pre = None, None, None
## Main Code ##
if __name__ == "__main__" :
while True:
name = input("Name--> ")
if name == "" or name == None :
break
email = input("Email--> ")
makeSimpleLinkedList([name, email])
printNodes(head)
랜덤 데이터로 연결 리스트를 생성하고 정렬 삽입합니다
import random ## 클래스와 함수 선언 부분 ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def makeLottoList(num) :
def findNumber(num) :
## 전역 변수 선언 부분 ## memory = [] head, current, pre = None, None, None ## 메인 코드 부분 ##
Creates a linked list with random data using sorted insertion
import random ## Class & Function Declarations ## class Node() : def __init__ (self) : self.data = None self.link = None def printNodes(start) :
def makeLottoList(num) :
def findNumber(num) :
## Global Variables ## memory = [] head, current, pre = None, None, None ## Main Code ## if __name__ == "__main__" : lottoCount = 0 while True: lotto = random.randint(1,45) if findNumber(lotto) : continue lottoCount += 1 makeLottoList(lotto) if lottoCount >= 6 : break printNodes(head)
연결 리스트의 노드 삭제 기능을 직접 구현합니다
## 클래스 및 함수 선언부 ##
class Node() :
def __init__ (self) :
self.data = None
self.link = None
def printNodes(start) :
def deleteNode(deleteData) :
## 전역 변수 선언부 memory = [] head, current, pre = None, None, None dataArray = ["다현", "정연", "쯔위", "사나", "지효"] ## 메인 코드부
Implements linked list node deletion yourself
## Class & Function Declarations ##
class Node() :
def __init__ (self) :
self.data = None
self.link = None
def printNodes(start) :
def deleteNode(deleteData) :
## Global Variables
memory = []
head, current, pre = None, None, None
dataArray = ["Dahyun", "Jungyeon", "Tzuyu", "Sana", "Jihyo"]
## Main Code
if __name__ == "__main__" :
# First node
node = Node()
node.data = dataArray[0]
head = node
memory.append(node)
# From the second node onward
for data in dataArray[1:] :
pre = node
node = Node()
node.data = data
pre.link = node
memory.append(node)
printNodes(head)
deleteNode("Dahyun")
printNodes(head)
deleteNode("Tzuyu")
printNodes(head)
deleteNode("Sana")
printNodes(head)
deleteNode("Jaenam")
printNodes(head)연결 리스트의 노드 삽입 기능을 직접 구현합니다
## 클래스 및 함수 선언부 ##
class Node() :
def __init__ (self) :
self.data = None
self.link = None
def printNodes(start) :
def makeSimpleLinkedList(nameAge) :
## 전역 변수 선언부 memory = [] head, current, pre = None, None, None dataArray = [["지민", 180], ["정국", 177], ["뷔", 183], ["슈가", 175], ["진", 179]] ## 메인 코드부
Implements linked list node insertion yourself
## Class & Function Declarations ##
class Node() :
def __init__ (self) :
self.data = None
self.link = None
def printNodes(start) :
def makeSimpleLinkedList(nameAge) :
## Global Variables
memory = []
head, current, pre = None, None, None
dataArray = [["Jimin", 180], ["Jungkook", 177], ["V", 183], ["Suga", 175], ["Jin", 179]]
## Main Code
if __name__ == "__main__" :
for data in dataArray :
makeSimpleLinkedList(data)
printNodes(head)