Linked List 2

์ฑ„์˜๋ฏผยท2024๋…„ 3์›” 26์ผ
0
post-custom-banner

๐Ÿ“š Node in Linked List

๐Ÿ’ก ๋…ธ๋“œ๋Š” ๊ฐ’๊ณผ ํฌ์ธํ„ฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

  • ๋…ธ๋“œ๋Š” ๊ทผ๋ณต์ ์œผ๋กœ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜• ์ด๋‹ค.
  • ์œ„ Linked List๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
head = {
	'value' : 11,
    'next' : {
			'value' : 3,
	    	'next' : {
					'value' : 23,
				    'next' : {
							'value' : 7,
						    'next' : None
							}
					}
			}
		}

print(my_linked_list.head.next.next.value) // 23
  • ์œ„์— ๋”•์…”๋„ˆ๋ฆฌ ํ˜•๊ณผ, ์‹ค์ œ Linked List ๊ตฌ๋ฌธ์€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ๋‹ค๋ฅธ ๊ณณ์— ์ €์žฅํ•  ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์€ ์„œ๋กœ ๋น„์Šทํ•˜๋‹ค.
  • ์‹ค์ œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€๋Š” ๊ตฌํ˜„ ๋ฐฉ์‹์ด ๋‹ค๋ฅด์ง€๋งŒ, ๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š” ์œ ์‚ฌํ•˜๋ฏ€๋กœ Linked List๋ฅผ ์ค‘์ฒฉ๋œ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์œผ๋กœ ์ƒ๊ฐํ•ด๋„ ๋œ๋‹ค.

๐Ÿ“š Constructor in Linked List

class LinkedList :
	def __init__(self, value) : 
	# self ์ธ์ž๋ฅผ ํ†ตํ•ด, ํด๋ž˜์Šค ์•ˆ์— ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
    new_node = Node(value)
    self.head = new_node
	self.tail = new_node 
    self.length = 1
   
 	def append(self, value) :  
    	pass
    def prepend(self, value) :
    	pass
	def insert(self, value) :
    	pass

class Node :
	# ์œ„์˜ LinkedList์— ๋ฉ”์†Œ๋“œ๋“ค์€ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค ๋ผ๋Š” ๊ณตํ†ต์ ์ด ์žˆ๊ณ , 
    # ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋˜‘๊ฐ™์€ ์ž‘์—…์„ ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ค ๋ฒˆ ๋งŒ๋“ค์–ด ์ž‘์„ฑํ•˜์ง€ ์•Š๋Š” ๋Œ€์‹  ํ•ด๋‹น Node ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
    def __init__(self, value) : # ํ•ด๋‹น ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ „๋‹ฌ ํ›„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
    	self.value = value # ํŠน์ • ์ธ์Šคํ„ด์Šค์— ์ ์šฉ๋˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ๋Š” self ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
        self.next = None

๐Ÿ“š Append

๐Ÿ’ก ๋งจ ๋’ค์˜ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

def append(self, value) :
	new_node = Node(value) // ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ

	if self.head is None : // Linked List๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ
    	self.head = new_node
        self.tail = new_node
    else : 
    	self.tail.next = new_node
        self.tail = new_node
    self.length += 1
   
    return True # ์ด ๊ตฌ๋ฌธ์€ ํ•„์ˆ˜์ ์ด์ง€ ์•Š์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ๋“ค๊ณผ ๋น„๊ต๋ฅผ ์œ„ํ•ด์„œ ์ž‘์„ฑํ•œ ๊ตฌ๋ฌธ

๐Ÿ“š Pop

๐Ÿ’ก ๋งจ ๋’ค์˜ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์†Œ๋“œ

def pop(self) :
	if self.length == 0 : 
    	return None
	# ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 0, ์ฆ‰ ๋น„์–ด ์žˆ๋‹ค๋ฉด, ์ œ๊ฑฐํ•  ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ None์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒ.
       
	temp = self.head # temp๋Š” ํƒ์ƒ‰์„ ์œ„ํ•ด ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
    pre = self.head # pre๋Š” temp ๋ฐ”๋กœ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ถ”์ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
    # ๋‘ ๋ณ€์ˆ˜ temp์™€ pre๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ์ธ self.head๋กœ ์ดˆ๊ธฐํ™”
   
 	while(temp.next) : # temp๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ์˜ next๊ฐ€ None์ด ์•„๋‹ ๋•Œ๊นŒ์ง€, ์ฆ‰ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    	pre = temp # pre๋ฅผ temp๋กœ ์—…๋ฐ์ดํŠธํ•ด์„œ ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ์ถ”์ 
        temp = temp.next # ๊ทธ๋ฆฌ๊ณ  temp๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™์‹œํ‚ด. ์ด ๋ฃจํ”„๋ฅผ ํ†ตํ•ด ๋ฆฌ์ŠคํŠธ์˜ ๋๊นŒ์ง€ ์ด๋™ํ•˜๊ฒŒ ๋จ.
       
    self.tail = pre # ๋ฃจํ”„๊ฐ€ ๋๋‚˜๋ฉด, temp๋Š” ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
    self.tail.next = None # pre๋Š” ๋งˆ์ง€๋ง‰์—์„œ ๋‘ ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
    # ์ด ์‹œ์ ์—์„œ self.tail์„ pre๋กœ ์—…๋ฐ์ดํŠธํ•ด์„œ ์ƒˆ๋กœ์šด ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ๋งŒ๋“ค๊ณ , self.tail.next๋ฅผ None์œผ๋กœ ์„ค์ •ํ•ด ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ž„์„ ๋ช…์‹œ
   
    self.length -= 1

	if self.length == 0 :
    	self.head = None
        self.tail = None
    # ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์ œ๊ฑฐ๋˜์–ด ๊ธธ์ด๊ฐ€ 0์ด ๋˜๋ฉด, self.head์™€ self.tail์„ None์œผ๋กœ ์„ค์ •ํ•ด ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด ์žˆ์Œ์„ ํ‘œ์‹œ
      
 	return temp # ๋งˆ์ง€๋ง‰์œผ๋กœ ์ œ๊ฑฐ๋œ ๋…ธ๋“œ์ธ temp๋ฅผ ๋ฐ˜ํ™˜

๐Ÿ“š Prepend

๐Ÿ’ก ๋งจ ์•ž์˜ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

def prepend(self, value) :
	new_node = Node(value)
    if self.length == 0 :
    	self.haed = new_node
        self.tail = new_node
    else : 
    	new_node.next = self.head
        self.head = new_node
	self.length += 1
    return True

๐Ÿ“š Pop first

๐Ÿ’ก ๋งจ ์•ž์˜ ๋…ธ๋“œ๋ฅผ ์ œ๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

def pop_first(self) :
	if self.length == 0 :
    	return None
       
	temp = self.head
    self.head = self.head.next
    temp.next = None
    self.length -= 1
   
    if self.length == 0 : # ์ด ์กฐ๊ฑด๋ฌธ์€, ๋…ธ๋“œ๊ฐ€ ํ•œ๊ฐœ๋ฟ์ผ ๊ฒฝ์šฐ์— ์ ์šฉ๋œ๋‹ค. 
    	self.tail = None
	return temp

๐Ÿ“š Print List

def print_list(self) :
	temp = self.head
    while temp is not None :
    	print(temp.value)
        temp = temp.next
profile
์‹œ๊ฐ์ ์ธ ์ฝ”๋”ฉ์„ ์ฆ๊ธฐ๋Š” ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ ์ฑ„์˜๋ฏผ ์ž…๋‹ˆ๋‹ค;
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€