Exemplo n.º 1
0
class Queue(object):

    def __init__(self):
        self.__Llist = LinkedList()

    def __len__(self):
        return self.__Llist.__len__()

    def __str__(self):
        """
        Returns a STRING that contains every element in the Queue.
        Top of the Queue indicated by "->" character.
        """
        return "-> " + self.__Llist.__str__()


    def isEmpty(self):
        """
        True if current Queue is empty, False otherwise.
        """
        return self.__Llist.isEmpty()


    def add(self, data):
        """
        Inserts a new element DATA at the end of the Queue.
        Runtime: O(1)
        """
        self.__Llist.addLast(data)

    def peek(self):
        """
        Returns the element at the top of the Queue withouth
        removing it.
        Runtime: O(1)
        """
        dataPeeked = None

        try:
            dataPeeked = self.__Llist.getFirst()
        except IndexError: #as e:
            print("Cannot peek on an empty Queue")

        return dataPeeked
    
    def poll(self):
        """
        Removes and returns the element at the top of the Queue.
        Runtime: O(1)
        """
        dataPopped = None

        try:
            dataPopped = self.__Llist.removeFirst()
        except IndexError: #as e:
            print("Cannot pop from an empty Queue")

        return dataPopped
Exemplo n.º 2
0
def sumList(l1, l2):
	# edge case
	if l1.head is None and l2.head is None:
		return 0
	elif l1.head is None:
		return l2
	elif l2.head is None:
		return l1

	cur1 = l1.head
	cur2 = l2.head

	res = LinkedList()
	c = value = 0

	while (cur1 or cur2):
		value = value + c

		if cur1:
			value += cur1.value
			cur1 = cur1.next

		if cur2:
			value += cur2.value
			cur2 = cur2.next

		c = value // 10
		value = value % 10

		res.add(value)
		value = 0

	if c == 1:
		res.add(1)

	# edge case
	tmp2 = res.head

	while (tmp2.next != res.tail):
		tmp2 = tmp2.next

	if (res.tail.value == 0) and (res.__len__() > 1):
		tmp2.next = None

	return res
Exemplo n.º 3
0
        next_node         = current_node.next                    
        current_node.next = None

        if current_node.value < pivot:
            current_node.next = list.head
            list.head         = current_node
        else:
            list.tail.next = current_node 
            list.tail      = current_node

        current_node = next_node

    

if __name__ == "__main__":

    test_list = LinkedList()
    test_list.add_multiple([3,5,8,5,10,2,1])    #linked list from example.

    """print(test_list)
    print(partition(test_list, 5))
    print(partition_in_place(test_list, 5))
    print(test_list)
"""
    ll = LinkedList()
    ll.generate(10, 0, 99)
    print(ll)
    partition_in_place(ll, 50) #ll.head.value)
    print(ll)
    print(ll.__len__())
    
Exemplo n.º 4
0
def incluirProduto(nome, preco, quantidade):
    if nome and preco and quantidade:
        listaNome.append(nome)
        listaPreco.append(preco)
        listaQuantidade.append(quantidade)
        print ("Inclusão realizada com sucesso.")
    else:
        print ('Falha. É necessário informar os três parâmetros para realizar a inclusão.')

def imprimirProdutos():
    print('Nomes: ')
    listaNome.imprimir()
    print('Preços: ')
    listaPreco.imprimir()
    print('Quantidades: ')
    listaQuantidade.imprimir()



nome = str(input('Informe o nome do produto: '))
preco = input('Informe o preco do produto: ')
quantidade = input('Informe a quantidade do produto: ')

incluirProduto(nome, preco, quantidade)

print ('Número de elementos da lista: ', listaNome.__len__())

imprimirProdutos()