Exemplo n.º 1
0
def testLinkedList():
    ll = LinkedList()
    while True:
        print ("Choose operation: ")
        print (" 1 - Add\n",
               "2 - Remove\n",
               "3 - Search\n",
               "4 - Size\n",
               "5 - Exit\n")
        choice = input()
        if choice == '1':
            ll = addll(ll)
            ll.__str__()
        elif choice == '2':
            ll == removell(ll)
            ll.__str__()
        elif choice == '3':
            searchKey(ll)
            ll.__str__()
        elif choice == '4':
            size(ll)
            ll.__str__()
        elif choice == '5':
            break
        else:
            print ("BAD Choice! Choose from 1 to 4 numbers")
Exemplo n.º 2
0
class Queue:
    def __init__(self):
        self.__elements = LinkedList()

    # Adds an element to this queue
    def enqueue(self, e):
        self.__elements.add(e)
    
    # Removes an element from this queue
    def dequeue(self):
        if self.getSize() == 0:
            return None
        else:
            return self.__elements.removeAt(0)
    
    # Return the size of the queue
    def getSize(self):
        return self.__elements.getSize()
    
    # Returns a string representation of the queue
    def __str__(self):
        return self.__elements.__str__()

    # Return true if queue is empty 
    def isEmpty(self):
        return self.getSize() == 0
Exemplo n.º 3
0
class Queue:
    def __init__(self):
        self.__elements = LinkedList()

    # Adds an element to this queue
    def enqueue(self, e):
        self.__elements.add(e)
    
    # Removes an element from this queue
    def dequeue(self):
        if self.getSize() == 0:
            return None
        else:
            return self.__elements.removeAt(0)
    
    # Return the size of the queue
    def getSize(self):
        return self.__elements.getSize()
    
    # Returns a string representation of the queue
    def __str__(self):
        return self.__elements.__str__()

    # Return true if queue is empty 
    def isEmpty(self):
        return self.getSize() == 0
Exemplo n.º 4
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.º 5
0
class Stack(object):
    def __init__(self, arr=[]):
        self.stack = LinkedList(arr)

    def __str__(self):
        return self.stack.__str__()

    def getSize(self):
        return self.stack.getLength()

    def push(self, val):
        self.stack.push(val)

    def pop(self):
        return self.stack.pop()

    def peek(self):
        return self.stack.get(0)
Exemplo n.º 6
0
class Stack:
    """A Stack Datatype Implementation using a singly Linked List."""
    def __init__(self):
        self.top = None
        self.linked_list = LinkedList()

    def __str__(self):
        return self.linked_list.__str__()

    def push(self, value):
        self.linked_list.insert_at_head(value)
        self.top = self.linked_list.head.value

    def pop(self):
        popped_value_to_return = self.linked_list.head.value
        self.linked_list.delete_by_index(0)
        self.top = self.linked_list.head.value
        return popped_value_to_return

    def peek(self):
        return self.top
Exemplo n.º 7
0
        comment("OK -> " + test_name)
        grade(points['ll_1e_getsize'])
except:
    e = sys.exc_info()[1]
    comment("FAIL -> " + test_name + "\n" + str(e))
    grade(0)


# Check one element list - __str__()


try:
    test_name = 'LinkedList con un elemento: __str__'
    l = LinkedList()
    l.insertAfter(17)
    if type(str(l)) == types.StringType and not l.__str__().count('17') != 1:
        comment("OK -> " + test_name)
        grade(points['ll_1e_str'])
    else:
        comment("FAIL -> " + test_name +"\n __str__ debe devolver una lista de caracteres (string) con los elementos de la lista")
        grade(0)
except:
    e = sys.exc_info()[1]
    comment("FAIL -> " + test_name + "\n" + str(e))
    grade(0)


# Check one element list - remove()


try:
Exemplo n.º 8
0
from LinkedList import LinkedList

# Test cases for LinkedList
linklist = LinkedList()
linklist2 = LinkedList()
linklist.add(1)
print(linklist.__str__())
linklist.add(2)
print(linklist.__str__())
linklist.add(3)
print(linklist.__str__())
linklist.add(4)
print(linklist.__str__())
linklist.remove(2)
print(linklist.__str__())
linklist.remove(1)
linklist.remove(6)
print(linklist.contains(1))
print(linklist.__str__())
print(linklist.size)