コード例 #1
0
def bottomUp(percent, LinkedList):
    # print("BottomUp Activated!!!")
    percent = percent
    # print(LinkedList.size())
    length = int(howmuchichoose(LinkedList.size(), percent))
    # print(length)
    for i in range(0,length):
        LinkedList.headtotail()
    # print("Now : ",end='')
    print(LinkedList)
コード例 #2
0
 def test_delete(self):
     ll = LinkedList(['A', 'B', 'C'])
     ll.delete('A')
     assert ll.head.data == 'B'
     assert ll.tail.data == 'C'
     ll.delete('C')
     assert ll.head.data == 'B'
     assert ll.tail.data == 'B'
     ll.delete('B')
     assert ll.head is None
     assert ll.tail is None
     with self.assertRaises(ValueError):
         ll.delete('D')
コード例 #3
0
def findMidEle_1(list: LinkedList) -> int:
    length = list.length()
    mid_idx = length // 2
    temp = list.head
    idx = -1
    while (temp):
        idx = idx + 1
        if (idx == mid_idx):
            return temp.val
        temp = temp.next
コード例 #4
0
def checkPalindro_2(list: LinkedList):
    if (not list):
        return False
    if (list.length() <= 1):
        return True
    temp = list.head
    re = ''
    while (temp):
        re = re + temp.val
        temp = temp.next
    return re == re[::-1]
コード例 #5
0
def moveLast(list: LinkedList) -> LinkedList:
    if (not list or not list.head):
        return None
    temp = list.head
    while (temp.next):
        if (not temp.next.next):
            last = temp.next
            last.next = list.head
            temp.next = None
            list.head = last
            break
        temp = temp.next
    return list
コード例 #6
0
    def put(self, key, value):
        # resize if needed
        if self.get_load_factor() >= 0.7:
            self.resize(self.capacity * 2)

        # bucket being added to
        current = self.table[self.hash_index(key)]

        # for adding new list to empty bucket
        if current is None:
            new_list = LinkedList()
            self.table[self.hash_index(key)] = new_list
            new_list.add_new(HashTableEntry(key, value))
            self.count += 1
        # if there is already a list
        else:
            # if the new key/item doesn't exist, add it as a new head
            if current.find(key) == None:
                current.add_new(HashTableEntry(key, value))
                self.count += 1
            else:
                # change the current node to the new node
                current.change_value(key, HashTableEntry(key, value))
コード例 #7
0
def checkPalindro(list: LinkedList) -> bool:
    if (not list):
        return False
    if (list.length() <= 1):
        return True
    temp = list.head
    stack = []
    while (temp):
        stack.append(temp.val)
        temp = temp.next
    temp = list.head
    while (temp):
        ele = stack.pop()
        if (temp.val != ele):
            return False
        temp = temp.next
    return True
コード例 #8
0
 def test_init_with_list(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.head.data == 'A'
     assert ll.tail.data == 'C'
     assert ll.as_list() == ['A', 'B', 'C']
     assert ll.is_empty() is False
コード例 #9
0
 def test_init(self):
     ll = LinkedList()
     assert ll.head is None
     assert ll.tail is None
     assert ll.as_list() == []
     assert ll.is_empty() is True
from Node import Node
from Linkedlist import LinkedList


def length(lst):
    count = 0
    if lst.is_empty():
        return count

    current_node = lst.get_head()
    while current_node:
        count += 1
        current_node = current_node.next_element
    return count


lst = LinkedList()
lst.insert_at_head(4)
lst.insert_at_head(3)
lst.insert_at_head(2)
lst.insert_at_head(1)
lst.insert_at_head(0)
print(length(lst))
コード例 #11
0
        return None
    temp = list.head
    vals = set()
    vals.add(temp.val)
    while (temp.next):
        if (temp.next.val in vals):
            new = temp.next.next
            temp.next = new
        else:
            vals.add(temp.next.val)
            temp = temp.next
    return list


if __name__ == '__main__':
    llist = LinkedList()

    # test remove duplicates in a sorted list

    llist.push(20)
    llist.push(13)
    llist.push(13)
    llist.push(11)
    llist.push(11)
    llist.push(11)
    print("Created Linked List: ")
    llist.printList()
    print()
    print("Linked List after removing", "duplicate elements:")
    llist = removeDup(llist)
    llist.printList()
コード例 #12
0
from Linkedlist import Node
from Linkedlist import LinkedList

#  main function 
if __name__ == '__main__':
    #  sample data 
    llist = LinkedList()
    llist.head = Node(1)
    second = Node(2)
    third = Node(3)

    llist.head.next = second;
    second.next = third;
    # intial Linked List
    llist.printList()

    #  Inserting node at the front 
    llist.push(0)

    # updating the Linked List
    print("new updated list after adding element at the stating of the list")
    llist.printList()

    # Inserting element after specific element
    llist.insertAfterData(2, 4)
    print("updated linked list after insertion after the given node")
    llist.printList()

    llist.insertAfterNode(second, 5)
    print("updated linked list after insertion after the given node")
    llist.printList()
コード例 #13
0
    if (not list or not list.head):
        return None
    temp = list.head
    while (temp.next):
        if (not temp.next.next):
            last = temp.next
            last.next = list.head
            temp.next = None
            list.head = last
            break
        temp = temp.next
    return list


if __name__ == '__main__':
    llist = LinkedList()

    # test remove duplicates in a sorted list

    llist.push(20)
    llist.push(13)
    llist.push(13)
    llist.push(11)
    llist.push(11)
    llist.push(11)
    print("Created Linked List: ")
    llist.printList()
    print()
    print("Linked List after moving", "last element to front:")
    llist = moveLast(llist)
    llist.printList()
コード例 #14
0
   percent = percent
   length = int(howmuchichoose(LL.size(), percent))
#    print(length)
   LL.driff(length)
#    print("Now : ",end='')
   print(LL)

def deBottomUp(percent, LL):
    # print("De-BottomUp Activated!!!")
    percent = percent
    length = int(howmuchichoose(LL.size(), percent))
    # print(length)
    for i in range(0,length):
        LL.tailtohead()
    # print("Now : ",end='')
    print(LL)

LL = LinkedList()
for i in range(1,11):
    LL.append(str(i))

# print("start ",end='')
# print(LL)

bottomUp(130,LL)
riffle(60,LL)
deRiffle(60,LL)
deBottomUp(130,LL)


コード例 #15
0
 def test_length(self):
     ll = LinkedList()
     assert ll.length() == 0
     ll.append('A')
     assert ll.length() == 1
     ll.append('B')
     assert ll.length() == 2
     ll.append('C')
     assert ll.length() == 3
コード例 #16
0
from Linkedlist import Node, LinkedList

ll = LinkedList()
nodeOne = Node("Manoj")
ll.insertAtStart(nodeOne)
nodeTwo = Node("Amit")
ll.insertAtEnd(nodeTwo)
nodeThree = Node("Anish")
ll.insertAtEnd(nodeThree)
nodeFour = Node("Avinash")
ll.insertAtEnd(nodeFour)

ll.printLL()
print()

ll.reverseLL()
ll.printLL()
print()

nodeFive = Node("Arvind")
ll.insertAtEnd(nodeFive)

ll.printLL()
print()

ll.reverseLL()
ll.printLL()
コード例 #17
0
 def test_prepend(self):
     ll = LinkedList()
     ll.prepend('C')
     assert ll.head.data == 'C'
     assert ll.tail.data == 'C'
     assert ll.as_list() == ['C']
     ll.prepend('B')
     assert ll.head.data == 'B'
     assert ll.tail.data == 'C'
     assert ll.as_list() == ['B', 'C']
     ll.prepend('A')
     assert ll.head.data == 'A'
     assert ll.tail.data == 'C'
     assert ll.as_list() == ['A', 'B', 'C']
コード例 #18
0
    if (not list):
        return False
    if (list.length() <= 1):
        return True
    temp = list.head
    re = ''
    while (temp):
        re = re + temp.val
        temp = temp.next
    return re == re[::-1]


if __name__ == '__main__':

    # Start with the empty list
    llist = LinkedList()

    # Insert a.  So linked list becomes 6->None
    llist.append('a')

    # Insert b at the beginning. So linked list becomes b->a->None
    llist.push('b')

    # Insert b at the beginning. So linked list becomes b->a->b->None
    llist.append('b')

    # print list
    llist.printList()

    print(checkPalindro(llist))
    print('-------------')
コード例 #19
0
 def test_find(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.find(lambda item: item == 'B') == 'B'
     assert ll.find(lambda item: item < 'B') == 'A'
     assert ll.find(lambda item: item > 'B') == 'C'
     assert ll.find(lambda item: item == 'D') is None
コード例 #20
0
    if (list.head is None):
        return
    temp = list.head
    slow, fast = temp, temp
    while (slow and fast and fast.next):
        fast_next = fast.next
        fast = fast_next.next
        slow = slow.next

    return slow.val


if __name__ == '__main__':

    # Start with the empty list
    llist = LinkedList()

    # Insert 6.  So linked list becomes 6->None
    llist.append(6)

    # Insert 7 at the beginning. So linked list becomes 7->6->None
    llist.push(7)

    # Insert 1 at the beginning. So linked list becomes 1->7->6->None
    llist.push(1)

    # Insert 4 at the end. So linked list becomes 1->7->6->4->None
    llist.append(4)

    # Insert 10 at the end. So linked list becomes 1->7->6->4->10->None
    llist.append(10)