SHARE
TWEET

Untitled

a guest Nov 17th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node:
  2.     def __init__ (self,value):
  3.         self.value = value
  4.         self.next = None
  5.         self.prev = None
  6.    
  7. class LinkedList:
  8.     def __init__ (self):
  9.         self.head = None
  10.         self.tail = None
  11.         self.size = 0
  12.  
  13.     def push(self, new_value):
  14.         new_value = Node(value)
  15.         if self.size >= 1:
  16.             new_value.prev = self.tail
  17.             self.tail.next = new_value
  18.         else:
  19.             self.head = new_value
  20.         self.tail = new_value
  21.         self.size += 1
  22.    
  23.     def pop(self):
  24.             deleted = self.tail
  25.             self.tail = self.tail.prev
  26.             if self.size > 1:
  27.                 self.tail.prev = None
  28.             self.size -= 1            
  29.             return deleted
  30.    
  31.     def unshift(self, new_value):
  32.         new_value = Node(value)
  33.         if self.size == 0:
  34.             self.tail = new_value
  35.         else:
  36.             new_value.next = self.head
  37.             self.head.prev = new_value
  38.        
  39.         self.head = value
  40.         self.size += 1
  41.    
  42.     def shift(self):
  43.         deleted = self.head
  44.         self.head = self.head.next
  45.         if self.size > 1:
  46.             self.head.prev = None
  47.         self.size -= 1            
  48.         return deleted
  49.    
  50.     def find(self, v):
  51.         current = self.head
  52.         for i in range(self.size):
  53.             if current.value == v:
  54.                 return current
  55.            
  56.             current = current.next
  57.        
  58.         return None
  59.     def get(self, index):
  60.         current = self.head
  61.         for i in range(self.size):
  62.             if i == index:
  63.                 return current
  64.             current = current.next
  65.    
  66.     def size(self):
  67.         return self.size()
  68.    
  69.     def print(self):
  70.         current = self.head
  71.         output = ''
  72.         if self.size > 0:
  73.             for i in range(self.size):
  74.                 output += str(current.value) + ' '
  75.                 current = current.next
  76.            
  77.         print(output)
  78.    
  79.     def insert(self, index, new_value):
  80.         new_value = Node(value)
  81.        
  82.         if index >= self.size - 1:
  83.             self.push(new_value.new_value)
  84.         elif index == 0:
  85.             self.unshift(new_value.new_value)
  86.         else:
  87.             current = self.head
  88.            
  89.             for i in range(self.size):
  90.                 if i == index:
  91.                     current.prev.next = new_value
  92.                     new_value.prev = current.prev
  93.                     new_value.next = current
  94.                     current.prev = new_value
  95.                 else:
  96.                     current = current.next
  97.                    
  98.             self.size += 1
  99.  
  100.  
  101. list = LinkedList()
  102. n = int(input())
  103. if n > 0: elements = input().split()
  104.  
  105. for i in range(n):
  106.     list.push(float(elements[i]))
  107.    
  108. m = int(input())
  109.  
  110. for i in range(m):
  111.     commands = input().split()
  112.     commandType = commands[0]
  113.     commandValue = None
  114.     if len(commands) > 1:
  115.         commandValue = command[1]
  116.    
  117.     if commandType == 'push':
  118.         list.push(float(commandValue))
  119.     elif commandType == 'pop':
  120.         list.pop()
  121.     elif commandType == 'unshift':
  122.         list.unshift(float(commandValue))
  123.     elif commandType == 'shift':
  124.         list.shift()
  125.     elif commandType == 'insert':
  126.         list.insert(int(commandValue), float(command[2]))
  127.        
  128. list.print()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top