Advertisement
AquaBlitz11

Doubly linked list, Stack, Queue, Deque

Jul 15th, 2020
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. class Node:
  2.  
  3.     def __init__(self, val, prev, nxt):
  4.         self.val = val
  5.         self.prev = prev
  6.         self.nxt = nxt
  7.  
  8. class LinkedList:
  9.  
  10.     def __init__(self):
  11.         self.head = None
  12.         self.tail = None
  13.         self.count = 0
  14.    
  15.     def get_count(self):
  16.         return self.count
  17.    
  18.     def is_empty(self):
  19.         return (self.get_count() == 0)
  20.    
  21.     def __str__(self):
  22.         ret = []
  23.         curr = self.head
  24.         while curr != None:
  25.             ret.append(curr.val)
  26.             curr = curr.nxt
  27.         return str(ret)
  28.    
  29.     def get_node_at(self, idx):
  30.         if idx >= self.get_count() or idx < -self.get_count():
  31.             raise IndexError("list index out of range")
  32.         if idx < 0:
  33.             curr = self.tail
  34.             for i in range(-1, idx, -1):
  35.                 curr = curr.prev
  36.             return curr
  37.         else:
  38.             curr = self.head
  39.             for i in range(idx):
  40.                 curr = curr.nxt
  41.             return curr
  42.    
  43.     def __getitem__(self, idx):
  44.         return (self.get_node_at(idx)).val
  45.    
  46.     def insert_front(self, val):
  47.         self.count += 1
  48.         self.head = Node(val, None, self.head)
  49.         if self.get_count() == 1:
  50.             self.tail = self.head
  51.         else:
  52.             self.head.nxt.prev = self.head # need to make sure to link the previous too
  53.    
  54.     def insert_back(self, val):
  55.         if self.is_empty():
  56.             self.head = Node(val, None, None)
  57.             self.tail = self.head
  58.             self.count += 1
  59.             return
  60.         self.tail.nxt = Node(val, self.tail, None)
  61.         self.tail = self.tail.nxt
  62.         self.count += 1
  63.    
  64.     def delete_front(self):
  65.         if self.is_empty():
  66.             raise IndexError("deleting from an empty list")
  67.         self.head = self.head.nxt
  68.         self.count -= 1
  69.         if self.is_empty():
  70.             self.tail = None
  71.         else:
  72.             self.head.prev = None
  73.    
  74.     def delete_back(self):
  75.         if self.is_empty():
  76.             raise IndexError("deleting from an empty list")
  77.         self.tail = self.tail.prev
  78.         self.count -= 1
  79.         if self.is_empty():
  80.             self.head = None
  81.         else:
  82.             self.tail.nxt = None
  83.  
  84. class Stack:
  85.  
  86.     def __init__(self):
  87.         self.li = LinkedList()
  88.    
  89.     def count(self):
  90.         return self.get_count()
  91.    
  92.     def empty(self):
  93.         return self.is_empty()
  94.    
  95.     def top(self):
  96.         return self.li[0]
  97.    
  98.     def pop(self):
  99.         ans = self.li[0]
  100.         self.li.delete_front()
  101.         return ans
  102.    
  103.     def push(self, val):
  104.         self.li.insert_front(val)
  105.  
  106. class Queue:
  107.  
  108.     def __init__(self):
  109.         self.li = LinkedList()
  110.    
  111.     def count(self):
  112.         return self.get_count()
  113.    
  114.     def empty(self):
  115.         return self.is_empty()
  116.    
  117.     def front(self):
  118.         return self.li[0]
  119.    
  120.     def dequeue(self):
  121.         ans = self.li[0]
  122.         self.li.delete_front()
  123.         return ans
  124.    
  125.     def enqueue(self, val):
  126.         self.li.insert_back(val)
  127.  
  128. class Deque: # double ended queue
  129.  
  130.     def __init__(self):
  131.         self.li = LinkedList()
  132.    
  133.     def count(self):
  134.         return self.get_count()
  135.    
  136.     def empty(self):
  137.         return self.is_empty()
  138.    
  139.     def front(self):
  140.         return self.li[0]
  141.     def back(self):
  142.         return self.li[-1]
  143.    
  144.     def pop_front(self):
  145.         ans = self.li[0]
  146.         self.li.delete_front()
  147.         return ans
  148.     def pop_back(self):
  149.         ans = self.li[-1]
  150.         self.li.delete_back()
  151.         return ans
  152.    
  153.     def push_front(self, val):
  154.         self.li.insert_front(val)
  155.     def push_back(self, val):
  156.         self.li.insert_back(val)
  157.  
  158. Q = Deque()
  159. Q.push_front(3)
  160. Q.push_front(6)
  161. Q.push_front(4)
  162. Q.push_front(8)
  163. Q.push_back(-1)
  164. Q.push_back(-2) # 8 4 6 3 -1 -2
  165. print(Q.pop_front())
  166. print(Q.pop_front()) # 6 3 -1 -2
  167. Q.push_back(-3)
  168. Q.push_back(-4) # 6 3 -1 -2 -3 -4
  169. print(Q.pop_back())
  170. print(Q.pop_back()) # 6 3 -1 -2
  171. print(Q.pop_front())
  172. print(Q.pop_front())
  173. print(Q.pop_front())
  174.  
  175. # L = LinkedList()
  176. # L.insert_back(5)
  177. # L.insert_back(6)
  178. # L.insert_back(7)
  179. # L.insert_back(8)
  180. # L.insert_front(3)
  181. # L.insert_front(2)
  182. # L.insert_front(1)
  183. # print(L)
  184. # L.delete_back()
  185. # L.delete_back()
  186. # L.delete_front()
  187. # print(L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement