Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__ (self,value):
- self.value = value
- self.next = None
- self.prev = None
- class LinkedList:
- def __init__ (self):
- self.head = None
- self.tail = None
- self.size = 0
- def push(self, value):
- value = Node(value)
- if self.size >= 1:
- value.prev = self.tail
- self.tail.next = value
- else:
- self.head = value
- self.tail = value
- self.size += 1
- def pop(self):
- deleted = self.tail
- if self.size > 1:
- self.tail = self.tail.prev
- self.tail.next = None
- self.size -= 1
- if self.size == 1:
- self.tail = None
- self.size -= 1
- return deleted
- def unshift(self, value):
- value = Node(value)
- if self.size == 0:
- self.tail = value
- else:
- value.next = self.head
- self.head.prev = value
- self.head = value
- self.size += 1
- def shift(self):
- deleted = self.head
- self.head = self.head.next
- if self.size > 1:
- self.head.prev = None
- self.size -= 1
- return deleted
- def find(self, v):
- current = self.head
- for i in range(self.size):
- if current.value == v:
- return current
- current = current.next
- return None
- def get(self, index):
- current = self.head
- for i in range(self.size):
- if i == index:
- return current
- current = current.next
- def size(self):
- return self.size()
- def print(self):
- current = self.head
- output = ''
- if self.size > 0:
- for i in range(self.size):
- output += str(current.value) + ' '
- current = current.next
- print(output)
- def insert(self, index, value):
- value = Node(value)
- if index >= self.size - 1:
- self.push(value.value)
- elif index == 0:
- self.unshift(value.value)
- else:
- current = self.head
- for i in range(self.size):
- if i == index:
- current.prev.next = value
- value.prev = current.prev
- value.next = current
- current.prev = value
- else:
- current = current.next
- self.size += 1
- def printReverse(self):
- current = self.tail
- output = ''
- if self.size > 0:
- for i in range(self.size):
- output += str(current.value) + ' '
- current = current.prev
- print(output)
- list = LinkedList()
- n = int(input())
- if n > 0: elements = input().split()
- for i in range(n):
- list.push(float(elements[i]))
- m = int(input())
- for i in range(m):
- commands = input().split()
- commandType = commands[0]
- commandValue = None
- if len(commands) > 1:
- commandValue = commands[1]
- if commandType == 'push':
- list.push(float(commandValue))
- elif commandType == 'pop':
- list.pop()
- elif commandType == 'unshift':
- list.unshift(float(commandValue))
- elif commandType == 'shift':
- list.shift()
- elif commandType == 'insert':
- list.insert(int(commandValue), float(commands[2]))
- list.printReverse()
- list.print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement