class Node:
def __init__(self, val):
self.val = val
self.next = None
class Linked_list:
def __init__(self, lst=[]):
if not lst:
self.head = None
self.size = 0
return
self.head = Node(lst[0])
cur = self.head
for i in range(1, len(lst)):
cur.next = Node(lst[i])
cur = cur.next
def __str__(self):
if not self.head:
return'empty'
a = ''
cur = self.head
while cur:
a += f'{cur.val}'
cur = cur.next
a += f'{cur.val}'
return a
def __get_item__(self, idx):
if idx > self.size -1 :
raise IndexError("out of range")
cur = self.head
cur_idx = 0
while cur_idx != idx:
cur = cur.next
cur_idx +=1
return cur
def __getitem__(self, idx):
target = self.__get_item__(idx)
return target.val
def __setitem__(self, idx, value):
target = self.__get_item__(idx)
target.val =value
def __iter__(self):
cur = self.head
while cur:
yield cur.val
cur = cur.next
def append(self,val):
if not self.head:
self.head = Node(val)
self.size +=1
return
target = self.__get__item(-1)
target.next = Node(val)
self.size+=1
def is_empty(self):
return self.head == Node
def insert(self,idx,val):
new_node=Node(val)
if idx == 0:
new_node.next=self.head
self.head = new_node
return
else:
target =self.__get_item__(idx - 1)
new_node.next = target.next
target.next = new_node
self.size +=1
def remove(self,val):
pass
def pop(self,idx):
pass
def swap(self,idx1,idx2):
pass
def reverse(self):
pass