본문 바로가기
자료구조

연습문제와 응용예제

by Benihs 2022. 10. 18.

 1  연습문제

** 문제는 처음에 언급한 책에 나와있습니다 **

 

답 :

1.  3,4번
2. node1.link = node1
3. (1)node2 (2) node3 (3) node3

4. 2번
5.  (1) current = head (2) last = last.link (3) last.link = head
6.  (1) return current (2) current.link != head (3) current = current.link

 

 2  응용예제 01

현재 위치를 (0,0)이라 가정하고, 편의점 위치(x,y)와 거리가 가까운 순서대로 원형 연결 리스트를 생성하는 프로그램

import random
import math

## 클래스와 함수 선언 부분 ##
class Node():
    def __init__(self):
        self.data = None
        self.link = None

def printStores(start):
    current = start
    if current == None:
        return

    while current.link != head:
        current = current.link
        x,y = current.data[1:]
        print(current.data[0], "편의점 거리 :", math.sqrt(x*x + y*y))
    print()
    
def makeStoreList(store) :
    global memory, head, current, pre
    
    node = Node()
    node.data = store
    memory.append(node)
    
    if head == None:
        head = node
        node.link = head
        return
    
    nodeX, nodeY = node.data[1:]
    nodeDist = math.sqrt(nodeX*nodeX + nodeY*nodeY)
    headX, headY = head.data[1:]
    headDist = math.sqrt(headX*headX + headY*headY)
    
    if headDist > nodeDist :
        node.link = head
        last = head

        while last.link != head :
            last = last.link
        last.link = node
        head = node
        return
    current = head

    while current.link != head :
        pre = current
        current = current.link
        currX, currY =current.data[1:]
        currDist = math.sqrt(currX*currX + currY*currY)
        if currDist > nodeDist :
            pre.link = node
            node.link = current
            return
        
    current.link = node
    node.link = head
    
memory = []
head, current, pre = None, None, None

if __name__ == "__main__" :
    
    storeArray = []
    storeName = 'A'
    for _ in range(10) :
        store = (storeName, random.randint(1,100), random.randint(1,100))
        storeArray.append(store)
        storeName = chr(ord(storeName)+ 1)
        
    for store in storeArray :
        makeStoreList(store)
        
    printStores(head)

 

 3  응용예제 02

양뱡향으로 링크가 연결되는 이중 연결 리스트를 만든다

## 클래스와 함수 선언 부분 ##
class Node2():
    def __init__(self):
        self.plink = None
        self.data = None
        self.nlink = None

def printNodes(start):
    current = start
    if current.nlink == None:
        return
    print("정방향 -->", end=' ')
    print(current.data, end=' ')
    while current.nlink != None:
        current = current.nlink
        print(current.data, end=' ')
    print()
    print("역방향 -->", end=' ')
    print(current.data, end=' ')
    while current.plink != None:
        current = current.plink
        print(current.data, end=' ')
    print()
    
memory = []
head, current, pre = None, None, None
dataArray = ["다현", "정연", "쯔위", "사나", "지효"]

if __name__ == "__main__" :
    
    node = Node2()
    node.data = dataArray[0]
    head = node
    memory.append(node)
    
    for data in dataArray[1:] :
        pre = node
        node = Node2()
        node.data = data
        pre.nlink = node
        node.plink = pre
        memory.append(node)
        
    printNodes(head)