Advertisement
AquaBlitz11

Linked List (iterative, with tails)

Jul 15th, 2020
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. # LOOK FOR "NEW LINE" (use CTRL+F)
  2.  
  3. class Node:
  4.  
  5.     def __init__(self, val, nxt=None):
  6.         self.val = val
  7.         self.nxt = nxt
  8.  
  9. class LinkedList:
  10.  
  11.     def __init__(self):
  12.         self.head = None
  13.         self.tail = None # NEW LINE
  14.         self.count = 0
  15.    
  16.     def get_count(self):
  17.         return self.count
  18.    
  19.     def is_empty(self):
  20.         return (self.get_count() == 0)
  21.    
  22.     def __str__(self):
  23.         ret = []
  24.         curr = self.head
  25.         while curr != None:
  26.             ret.append(curr.val)
  27.             curr = curr.nxt
  28.         return str(ret)
  29.    
  30.     def get_node_at(self, idx):
  31.         if idx >= self.get_count() or idx < -self.get_count():
  32.             raise IndexError("list index out of range")
  33.         if idx == -1: # shortcut case # NEW LINE
  34.             return self.tail # NEW LINE
  35.         if idx < 0: # negative index = start counting from the back
  36.             idx = self.get_count()+idx
  37.         curr = self.head
  38.         for i in range(idx):
  39.             curr = curr.nxt
  40.         return curr
  41.    
  42.     def __getitem__(self, idx):
  43.         return (self.get_node_at(idx)).val
  44.    
  45.     def insert_front(self, val):
  46.         self.count += 1
  47.         self.head = Node(val, self.head)
  48.         if self.get_count() == 1: # NEW LINE
  49.             self.tail = self.head # NEW LINE
  50.    
  51.     def insert_back(self, val):
  52.         if self.is_empty():
  53.             self.head = Node(val, None)
  54.             self.tail = self.head # NEW LINE
  55.             self.count += 1
  56.             return
  57.         self.tail.nxt = Node(val, None) # NEW LINE
  58.         self.tail = self.tail.nxt # NEW LINE
  59.         self.count += 1
  60.    
  61.     def delete_front(self):
  62.         if self.is_empty():
  63.             raise IndexError("deleting from an empty list")
  64.         self.head = self.head.nxt
  65.         self.count -= 1
  66.         if self.is_empty(): # NEW LINE
  67.             self.tail = None # NEW LINE
  68.    
  69.     def delete_at(self, idx):
  70.         if self.is_empty():
  71.             raise IndexError("deleting from an empty list")
  72.         if idx >= self.get_count() or idx < -self.get_count():
  73.             raise IndexError("list index out of range")
  74.         if idx == 0 or idx == -self.get_count():
  75.             self.delete_front()
  76.             return
  77.         curr = self.get_node_at(idx-1)
  78.         if curr.nxt == self.tail: # NEW LINE
  79.             self.tail = curr # NEW LINE
  80.         curr.nxt = curr.nxt.nxt
  81.         self.count -= 1
  82.    
  83.     def delete_back(self):
  84.         self.delete_at(-1)
  85.    
  86.     def reverse(self):
  87.         if self.get_count() <= 1:
  88.             return
  89.         curr = self.head
  90.         self.tail = self.head # NEW LINE
  91.         f1 = curr.nxt
  92.         f2 = f1.nxt
  93.         self.head.nxt = None
  94.         while f2 != None:
  95.             f1.nxt = curr
  96.             curr = f1
  97.             f1 = f2
  98.             f2 = f2.nxt
  99.         f1.nxt = curr
  100.         self.head = f1
  101.  
  102. L = LinkedList()
  103. L.insert_back(5)
  104. L.insert_back(6)
  105. L.insert_back(7)
  106. L.insert_back(8)
  107. L.insert_front(3)
  108. L.insert_front(2)
  109. L.insert_front(1)
  110. print(L)
  111. L.reverse()
  112. print(L)
  113. L.delete_back()
  114. L.delete_back()
  115. L.delete_back()
  116. print(L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement