Exemplo n.º 1
0
class LinkedQueue:
    def __init__(self):
        self.data = Doubly_Linked_List()

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

    def is_empty(self):
        return self.data.is_empty()

    def first(self):
        if self.is_empty():
            raise Empty("Queue is Empty")
        return self.data.first()

    def dequeue(self):  # O(1)
        if self.is_empty():
            raise Empty("Queue is Empty")
        return self.data.delete_first()

    def enqueue(self, e):  # O(1)
        self.data.insert_last(e)

    def __str__(self):
        return str(self.data)
Exemplo n.º 2
0
class LinkedQueue():
    def __init__(self, N=10):
        self._data = Doubly_Linked_List()
        self._size = 0

    def __len__(self):
        return self._size

    def is_empty(self):
        return self._size == 0

    def first(self):  #O(1)
        if self.is_empty():
            raise Empty("Queue is Empty")
        return self._header._next._element

    def dequeue(self):  #O(1)
        if self.is_empty():
            raise Empty("Queue is Empty")
        self._size -= 1
        return self._data.delete_first()  ##???

    def enqueue(self, e):  #O(1)
        self._size += 1
        self._data.insert_last(e)  ##???

    def __str__(self):
        ''' You can simply print self._data '''
        return str(self._data)  #print directly or as string
Exemplo n.º 3
0
 def __init__(self, orig_str):
     self._data = Doubly_Linked_List()
 	for i in range(len(orig_str)):
 		if i > 0 and orig_str[i] !=  orig_str[i-1]:
 			self._data.insert_last((orig_str[i], 1))
 		else:
 			mylast = self._data.last()
 			tup1 = mylast[1]
 			self._data._trailer._prev._element = (orig_str[i], tup1+1)
Exemplo n.º 4
0
class Integer():
    def __init__(self, num_str=''):
        self._data = Doubly_Linked_List()
        for i in range(len(num_str)):
            self._data.insert_last(num_str[i])

    def __add__(self, other):
        carrier = 0
        newInt = 0
        result = Integer()
        currNode = self._data._trailer
        myNode = other._data._trailer
        if self._data.is_empty() or other._data.is_empty():
            raise Exception("invalid input")
        while currNode._prev is not self._data._header:
            currNode = currNode._prev
            if myNode._prev is not self._data._header:
                myNode = myNode._prev
                newInt = int(currNode._element) + int(myNode._element)
            if carrier != 0:
                newInt += 1
                carrier -= 1
            if newInt >= 10:
                newInt -= 10
                carrier += 1
            result._data.insert_first(str(newInt))

        if currNode._prev == self._data._header:
            mybool = False
            while myNode._prev is not other._data._header:
                myNode = myNode._prev
                result._data.insert_first(myNode._element)
        if myNode._prev == other._data._header:
            obool = False
            while currNode._prev is not self._data._header:
                currNode = currNode._prev
                result._data.insert_first(currNode._element)
        return result

    def __repr__(self):
        repstr = ''
        currNode = self._data._header._next
        while currNode is not self._data._trailer:
            repstr += currNode._element
            currNode = currNode._next
        return repstr


# n1 = Integer('375')
# n2 = Integer('4029')
# print(n1)
# print(n2)
# n3 = n1 + n2
# print(n3)
Exemplo n.º 5
0
 def merge_sublists(srt_lnk_lst1, srt_lnk_lst2, Node1, Node2):
     if Node1._element is None and Node2._element is None:
         new = Doubly_Linked_List()
         return new
     else:
         if Node1._element is not None and Node2._element is not None:
             if Node1._element < Node2._element:
                 now = merge_sublists(srt_lnk_lst1, srt_lnk_lst2,
                                      Node1._next, Node2)
                 now.insert_first(Node1._element)
             else:
                 now = merge_sublists(srt_lnk_lst1, srt_lnk_lst2, Node1,
                                      Node2._next)
                 now.insert_first(Node2._element)
             return now
     if Node1._element is not None and Node2._element is None:
         now = merge_sublists(srt_lnk_lst1, srt_lnk_lst2, Node1._next,
                              Node2)
         now.insert_first(Node1._element)
         return now
     if Node1._element is None and Node2._element is not None:
         now = merge_sublists(srt_lnk_lst1, srt_lnk_lst2, Node1,
                              Node2._next)
         now.insert_first(Node2._element)
         return now
Exemplo n.º 6
0
class CompactString():

    def __init__(self, orig_str):
        self._data = Doubly_Linked_List()
    	for i in range(len(orig_str)):
    		if i > 0 and orig_str[i] !=  orig_str[i-1]:
    			self._data.insert_last((orig_str[i], 1))
    		else:
    			mylast = self._data.last()
    			tup1 = mylast[1]
    			self._data._trailer._prev._element = (orig_str[i], tup1+1)

    def __add__(self, other):
    	if self._trailer._prev._element[0] == other._header._next._element[0]:

    	else: 
    		self._trailer._prev._next = other._header._next
    		other._header._next._prev = self._trailer._prev


        for
    def __lt__(self, other):
    	if self._data._header._next == self._data._trailer and other._data._header._next == other._data._trailer: 
    		return False
    	elif other._data._header._next == self._data._trailer: 
    		return False
    	elif self._data._header._next == self._data._trailer: 

    def __eq__(self, other):
    	if self._data._header._next == self._data._trailer and other._data._header._next == other._data._trailer: 
    		return True

    def __le__(self, other):

    	if self == other: 
    		return True 
        
    def __gt__(self, other):
        
    def __ge__(self, other):

    	if self == other: 
    		return True 

    def __repr__(self):
Exemplo n.º 7
0
	def merge_sublists(Node1, Node2):
		if Node1 == srt_lnk_lst1._trailer._prev and Node2 == srt_lnk_lst2._trailer._prev:
			new = Doubly_Linked_List()
			return new 
		elif Node1 == srt_lnk_lst1._trailer._prev and Node2 != srt_lnk_lst2._trailer._prev:
			now = merge_sublists(Node1, Node2._next)
			rtiujh6iu jthiijotormnwhile Node2 is not srt_lnk_lst2._trailer._prev:
				now.insert_last(Node2._element)
			return now
Exemplo n.º 8
0
class Integer():
    def __init__(self, num_str=''):
        self._data = Doubly_Linked_List()
        for i in range(len(num_str)):
            self._data.insert_last(num_str[i])

    def __add__(self, other):
        carrier = 0
        newInt = 0
        result = Integer()
        currNode = self._data._trailer
        myNode = other._data._trailer
        while currNode._prev is not self._data._header:
            currNode = currNode._prev
            if myNode._prev is not self._data._header:
                myNode = myNode._prev
                newInt = int(currNode._element) + int(myNode._element)
            if carrier != 0:
                newInt += 1
                carrier -= 1
            if newInt >= 10:
                newInt -= 10
                carrier += 1
            result._data.insert_first(str(newInt))

        if currNode._prev == self._data._header:
            mybool = False
            while myNode._prev is not other._data._header:
                myNode = myNode._prev
                result._data.insert_first(myNode._element)
        if myNode._prev == other._data._header:
            obool = False
            while currNode._prev is not self._data._header:
                currNode = currNode._prev
                result._data.insert_first(currNode._element)
        return result

    def __repr__(self):
        repstr = ''
        currNode = self._data._header._next
        while currNode is not self._data._trailer:
            repstr += currNode._element
            currNode = currNode._next
        return repstr
Exemplo n.º 9
0
 def __init__(self, num_str=''):
     self._data = Doubly_Linked_List()
     for i in range(len(num_str)):
         self._data.insert_last(num_str[i])
Exemplo n.º 10
0
 def __init__(self):
     self.data = Doubly_Linked_List()
Exemplo n.º 11
0
		elif Node2 == srt_lnk_lst2._trailer._prev and Node1 != srt_lnk_lst1._trailer._prev:
			now = merge_sublists(Node1._next, Node2)
			while Node1 is not srt_lnk_lst1._trailer._prev:
				now.insert_last(Node1._element)
			return now
		else: 
			if Node1._element > Node2._element:
					now = merge_sublists(Node1._next, Node2)
					now.insert_last(Node1._element)
			else:
					now = merge_sublists(Node1, Node2._next)
					now.insert_last(Node._element)
			return now
	return merge_sublists(srt_lnk_lst1._header._next, srt_lnk_lst2._header._next)

lst1 = Doubly_Linked_List()
lst1.insert_last(1)
lst1.insert_last(3)
lst1.insert_last(5)
lst1.insert_last(6)
lst1.insert_last(8)
print(lst1)

lst2 = Doubly_Linked_List()
lst2.insert_last(2)
lst2.insert_last(3)
lst2.insert_last(5)
lst2.insert_last(10)
lst2.insert_last(15)
lst2.insert_last(18)
print(lst2)
Exemplo n.º 12
0
 def __init__(self):
     self.items = Doubly_Linked_List(
     )  #create a new dequeue with doubly linked list
Exemplo n.º 13
0
class Deque:
    def __init__(self):
        self.items = Doubly_Linked_List(
        )  #create a new dequeue with doubly linked list

    def isEmpty(self):
        return self.items.size() == 0

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

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

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

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

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

    def count(self, value):
        count = 0
        current = self.items.head
        while current != None:
            if current.data == value:
                count += 1
            current = current.next
        return count

    def extend(self, lst):
        if lst is self:
            lst = list(lst)
        for elem in lst:
            self.insertFront(elem)

    def extendleft(self, lst):
        if lst is self:
            lst = list(lst)
        for elem in lst:
            self.insertRear(elem)

    def rotate(self, n):
        length = self.size()
        if length <= 1:
            return
        halflen = length >> 1
        if n > halflen or n < -halflen:
            n %= length
            if n > halflen:
                n -= length
            elif n < -halflen:
                n += length
        while n > 0:
            self.insertRear(self.removeFront())
            n -= 1
        while n < 0:
            self.insertFront(self.removeRear())
            n += 1

    def reverse(self):
        self.items.reverse()

    def remove(self, value):
        self.items.remove(value)

    def removeAll(self, value):
        self.items.removeAll(value)

    def __str__(self):  #Returns a string representation of the object
        data = self.items.__str__().split('->')
        sep = "]"
        return sep + ",".join(data) + "]"
Exemplo n.º 14
0
class CompactString():
    def __init__(self, orig_str=''):
        self._data = Doubly_Linked_List()
        for i in range(len(orig_str)):
            if i == 0:
                self._data.insert_last((orig_str[i], 1))
            if i > 0 and orig_str[i] != orig_str[i - 1]:
                self._data.insert_last((orig_str[i], 1))
            else:
                mylast = self._data.last()
                tup1 = mylast[1]
                self._data._trailer._prev._element = (orig_str[i], tup1 + 1)

    def __add__(self, other):
        result = CompactString()
        if self._data._trailer._prev._element[
                0] == other._data._header._next._element[0]:
            newcount = other._data._header._next._element[
                1] + self._data._trailer._prev._element[1]
            other._data._header._next._element = other._data._header._next._element[
                0], newcount
            self._data.delete_last()
        curr = self._data._header._next
        while curr._element != None:
            result._data.insert_last(curr._element)
            curr = curr._next
        mycurr = other._data._header._next
        while mycurr._element != None:
            result._data.insert_last(mycurr._element)
            mycurr = mycurr._next
        return result

    def __lt__(self, other):
        curr = self._data._header._next
        mycurr = other._data._header._next
        while curr != self._data._trailer and mycurr != other._data._trailer:
            if curr._element[0] == mycurr._element[0]:
                if curr._element[1] == mycurr._element[1]:
                    curr = curr._next
                    mycurr = mycurr._next
                elif curr._element[1] < mycurr._element[1]:
                    if curr._next is not self._data._trailer:
                        return curr._next._element[0] < mycurr._element[0]
                    else:
                        return True
                else:
                    if mycurr._next is not other._data._trailer:
                        return curr._element[0] < mycurr._next._element[0]
                    else:
                        return False
            elif curr._element[0] < mycurr._element[0]:
                return True
            else:
                return False

        if mycurr != other._data._trailer and curr == self._data._trailer:
            return True
        return False

    def __eq__(self, other):
        if not self < other:
            if not self > other:
                return True
        return False

    # curr = self._data._header._next
    # mycurr = other._data._header._next
    # while curr != self._data._trailer:
    # 	if mycurr != self._data._trailer and curr._element == mycurr._element:
    # 		curr = curr._next
    # 		mycurr = mycurr._next
    # if curr == self._data._trailer and mycurr == self._data._trailer:
    # 	return True
    # return False

    def __le__(self, other):
        if self < other:
            return True
        else:
            if self == other:
                return True
        return False

    def __gt__(self, other):
        curr = self._data._header._next
        mycurr = other._data._header._next
        while curr != self._data._trailer and mycurr != other._data._trailer:
            if curr._element[0] == mycurr._element[0]:
                if curr._element[1] == mycurr._element[1]:
                    curr = curr._next
                    mycurr = mycurr._next
                elif mycurr._element[1] < curr._element[1]:
                    if mycurr._next is not other._data._trailer:
                        return curr._element[0] > mycurr._next._element[0]
                    else:
                        return True
                else:
                    if curr._next is not self._data._trailer:
                        return curr._next._element[0] > mycurr._element[0]
                    else:
                        return False
            elif curr._element[0] > mycurr._element[0]:
                return True
            else:
                return False
        if mycurr == other._data._trailer and curr != self._data._trailer:
            return True
        return False

    def __ge__(self, other):
        if self > other:
            return True
        else:
            if self == other:
                return True
        return False

    def __repr__(self):
        repstr = ''
        currNode = self._data._header._next
        while currNode is not self._data._trailer:
            for i in range(currNode._element[1]):
                repstr += currNode._element[0]
            currNode = currNode._next
        return repstr

    s1 = CompactString('aaaaabbbaaac')
    s2 = CompactString('aaaaaaacccaaaa')
    print(s1)
    print(s2)
    s3 = s2 + s1
    print(s3)
    print(s1 > s2)
Exemplo n.º 15
0
 def __init__(self, N=10):
     self._data = Doubly_Linked_List()
     self._size = 0