์์๋ค์ ์ ์ฅํ ๋ ๊ทธ ๋ค์ ์์๊ฐ ์๋ ์์น๋ฅผ ํฌํจ์ํค๋ ๋ฐฉ์์ผ๋ก ์ ์ฅํ๋ ์๋ฃ๊ตฌ์กฐ.


๋ฉ๋ชจ๋ฆฌ ์์ ์์๋ฅผ ๋๋ ๋ฐฉ๋ฒ์ ๋ฌ๋ผ๋ ์์ ์ฌ์ด์ ์ ํ ๊ด๊ณ๋ก ์ผ๋์ผ ์ ์๊ฐ ๋๋ค. ๋ฐฐ์ด๊ณผ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ ๋ ๋ค ์ ํ ์๋ฃ๊ตฌ์กฐ์ด๋ค. ๋ฐฐ์ด์ ๋ฐฐ์น๊ฐ ์ฐ์์ด์ง๋ง ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ ๋ถ์ฐ์์ด๋ค. ์์๊ฐ ๋ค์ ์์ or ์ด์ ,๋ค์ ์์์ ์ฃผ์๊ฐ์ ๊ฐ์ง๊ณ ์์ด์ผ ํ๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ข ๋ ํ์.
์ฐ๊ฒฐ๋ฆฌ์คํธ์์๋ ์์์ ์์น์ ์๋ ์์๋ก ๊ฐ๊ธฐ ์ํด์ ์์ฐจ์ ์ผ๋ก ๋ฐฉ๋ฌธํด์ผ ํ๋ค. ์ฐ๋ฆฌ๋ ์ฒซ ์์์ ์ฃผ์๋ง ์๊ณ ์๊ธฐ ๋๋ฌธ์.
์ถ๊ฐํ๊ณ ์ถ์ ์์น์ ์ฃผ์๋ฅผ ์๊ณ ์์ ๊ฒฝ์ฐ์๋ง O(1)์ด๋ค.
์์ ์์น์ ์์๋ฅผ ์ ๊ฑฐํ๋ ์ฐ์ฐ์ O(1)์ ๊ฐ๋ฅํ๋ค. ์ง์ฐ๋ ค๋ ์์์ ์ด์ ์์์๊ฒ ์ง์ฐ๋ ค๋ ์์ ์ดํ์ ์์ ์ฃผ์๋ฅผ ์๋ ค์ฃผ๋ฉด ๋๋ค.
๋ฉ๋ชจ์ฅ๊ณผ ํ
์คํธ ์๋ํฐ๊ฐ์ ์ํฉ์์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ฐ๊ฒ ๋๋ค.
-> ์์์ ์์น์ ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์์ ์์น์ ์์๋ฅผ ์ ๊ฑฐํ๋ ์ฐ์ฐ์ ๋ง์ด ํด์ผํ๋ฉด ๊ณ ๋ ค.
Node ๊ตฌ์กฐ์ฒด๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ์์ ์์ฑ๋ง๋ค ๋์ ํ ๋น์ ํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ค.
1 class Node:
2 def __init__(self, data):
3 self.data = data #data๋ ๊ฐ์ ๊ฐ๋ฆฌํค๋ ๋ณ์(์์ฑ, attribute)
4 self.next = None #next๋ ๋ค์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ณ์
5
6
7 head=Node(1) # ๋งจ ์ฒซ ๋ฒ์งธ ๋
ธ๋๋ head๋ผ๊ณ ํ์
8 head.next=Node(2)
9 head.next.next=Node(3)

๊ฐ์ฅ ์ฒ์
๋งจ ๋ง์ง๋ง์ last๋ผ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง ๋ ธ๋๋ฅผ ๋ง๋ค์ด๋ณด์.
node = head
while node.next: # ํ์ฌ ๋
ธ๋์ next๊ฐ None์ด ์๋ ๋์ ์คํ
node = node.next
node.next = Node('last')

์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ class๋ก ๊ตฌํํด๋ณด์.
class LinkedList:
def __init__(self):
self.head = None
self.length = 0
def __len__(self):
return self.length
๊ตฌํํ ๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ฉ์๋๋ ์๋์ ๊ฐ๋ค. ์ด๋ฆ์ ๋ฆฌ์คํธ์ ๋ฐํฌ(deque)์ ๋ฉ์๋ ์ด๋ฆ์ ์ฐธ์กฐํ๋ค.
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ append ๋ฉ์๋์ ๊ทธ์ ๋ฐ๋๋๋ appendleft ๋ฉ์๋๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด
class LinkedList:
def __init__(self):
self.head = None
self.length = 0
def __len__(self):
return self.length
def append(self,data):
if self.head is None:
self.head=Node(data)
else:
node=self.head #์ฒซ๋ฒ์งธ ๋
ธ๋๋ฅผ ๋ถ๋ฌ์ค๊ธฐ
while node.next:
node=node.next
node.next=Node(data)
self.length+=1
def appendleft(self,data):
if self.head is None:
self.head = Node(data)
else:
node=Node(data)
node.next=self.head
self.head=node
self.length+=1
appendleft๋ ์ผ์ชฝ๋ถํฐ ์ถ๊ฐ๋๊ณ append๋ ์ค๋ฅธ์ชฝ์์๋ถํฐ ์ถ๊ฐ๋๋ค.
๋ค์์ผ๋ก ๊ตฌํํ ๋ฉ์๋๋ ์ํ ์ถ๋ ฅ๊ณผ ๊ฐ ๊ฒ์์ด๋ค.
def __str__(self):
if self.head is None: #์์ผ๋ฉด ์๋ค๊ณ ๋งํ๊ณ
return "Empty LinkedList"
print("List Print")
node=self.head #์ฒซ ๋
ธ๋๋ฅผ ๊ฐ์ ธ์จ ๋ค์์
res="Head"
while node: # ๋
ธ๋ ์์ ๊ฐ์ด ์์ผ๋ฉด
res+="-> "+str(node.data) #์ถ๋ ฅ์ ํฌํจ
node=node.next
return res
def __contains__(self,target):
if self.head is None:
return False
node=self.head
while node:
if node.data==target: #์์ผ๋ฉด ์ถ๋ ฅ
return True
node=node.next
return False
๋ง์ง๋ง์ผ๋ก remove()๊ณผ insert(i,x)๋ฅผ ๊ตฌํํด๋ณด๊ฒ ๋ค.
remove ๋ฉ์๋๋ฅผ ์ค๋ช
ํ์๋ฉด.
์์ ๋
ธ๋ ๋ ๊ฐ(node,prev)๋ฅผ ๋ง๋๋ ๊ฒ์ด ํฌ์ธํธ์ด๋ค.
1. node๋ ์ญ์ ํ ์์๋ฅผ ์ฐพ๊ณ ,prev๊ฐ ๋ค๋ฅผ ๋ฐ๋ผ๊ฐ๋ค.
2. ์ญ์ ํ ์์๋ฅผ ์ฐพ์์ผ๋ฉด prev.next๊ฐ node๊ฐ ์๋ node.next๊ฐ ๋๊ฒํ๋ค.
-> ์ญ์ ํ ์์๊ฐ head ์ผ๋์ ์๋ ๋๋ฅผ ๊ตฌ๋ถํ๋ค.
def remove(self,target):
node=self.head
while node is not None and node.data != target:
prev=node
node=node.next
if node is None:
return False
if node==self.head:
self.head=self.head.next
else:
prev.next=node.next
self.length-=1
return True
insert() ๋ฉ์๋๋ ๋ช๊ฐ์ง ๊ท์น์ด ํ์ํ๋ค.
๋
ธ๋ ์ฝ์
์ ์ฒ๋ฆฌํ ๋จ๊ณ๋ ๋ง๊ณ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ์์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ธ์ง๋ ๊ณ ๋ คํด์ผ ํ๋ค.
def insert(self,i,data):
if i<0:
self.appendleft(data)
elif i>self.length:
self.append(data)
else:
node=self.head
for _ in range(i-1):
node=node.next
temp=Node(data)
temp.next=node.next
node.next=temp
self.length+=1
์ฐ์ต ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์
์ด ํธ์ง๊ธฐ์๋ '์ปค์'๋ผ๋ ๊ฒ์ด ์๋๋ฐ, ์ปค์๋ ๋ฌธ์ฅ์ ๋งจ ์(์ฒซ ๋ฒ์งธ ๋ฌธ์์ ์ผ์ชฝ), ๋ฌธ์ฅ์ ๋งจ ๋ค(๋ง์ง๋ง ๋ฌธ์์ ์ค๋ฅธ์ชฝ), ๋๋ ๋ฌธ์ฅ ์ค๊ฐ ์์์ ๊ณณ(๋ชจ๋ ์ฐ์๋ ๋ ๋ฌธ์ ์ฌ์ด)์ ์์นํ ์ ์๋ค. ์ฆ ๊ธธ์ด๊ฐ L์ธ ๋ฌธ์์ด์ด ํ์ฌ ํธ์ง๊ธฐ์ ์ ๋ ฅ๋์ด ์์ผ๋ฉด, ์ปค์๊ฐ ์์นํ ์ ์๋ ๊ณณ์ L+1๊ฐ์ง ๊ฒฝ์ฐ๊ฐ ์๋ค.
์ด ํธ์ง๊ธฐ๊ฐ ์ง์ํ๋ ๋ช ๋ น์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ฒซ์งธ ์ค์๋ ์ด๊ธฐ์ ํธ์ง๊ธฐ์ ์ ๋ ฅ๋์ด ์๋ ๋ฌธ์์ด์ด ์ฃผ์ด์ง๋ค. ์ด ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ N์ด๊ณ , ์์ด ์๋ฌธ์๋ก๋ง ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ, ๊ธธ์ด๋ 100,000์ ๋์ง ์๋๋ค. ๋์งธ ์ค์๋ ์ ๋ ฅํ ๋ช ๋ น์ด์ ๊ฐ์๋ฅผ ๋ํ๋ด๋ ์ ์ M(1 โค M โค 500,000)์ด ์ฃผ์ด์ง๋ค. ์ ์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์ ๊ฑธ์ณ ์ ๋ ฅํ ๋ช ๋ น์ด๊ฐ ์์๋๋ก ์ฃผ์ด์ง๋ค. ๋ช ๋ น์ด๋ ์์ ๋ค ๊ฐ์ง ์ค ํ๋์ ํํ๋ก๋ง ์ฃผ์ด์ง๋ค.
์ฒซ์งธ ์ค์ ๋ชจ๋ ๋ช ๋ น์ด๋ฅผ ์ํํ๊ณ ๋ ํ ํธ์ง๊ธฐ์ ์ ๋ ฅ๋์ด ์๋ ๋ฌธ์์ด์ ์ถ๋ ฅํ๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ํ์ฉํ์ฌ ํ์ด์ฌ์ผ๋ก ๋ง๋ค์๋ค.
import sys
class Node:
def __init__(self, data):
self.data = data #data๋ ๊ฐ์ ๊ฐ๋ฆฌํค๋ ๋ณ์(์์ฑ, attribute)
self.next = None #next๋ ๋ค์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ณ์
class LinkedList:
def __init__(self):
self.head = None
self.length = 0
self.cursor=0
def __len__(self):
return self.length
def append(self,data):
if self.head is None:
self.head=Node(data)
else:
node=self.head
while node.next is not None:
node=node.next
node.next=Node(data)
self.length+=1
self.cursor+=1
def __str__(self):
if self.head is None:
return "It is empty"
res=""
node=self.head
while node is not None:
res+=str(node.data)
node=node.next
return res
def L(self):
if self.cursor!=0:
self.cursor-=1
def D(self):
if self.cursor!=self.length:
self.cursor+=1
#์ปค์ ์ผ ์ชฝ์ ์๋ ์์๋ฅผ ์ญ์ ํด์ผํ๋ค. ์ผ์ชฝ
def B(self):
if self.cursor!=0:
cursor=self.cursor
node=self.head
for _ in range(cursor-1):
prev=node
node=node.next
if node==self.head:
self.head=self.head.next
else:
prev.next=node.next
self.cursor-=1
self.length-=1
def P(self,data):
cursor=self.cursor
if cursor==0 and self.head is not None:
temp=Node(data)
temp.next=self.head
self.head=temp
self.cursor+=1
self.length+=1
return
if self.head:
node=self.head
for _ in range(cursor-1):
node=node.next
temp=Node(data)
temp.next=node.next
node.next=temp
self.cursor+=1
self.length+=1
else:
self.head=Node(data)
self.cursor+=1
self.length+=1
str_ex=sys.stdin.readline().strip()
n=int(sys.stdin.readline())
editor=LinkedList()
for i in range(len(str_ex)):
editor.append(str_ex[i])
for _ in range(n):
func=sys.stdin.readline().split()
if func[0]=='P':
editor.P(func[1])
elif func[0]=='L':
editor.L()
elif func[0]=='D':
editor.D()
elif func[0]=='B':
editor.B()
print(editor)
์ผ๋จ, ๋ฐฐ์ด๊ฑด ๋ค ์ฌ์ฉํ์ง๋ง ์๊พธ ์๊ฐ์ด๊ณผ๊ฐ ๋์ ์ฝ๋๋ฅผ ์์ ํด์ผํ๋ค.
pop๊ณผ append๋ก ์คํ์ ํ์ฉํด์ ์์ฑํ๋ผ๋๋ฐ?
์ด๊ฑด ํ์ ์์ ํด๋ด์ผ ๊ฒ ๋ค.