예제 #1
0
def set(k, t, v, map):

    eval = map.get(k)
    print(eval)
    if eval is None:
        head = Node(t)
        arr = []
        heapq.heappush(arr, v)
        head.set_heap_ref(arr)
        map[k] = head
    else:
        node = eval
        inserted = False
        prev = None
        while node:
            if node.value == t:
                arr = node.heapref
                heapq.heappush(arr, v)
                inserted = True
                break
            elif node.prev and node.prev.value < t and node.next and node.next.value > t:
                tmp = Node(t)
                arr = []
                heapq.heappush(arr, v)
                tmp.set_heap_ref(arr)
                prev = node.prev
                next = node.next
                prev.next = tmp
                tmp.prev = prev
                tmp.next = next
                next.prev = tmp
                inserted = True
                break
            else:
                prev = node
                node = node.next

        if not inserted:
            if t > prev.value:
                print('inserting at ', t)
                tmp = Node(t)
                arr = []
                heapq.heappush(arr, v)
                tmp.set_heap_ref(arr)
                prev.next = tmp
                tmp.prev = prev
            else:
                print('inserting1 at ', t, prev)
                tmp = Node(t)
                arr = []
                heapq.heappush(arr, v)
                tmp.set_heap_ref(arr)

                prev.prev.next = tmp
                tmp.prev = prev.prev
                tmp.next = prev
                prev.prev = tmp
                prev.next = None
예제 #2
0
def clone(head):

    clonehead = None
    clonehead1 = Node
    tmp = head
    while tmp:
        if clonehead is None:
            clonehead = Node(tmp.value)
            tmp.clonenode = clonehead
            clonehead1 = clonehead
        else:
            clonehead.next = Node(tmp.value)
            clonehead = clonehead.next
            tmp.clonenode = clonehead

        tmp = tmp.next

    # tmp2 = head
    # while tmp2:
    #     print(tmp2, tmp2.clonenode, tmp2.random)
    #     tmp2 = tmp2.next

    tmp = head
    while tmp:
        print(tmp, tmp.clonenode, tmp.random)
        clonednode = tmp.clonenode
        rnode = tmp.random
        if rnode:
            clonednode.random = rnode.clonenode
        #tmp.clonenode = None
        tmp = tmp.next

    tmp2 = clonehead1
    while tmp2:
        print(tmp2 , tmp2.random)
        tmp2 = tmp2.next
예제 #3
0
from src.main.prep.node import Node

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

stack = []

while head:
    stack.append(head)
    head = head.next

p = stack.pop()
head = p
while len(stack) > 0:
    e = stack.pop()
    p.next = e
    p = e

p.next = None

t = head
while t:
    print(t)
    t = t.next

c = head
p = None
n = None
예제 #4
0
from src.main.prep.node import Node

head1 = Node(1)
head1.next = Node(2)
head1.next.next = Node(3)
head1.next.next.next = Node(4)
head1.next.next.next.next = Node(5)

head2 = Node(8)
head2.next = Node(9)
head2.next.next = Node(8)
head2.next.next.next = Node(6)
head2.next.next.next.next = Node(7)

head3 = None
remainder = 0
tmp = None
prev = 0
while head1 and head2:
    s = head1.value + head2.value + remainder
    prev = s
    print(remainder, s)
    if s >=10:
        remainder = s//10
        s = s%10
    else:
        remainder = 0

    if head3 is None:
        head3 = Node(s)
from src.main.prep.node import Node

def print_ll(h):
    lst = []
    while h:
        lst.append(h)
        h = h.next
    print(lst)

head1 = Node(5)
head1.next = Node(8)
head1.next.next = Node(10)

head2 = Node(4)
head2.next = Node(9)
head2.next.next = Node(15)

print_ll(head1)
print_ll(head2)

def using_dummy_node(head1, head2):

    dummy = Node(None)
    tail = dummy

    while head1 and head2:
        print(head1, head2, tail, dummy)

        if head1.value <= head2.value:
            tail.next = head1
from src.main.prep.node import Node

head = Node(4)
head.next = Node(1)
head.next.next = Node(-3)
head.next.next.next = Node(9)
head.next.next.next.next = Node(2)


head2 = Node(10)
head2.next = Node(80)
head2.next.next = Node(15)
head2.next.next.next = Node(20)
head2.next.next.next.next = Node(40)
head2.next.next.next.next.next = Node(70)
head2.next.next.next.next.next.next = Node(30)

#  4 -> 1 -> -3 -> 9 -> 2

def print_ll(head):
    lst = []
    while head:
        lst.append(str(head.value))
        lst.append(' -> ')
        head = head.next
    return "".join(lst)

def get_tail(start, partition):

    while start and start.next != partition:
        start = start.next
from src.main.prep.node import Node

def print_ll(h):
    lst = []
    while h:
        lst.append(h)
        h = h.next
    print(lst)

head1 = Node(5)
head1.next = Node(8)
head1.next.next = Node(10)
head1.next.next.next = Node(12)
head1.next.next.next.next = Node(20)
head1.next.next.next.next.next = Node(30)

k = 3
print_ll(head1)
i=0
ptr = None
tmp = head1

while tmp:
    tmp = tmp.next
    i+=1

    if i == k+1:
        ptr = head1
        continue

    if ptr: