Ejemplo n.º 1
0
class Stack:  #Last in First Out
    def __init__(self):

        self.newlist = UnorderedList()  #creates new empty list

    def isEmpty(self):

        #newlist = UnorderedList()
        return self.newlist.isEmpty()  # see whether list is empty

    def push(self, item):
        # newlist = UnorderedList()
        return self.newlist.append(item)  #add

    def pop(self):
        #  newlist = UnorderedList() #pop() The front would be index 0, end will be size() -1
        return self.newlist.pop(self.newlist.size() - 1)

        #https://piazza.com/class/jtqbsk4spmk58r?cid=89
    def peek(self):  #Consider peek() as popping an item off the stack
        #and then pushing it back on the stack before returning it.
        # newlist = UnorderedList()
        item = self.newlist.pop(self.newlist.size() - 1)
        self.newlist.append(item)
        return item  # askingfor item
        #return self.newlist.append(item)

    def size(self):
        #  newlist = UnorderedList()
        return self.newlist.size()
Ejemplo n.º 2
0
class Queue():
    def __init__(self):
        self.items = UnorderedList()

    def is_empty(self):
        return self.items.isEmpty()

    def enqueue(self, item):
        self.items.insert(0, item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return self.items.size()
Ejemplo n.º 3
0
class StackAsUnorderedList:
    def __init__(self):
        self.stack = UnorderedList(
        )  # Stack is client to UnorderedList, the supplier

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

    def push(self, item):
        self.stack.add(item)

    def pop(self):
        if self.stack.isempty():
            return 'Stack is empty'
        else:
            temp = self.stack.head.data
            self.stack.head = self.stack.head.next
            return temp

    def peek(self):
        if self.stack.isempty():
            return 'Stack is empty'
        else:
            return self.stack.head.data

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

    def __str__(self):
        return self.stack.__str__()
Ejemplo n.º 4
0
class Queue:  #FIFO
    def __init__(self):  #initialize
        self.newlist = UnorderedList()

    def isEmpty(self):  #checking empty list
        #newlist = UnorderedList()
        return self.newlist.isEmpty()

    def enqueue(self, item):  # enqueue
        return self.newlist.append(item)
        #newlist = UnorderedList()
        #newlist.append(item)
        #newlist.size +=1

    def dequeue(self):  #dequeue
        #  newlist = UnorderedList()
        return self.newlist.pop(
            0)  #The front would be index 0, end will be size() -1

    def size(self):  #return size
        # newlist = UnorderedList()
        return self.newlist.size()
Ejemplo n.º 5
0
class StackLinkedList:
    """Represent a Stack using a linked list."""
    def __init__(self):
        self.items = UnorderedList()

    def push(self, item):
        self.items.add(item)

    def pop(self):
        item = self.items.head.getData()
        self.items.head = self.items.head.getNext()
        self.items.size -= 1
        return item

    def peek(self):
        return self.items.head.getData()

    def isEmpty(self):
        return self.items.head == None

    def size(self):
        return self.items.size
Ejemplo n.º 6
0
class Deque:  #Double End Queue
    def __init__(self):
        self.newlist = UnorderedList()

    def isEmpty(self):
        return self.newlist.isEmpty()

    def addFront(self, item):  #insert front
        self.newlist.insert(0, item)

    def addRear(self, item):  # add newest number and new position at the end
        self.newlist.append(item)

    def removeFront(self):
        return self.newlist.pop(0)  # 0 front index

    def removeRear(self):  #https://piazza.com/class/jtqbsk4spmk58r?cid=151
        return self.newlist.pop(self.newlist.size() - 1)

    def size(self):
        return self.newlist.size()
Ejemplo n.º 7
0
class Deque():
    def __init__(self):
        self.items = UnorderedList()

    def add_front(self, item):
        self.items.append(item)

    def remove_front(self):
        return self.items.pop()

    def add_rear(self, item):
        self.items.insert(0, item)

    def remove_rear(self):
        return self.items.pop(0)

    def size(self):
        return self.items.size()

    def is_empty(self):
        return self.items.isEmpty()
Ejemplo n.º 8
0
class Stack():
    def __init__(self):
        self.items = UnorderedList()

    def is_empty(self):
        return self.items.isEmpty()

    def push(self, item):
        self.items.append(item)

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

    def peek(self):
        return int(self.items.slice()[-2])

    def size(self):
        return self.items.size()
Ejemplo n.º 9
0
    def __init__(self):

        self.newlist = UnorderedList()  #creates new empty list
Ejemplo n.º 10
0
class Map():
    def __init__(self):
        self.slots = UnorderedList()
        self.data = UnorderedList()

    def __setitem__(self, key, value):
        if self.slots.search(key):
            index = self.slots.index(key)
            self.data.insert(index, value)
            self.data.pop(index + 1)

        else:
            self.slots.append(key)
            self.data.append(value)

    def __getitem__(self, key):
        index = self.slots.index(key)

        if (index == int()):
            return "{} doesn't exist.".format(key)

        values = self.data.head

        for i in range(index):
            values = values.getNext()

        return values.getData()

    def __delitem__(self, key):
        index = self.slots.index(key)

        self.slots.pop(index)
        self.data.pop(index)

    def __str__(self):
        if self.slots.isEmpty():
            return "{}"

        else:
            keys = self.slots.head
            values = self.data.head
            item_list = '{'

            while keys != None:
                item_list += str(keys.getData()) + ": " + str(values.getData())
                if keys.getNext() != None:
                    item_list += ", "

                else:
                    item_list += '}'

                keys = keys.getNext()
                values = values.getNext()

        return item_list

    def __len__(self):
        return len(self.slots)

    def __contains__(self, key):
        return self.slots.search(key)
Ejemplo n.º 11
0
 def __init__(self):
     self.newlist = UnorderedList()
Ejemplo n.º 12
0
 def __init__(self):
     self.stack = UnorderedList(
     )  # Stack is client to UnorderedList, the supplier
Ejemplo n.º 13
0
__author__ = 'Gaurav'
import LinkedListNode
from UnorderedList import UnorderedList

#driver code to test logic

linked_list = UnorderedList()

#add at head of linked list
linked_list.add_at_head(4)
linked_list.add_at_head(4)
linked_list.add_at_head(4)
linked_list.add_at_head(4)
linked_list.add_at_head(56)
linked_list.add_at_head(58)
linked_list.add_at_head(4)
linked_list.add_at_head(58)
#add at the tail of linked list
linked_list.add_at_tail(33)

#delete some element
linked_list.delete_element_at(0)

#print contents of linked list
linked_list.print_list()

#delete the duplicate
linked_list.remove_duplicates_with_buffer()
#print contents of linked list
linked_list.print_list()
Ejemplo n.º 14
0
 def __init__(self):
     self.slots = UnorderedList()
     self.data = UnorderedList()
Ejemplo n.º 15
0
 def __init__(self):  #initialize
     self.newlist = UnorderedList()
Ejemplo n.º 16
0
#!/usr/bin/python

import random
from UnorderedList import UnorderedList

myList = UnorderedList()

for i in range(1,11):
    n = random.randint(1,100)
  #  print "%d> %d" % (i,n)
    myList.add(n)

myList.add(12)
myList.add(24)

print myList.size()
print "search", myList.search(12)

print "last: %d" % myList.getLast().getData()

myList.printList()

myList.append(40)

print
myList.printList()
print


print "idx %d, %d" % (12, myList.index(12))
Ejemplo n.º 17
0
from UnorderedList import UnorderedList
from OrderedList import OrderedList

name_list = ["Gill", "Tom", "Eduardo", "Raffaele", "Serena", "Bella"]
my_unorderedlist = UnorderedList()
for name in name_list:
    my_unorderedlist.add(name)

for item in my_unorderedlist:
    print(item, end=" ")

print()
my_orderedlist = OrderedList()

for name in name_list:
    my_orderedlist.add(name)

for item in my_orderedlist:
    print(item, end=" ")
print()
Ejemplo n.º 18
0
from UnorderedList import UnorderedList, Node

ul = UnorderedList()
ul.add(3)
ul.add(34)
ul.add(53)
ul.add(2)
ul.insert(44, 77)
ul.remove(34)
ul.append(1)
ul.display()
print(ul.pop(1))
ul.display()
print(ul.index(2))
print(ul.length())
print(ul.search(77))

Ejemplo n.º 19
0
from Node import Node
from UnorderedList import UnorderedList

mylist = UnorderedList()

mylist.add(3)
print(mylist.isEmpty())

mylist.add(2)
mylist.add(12)
mylist.add(123)
mylist.add(18)
mylist.add(73)

print(mylist.size())

print(mylist.search(123))
print(mylist.search(19))

print(mylist.size())

mylist.remove(2)
print(mylist.size())
Ejemplo n.º 20
0
 def __init__(self):
     self.items = UnorderedList()
Ejemplo n.º 21
0
from UnorderedList import UnorderedList


def print_list(ul):
    node = ul.head

    while node != None:
        print(node.get_data(), end=" ")
        node = node.get_next()

    print()


l = UnorderedList()

for i in range(10):
    l.append(i)

print_list(l)

l.add(-1000)
l.append(-3000)
print_list(l)

# l.remove(-10)
l.remove(-1000)
print_list(l)

l.remove(-3000)
print_list(l)