def test_sublist_split(self): # even-length list self.class_under_test.insert(s.Node(1)) self.class_under_test.insert(s.Node(2)) self.class_under_test.insert(s.Node(3)) self.class_under_test.insert(s.Node(4)) first, second = s.sublistSplit(self.class_under_test) self.assertTrue(first.count == 2) self.assertTrue(first.delete() == 4) self.assertTrue(first.delete() == 3) self.assertTrue(second.count == 2) self.assertTrue(second.delete() == 2) self.assertTrue(second.delete() == 1) # odd-length list # self.class_under_test is the same as first above, which was emptied by the delete() self.class_under_test.insert(s.Node(1)) self.class_under_test.insert(s.Node(2)) self.class_under_test.insert(s.Node(3)) self.class_under_test.insert(s.Node(4)) self.class_under_test.insert(s.Node(5)) first, second = s.sublistSplit(self.class_under_test) self.assertTrue(first.count == 3) self.assertTrue(first.delete() == 5) self.assertTrue(first.delete() == 4) self.assertTrue(first.delete() == 3) self.assertTrue(second.count == 2) self.assertTrue(second.delete() == 2) self.assertTrue(second.delete() == 1)
def __init__(self, pub_key, start_amount, sign): glob.goofy_list = ll.SinglyLinkedList() genesis = node.ListNode(start_amount) genesis.me = pub_key genesis.id = datetime.datetime.now() genesis.signature = sign genesis.is_minted = True glob.goofy_list.add_node(genesis)
def test_insert_into_cyclic_sorted_list(self): node = None node = s.insert_into_cyclic_sorted_list_node(node, 1) self.assertTrue(node) self.assertTrue(node.getNext() == node) node5 = s.Node(5) node4 = s.Node(4) node3 = s.Node(3) node1 = s.Node(1) self.class_under_test.insert(node5) node5.setNext(node1) self.class_under_test.insert(node4) self.class_under_test.insert(node3) self.class_under_test.insert(s.Node(2)) self.class_under_test.insert(node1) # insert element larger than max in list self.assertTrue(node5.getNext().getData()==1) s.insert_into_cyclic_sorted_list_node(node3, 6) self.assertTrue(self.class_under_test.isCyclic()) self.assertTrue(node5.getNext().getData()==6) # insert element smaller than min in list s.insert_into_cyclic_sorted_list_node(node3, 0) self.assertTrue(self.class_under_test.isCyclic()) self.assertTrue(node5.getNext().getNext().getData()==0) # insert element in range of list self.assertTrue(node4.getNext().getData()==5) s.insert_into_cyclic_sorted_list_node(node3, 4) self.assertTrue(self.class_under_test.isCyclic()) self.assertTrue(node4.getNext().getData()==4)
def getValues(self): result = SinglyLinkedList.SinglyLinkedList() tempIndex = 0 while tempIndex < self.__elements.size(): element = self.__elements.get(tempIndex) if element is not None and element.isStatus() is True: result.add(element.getValue()) tempIndex += 1 return result
def main(): mylist = SinglyLinkedList() intersected_node = Node(4) mylist.insert_node(1) mylist.insert_node(2) mylist2 = SinglyLinkedList() mylist2.insert_node(4) mylist2.insert_node(2) print(intersection(mylist, mylist2))
def find_ordered_alphabet(dictionary): """This method finds one of the possible lexicographic orders of dictionary characters given an ordered dictionary words. Args: dictionary: an array of strings, list of language words in lexicographical order Returns: a list of characters, one the the possible lexicographical orderings of dictionary characters. Throws: ValueError: if the passed dictionary is empty or the word order in it is not valid. """ # Empty dictionary. if len(dictionary) == 0: raise ValueError("The given dictionary is empty!") # Create the partial ordering of dictionary characters. partial_order = create_corresponding_partial_order(dictionary) # Instantiate states to _WHITE for all characters state = initialize_state_dictionary(dictionary) # Initialize the singly linked list. It will contain the total order of characters "accumulated so far". total_order = SinglyLinkedList.SinglyLinkedList() # Perform a topological sort. for char in partial_order: if state[char] == _WHITE: find_total_order_from_char(partial_order, state, char, total_order) # Copy values from singly linked list to array of characters in order. ordered_alphabet = [] current_char_node = total_order.get_head() while current_char_node is not None: ordered_alphabet.append(current_char_node.get_data()) current_char_node = current_char_node.get_next_node() return ordered_alphabet
import SinglyLinkedList def reverseLinkedList(myLinkedList): previous = None current = myLinkedList.head while (current != None): temp = current.next current.next = previous previous = current current = temp myLinkedList.head = previous if __name__ == '__main__': myLinkedList = SinglyLinkedList.LinkedList() for i in range(10, 0, -1): myLinkedList.insertAtStart(i) print('Original:', end=' ') myLinkedList.printLinkedList() print() print('Reversed:', end=' ') reverseLinkedList(myLinkedList) myLinkedList.printLinkedList() # OUTPUT: # Original: 1 2 3 4 5 6 7 8 9 10 # Reversed: 10 9 8 7 6 5 4 3 2 1
import SinglyLinkedList as ListClass if __name__ == "__main__": ssl = ListClass.SinglyLinkedList() ssl.addFirst(9) ssl.addLast(10) ssl.addLast(2) ssl.toString()
__author__ = 'ankit.b.patel' import SinglyLinkedList import DoublyLinkedList if __name__ == "__main__": #-----------------Implementation of Singly Linked List------------------# ll = SinglyLinkedList.LinkedList() print "Before Adding Nodes :" ll.Print() ll.AddNode(1) ll.AddNode(2) ll.AddNode(1) ll.AddNode(1) ll.AddNode(1) ll.AddNode(3) ll.AddNode(4) ll.AddNode(5) ll.AddNode(1) ll.AddNode(5) print "After Adding Nodes :" ll.Print() ll.InsertNodeAfterNode(ll.GetNodeAtPosition(1), 5) print "After Inserting node (data=5) at position 2" ll.Print() ll.AddNodeInFront(10) print "After Inserting node at Front" ll.Print() ll.RemoveDuplicates_withbuffer()
def __init__(self): self.__elements = SinglyLinkedList.SinglyLinkedList() self.__currentSize = 0
import SinglyLinkedList as SLL _list = SLL.SinglyLinkedList() print("Making empty _list...") print(_list, "length =",_list.get_length()) _list.append(5) print("appending 5...") print(_list, "length =",_list.get_length()) _list.append(4) _list.extend(_list) print("appending 4 then extending by self...") print(_list, "length =",_list.get_length()) _list.insert(0, 3) print("inserting 3 at index 0") print(_list, "length =",_list.get_length()) _list.insert(3, 1) print("inserting 1 at index 3") print(_list, "length =",_list.get_length()) _list.insert(_list.get_length(), 0) print("inserting 0 at last index") print(_list, "length =",_list.get_length()) _list.reverse() print("reversing _list...") print(_list, "length =",_list.get_length()) print("popping from _list...", _list.pop()) print(_list, "length =",_list.get_length()) print("popping from front...", _list.pop(0)) print(_list, "length =", _list.get_length()) print("popping from index 3...", _list.pop(3)) print(_list, "length =", _list.get_length()) print("removing 5", _list.remove(5))
def __init__(self): self.list = SinglyLinkedList()
class LineEditor: def __init__(self): self.list = SinglyLinkedList() def runLineEditor(self): while True: command = input( "i-insert, d-delete, r- replace, p- print, l- loadfile, s- writefile, q-quit -> " ) if command == 'i': self.addLine() elif command == 'd': self.deleteLine() elif command == 'r': self.replaceLine() elif command == 'p': self.printByLine() elif command == 'l': self.loadFromFile() elif command == 's': self.writeToFile() elif command == 'q': return def addLine(self): pos = int(input(" input line number: ")) str = input("input line text ") self.list.addAt(pos, str) def deleteLine(self): pos = int(input("input line number: ")) self.list.deleteAt(pos) def replaceLine(self): pos = int(input("input line number: ")) str = input("input modified: ") self.list.replaceDFateAT(pos, str) def printByLine(self): self.list.printByLine() def loadFromFile(self): filename = 'test.txt' with open(filename, "r") as infile: lines = infile.readline() for line in lines: self.list.addAt(self.list.getSize(), line.rstrip('\n')) def writeToFile(self): filename = 'test.txt' with open(filename, 'w') as outfile: sz = self.list.getSize() print(sz) for i in range(sz): outfile.write(self.list.getDataAt(i) + '\n')
def __init__(self): self.__sll = sll.SinglyLinkedList()