본문 바로가기
자료구조

자료구조 - 연결리스트 (LinkedList) Python Code

by ddahu 2023. 2. 13.

설명은 JAVA 쪽에서 더 많이

 

class Node:
    def __init__(self,data ,next=None):
        self.data = data
        self.next = next

class LinkedList:
    def __init__(self,data):
        self.head = Node(data)
    
    def addData(self,data):
        cur = self.head
        while cur.next is not None:
            cur = cur.next
        cur.next = Node(data)
    
    def ShowData(self):
        cur = self.head
        while cur is not None:
            print(cur.data , end = " ")
            cur = cur.next

    def removeData(self,data):
        cur = self.head
        prev = cur

        while cur.next is not None :
            if cur.data == data:
                if cur == self.head:
                    self.head = cur.next
                else:
                    prev.next = cur.next
                break
            prev = cur
            cur = cur.next