class Queue(object): ''' A linked list implementation of a queue. This contains a LinkedList internally. It does not extend LinkedList. In other words, this class uses "Composition" rather than "Inheritance". ''' def __init__(self): '''Constructor''' self.queue = LinkedList() def debug_print(self): '''Prints a representation of the entire queue.''' self.queue.debug_print() def enqueue(self, item): '''Adds an item to the end of the queue''' self.queue.add(item) def dequeue(self): ''' Dequeues the first item from the list. This involves the following: 1. Get the first node in the list. 2. Delete the node from the list. 3. Return the value of the node. ''' beg = self.queue._get_node(0) self.queue.delete(0) return beg.value def size(self): '''Returns the number of items in the queue''' return self.queue.size
def test(): ll = LinkedList() ll.add("a") ll.add("b") ll.debug_print() ll.set(1, "B") ll.debug_print() ll.add("c") ll.debug_print() # ll.delete(3) ll.swap(0, 1) ll.debug_print()
def array_runner(command, arg1, arg2): global a if arg1.isdigit(): arg1 = int(arg1) if arg2.isdigit(): arg2 = int(arg2) if command == 'CREATE': a = LinkedList() elif command == 'DEBUG': a.debug_print() elif command == 'ADD': a.add(arg1) elif command == 'SET': a.set(arg1, arg2) elif command == 'GET': a.get(arg1) elif command == 'DELETE': a.delete(arg1) elif command == 'INSERT': a.insert(arg1, arg2) elif command == 'SWAP': a.swap(arg1, arg2) return []
from linkedlist_api import LinkedList import csv linkedList = LinkedList() linkedList.add(1) linkedList.add(2) linkedList.add(3) linkedList.debug_print() linkedList.set(1,0) a=linkedList.get(1) linkedList.debug_print() linkedList.swap(0,1) print(a) linkedList.debug_print() linkedList.insert(1,9) linkedList.debug_print() linkedList.delete(0) linkedList.debug_print() with open('data_example.csv', newline='') as csvfile: reader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in reader: items = row[0].split(",",1) parameters=items[1].split(",") action = items[0] if action == 'CREATE': linkedList = LinkedList() elif action == 'DEBUG': linkedList.debug_print() elif action == 'ADD':
import sys orig_stdout = sys.stdout f = open('output.txt', 'w') sys.stdout = f line_count = 0 with open('data.csv') as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: print("{}:{}".format(line_count, ','.join(str(item) for item in row))) command = row[0] if command == 'CREATE': linked_list = LinkedList() elif command == 'DEBUG': linked_list.debug_print() elif command == 'ADD': linked_list.add(row[1]) elif command == 'GET': linked_list.get(int(row[1])) elif command == 'SET': linked_list.set(int(row[1]), row[2]) elif command == 'INSERT': linked_list.insert(int(row[1]), row[2]) elif command == 'DELETE': linked_list.delete(int(row[1])) elif command == 'SWAP': linked_list.swap(int(row[1]), int(row[2])) line_count += 1