def mklist(initializer): head = temp = Node(initializer[0], None) for i in xrange(1, len(nums)): n = Node(initializer[i], None) temp.setNext(n) temp = temp.getNext() return head
def mklist(initializer): head = temp = Node(initializer[0], None) for i in xrange(1, len(nums)): n = Node(initializer[i], None) temp.next = n temp = temp.next return head
def insertLast(self,data): newNode=Node() newNode.setData(data) if self.length==0: self.head=newNode else: current=self.head while current.getNext()!=None: current=current.getNext() current.setNext(newNode) self.length+=1
def removeDup(head): new_head = Node(head.data, None, None) tail = new_head head = head.next while head is not None: if head.data != tail.data: node = Node(head.data, tail, None) tail.next = node tail = tail.next head = head.next return new_head
def rearrange(head): slowptr = head fastptr = head prev = None while fastptr is not None and fastptr.next is not None: prev = slowptr slowptr = slowptr.next fastptr = fastptr.next.next head1 = head head2 = slowptr prev.next = None head2 = reverseList(head2) pl(head1) pl(head2) head = Node(-1, None) curr = head while head1 is not None or head2 is not None: if head1 is not None: curr.next = head1 curr = curr.next head1 = head1.next if head2 is not None: curr.next = head2 curr = curr.next head2 = head2.next return head.next
def merge(node_a, node_b): if node_a is None: return node_b if node_b is None: return node_a if node_a.data < node_b.data: node = Node(node_a.data, None, None) node.next = merge(node_a.next, node_b) else: node = Node(node_b.data, None, None) node.next = merge(node_a, node_b.next) node.next.prev = node node.prev = None return node
def evenodd(head): head1 = Node(-1, None) head2 = Node(-1, None) h1 = head1 h2 = head2 while head is not None: h1.next = head h1 = h1.next head = head.next if head is not None: h2.next = head h2 = h2.next head = head.next h2.next = None h1.next = head2.next return head1.next
def insertionSort(head): if head is not None: root = Node(head.data, None) curr = root head = head.next while head is not None: prev = None curr = root if head.data < root.data: node = Node(head.data, root) root = node head = head.next continue while curr.next is not None and curr.next.data < head.data: prev = curr curr = curr.next node = Node(head.data, curr.next) curr.next = node head = head.next return root
def insert(head, data): if head is None: head = Node(data, None) head.next = head return head elif data <= head.data: curr = head while curr.next is not head: curr = curr.next node = Node(data, head) curr.next = node head = node return head else: curr = head while curr.next is not head and data > curr.next.data: curr = curr.next node = Node(data, curr.next) curr.next = node return head
def huffman(s): # s为列表列表里的元素是由频率以及对应的字符[(频率,字符)] s.sort() n = len(s) pq = [] for i in range(n): a = Node(s[i][0]) a.symbol = s[i][1] pq.append(a) def insert(ss, rr): # 为rr找出插入位置使得ss是按里面元素的频率从小到大排序 long = len(ss) def _insert(low, high): if low > high: ss.insert(low, rr) else: mid = int((low + high) / 2) if rr.frequency <= ss[mid].frequency: _insert(low, mid - 1) else: _insert(mid + 1, high) _insert(0, long - 1) k = 1 while k <= n - 1: r = Node(pq[0].frequency + pq[1].frequency) r.left = pq[0] r.right = pq[1] del pq[0] del pq[0] insert(pq, r) k += 1 r = pq[0] # return r # 霍夫曼树根节点 result_code = [] def _get_huffman_code(node, code_value=[]): # 得到各个字符的编码 if node.symbol is not None: # print(''.join(code_value), node.symbol) # 编码结果 result_code.append((''.join(code_value), node.symbol)) else: _get_huffman_code(node.left, code_value + ['0']) _get_huffman_code(node.right, code_value + ['1']) _get_huffman_code(r, []) return result_code
def addOne(head): num1 = getNum(head) head = reverseList(head) curr = head carry = 1 prev = None while curr is not None: sum = carry + curr.data carry = 1 if sum >= 10 else 0 sum = sum % 10 curr.data = sum prev = curr curr = curr.next if carry >= 1: prev.next = Node(carry, None) head = reverseList(head) num2 = getNum(head) return num1, num2
node = gethalf(head) second_half = node.next #print(head.data,second_half.data ) #time.sleep(1) node.next = None first_half = head node_a = mergesort(first_half) node_b = mergesort(second_half) return merge(node_a, node_b) if __name__ == "__main__": res = [] for x in xrange(1000): size = random.randint(1, 100) head = Node(random.randint(1, 100), None) for i in xrange(1, size): node = Node(random.randint(1, 100), head) head = node #printlist(head) head = mergesort(head) res.append(issorted(head)) print("{} pass".format(res.count(True))) print("{} fail".format(res.count(False)))
curr.next = node if node.next is not None: node.next.prev = node return head if __name__ == "__main__": res = [] for i in xrange(10000): prev = None head = None size = randint(1, 1000) for i in xrange(size): head = Node(randint(-100, 100), prev, head) if prev is not None: prev.prev = head prev = head if head is not None: head.prev = None #printlist(head) head = insertionSort(head) #printlist(head) #print("isLinkListSorted" , isLinkListSorted(head)) res.append(isLinkListSorted(head)) print("%s Pass." % (res.count(True))) print("%s Fail." % (res.count(False)))
head = reverseList(head) num2 = getNum(head) return num1, num2 def test(num1, num2): return True if num2 == num1 + 1 else False if __name__ == "__main__": head = None res = [] for i in xrange(3000): head = None size = random.randint(3, 18) for i in xrange(size, 0, -1): if i == size: head = Node(random.randint(1, 9), head) else: head = Node(random.randint(0, 9), head) num1, num2 = addOne(head) truth = test(num1, num2) if not truth: print(num1, num2) res.append(truth) print("\n%s Pass." % (res.count(True))) print("%s Fail." % (res.count(False)))
def cutHalf(head): slowptr = head fastptr = head while fastptr.next != head and fastptr.next.next != head: slowptr = slowptr.next fastptr = fastptr.next.next if fastptr.next.next == head: fastptr = fastptr.next firsthead = slowptr.next slowptr.next = None fastptr.next = None return head, firsthead if __name__ == "__main__": head = tail = Node(10, None) for i in xrange(9, -1, -1): head = Node(i, head) tail.next = head printlist(head) head1, head2 = cutHalf(head) printlist(head1) printlist(head2)
while curr is not None and c < k: next = curr.next curr.next = prev curr.prev = next prev = curr curr = next c += 1 if next is not None: head.next = reverseKNode(next, k) head.next.prev = head else: head.next = None return prev if __name__ == "__main__": prev = None head = None for i in xrange(10): head = Node(randint(2, 10), prev, head) if prev is not None: prev.prev = head prev = head head.prev = None printlist(head) head = reverseKNode(head, 2) printlist(head)
curr.next = temp.next del temp else: curr = curr.next maxval = max(maxval, curr.data) head = reverse(head) return head def reverse(head): curr = head prev = None while curr is not None: next = curr.next curr.next = prev prev = curr curr = next return prev if __name__ == "__main__": head = None for i in xrange(0, 21): node = Node(randint(1,100), head) head = node printlist(head) head = deleteMax(head) printlist(head)
from __future__ import print_function from linklist import SimpleNode as Node from linklist import printlist def printReverse(head): if head is None: return printReverse(head.next) print(head.data, end="->") if __name__ == "__main__": head = Node(0, None) for i in xrange(1, 10): node = Node(i, head) head = node printlist(head) print("Reverse list : ") printReverse(head)
def push(self, data): new_node = Node(data) new_node.next = self.top.next self.top.next = new_node return self
kthtwo = head i = 0 while i < k and kthtwo is not None: kthtwo = kthtwo.next i += 1 runner = kthtwo kthtwo = head while runner is not None: runner = runner.next kthtwo = kthtwo.next kthone.data, kthtwo.data = kthtwo.data, kthone.data return head if __name__ == "__main__": head = None nums = [1, 2, 3, 4, 5, 6, 7, 8] nums.reverse() for i in xrange(len(nums)): head = Node(nums[i], head) printlist(head) head = swapKth(head, 1) printlist(head) head = swapKth(head, 2) printlist(head)
def evenodd(head): head1 = Node(-1, None) head2 = Node(-1, None) h1 = head1 h2 = head2 while head is not None: h1.next = head h1 = h1.next head = head.next if head is not None: h2.next = head h2 = h2.next head = head.next h2.next = None h1.next = head2.next return head1.next if __name__ == "__main__": head = None for i in xrange(11, 0, -1): node = Node(i, head) head = node printlist(head) head = evenodd(head) printlist(head)
def reverse(head): prev = None curr = head while curr is not None: next = curr.next curr.next = prev curr.prev = next prev = curr curr = next return prev if __name__ == "__main__": prev = None head = None for i in xrange(10, 0, -1): head = Node(i, prev, head) if prev is not None: prev.prev = head prev = head head.prev = None tail = printlist(head) head = reverse(head) printlist(head)
def deleteAllKeys(head, key): if head is not None: node = head while node is not None: if node.data == key : delete(node) node = node.next return head if __name__ == "__main__": prev = None head = None for i in xrange(12): head = Node(randint(0,8), prev , head) if prev is not None: prev.prev = head prev = head head.prev = None printlist(head) head = deleteAllKeys(head, 4) printlist(head)
node_a = mergesort(first_half) node_b = mergesort(second_half) return merge(node_a, node_b) if __name__ == "__main__": res = [] head = None for i in xrange(10000): size = randint(1, 100) head = None prev = None for i in xrange(size): head = Node(randint(1, size*2), prev, head) if prev is not None: prev.prev = head prev = head head.prev = None head = mergesort(head) res.append(isLinkListSorted(head)) print("%s Pass."%(res.count(True))) print("%s Fail."%(res.count(False)))
prev = None res = [] alp = { "level": True, "leel": True, "Hello": False, "lallal": True, "Anna": False, "Jazz": False } for key, val in alp.iteritems(): #print(key , val) head = prev = None for i in xrange(len(key)): head = Node(key[i], prev, head) if prev is not None: prev.prev = head prev = head head.prev = None #printlist(head) if val is isPallindrome(head): res.append(True) else: res.append(False) #res.append(val is isPallindrome(head)) print("%s Pass." % (res.count(True))) print("%s Fail." % (res.count(False)))
return False def isIdenticalItr(one, two): while one is not None and two is not None: if one.data != two.data: return False one = one.next two = two.next return one is None and two is None if __name__ == "__main__": head_one = Node(0, None) for i in xrange(1,10): node = Node(i, head_one) head_one = node head_two = Node(0, None) for i in xrange(1,10): node = Node(i, head_two) head_two = node head_three = Node(0, None) for i in xrange(1,5): node = Node(i, head_three) head_three = node for i in xrange(1,5): node = Node(randint(1,100), head_three)
#!/usr/bin/python __author__ = "Vishal Jasrotia. Stony Brook University" __copyright__ = "" __license__ = "GPL" __version__ = "1.0" __maintainer__ = "Vishal Jasrotia" __email__ = "*****@*****.**" __status__ = "" import os, sys from linklist import Node a = Node(1 ,None) b = Node(2 ,a) c = Node(3 ,b) d = Node(4 ,c) e = Node(5 ,d) f = Node(6 ,e) g = Node(7 ,f) h = Node(8 ,g) i = Node(9 ,h) j = Node(10 ,i) k = Node(11 ,j) l = Node(12 ,k) m = Node(13 ,l) n = Node(14 ,m) #create circle a.setNext(h)
first_half.next = None second_half.prev = None first_half = head printlist(first_half) printlist(second_half) second_half = reverse(second_half) printlist(second_half) return merge(first_half, second_half) if __name__ == "__main__": prev = None head = None nums = [1, 3, 5, 7, 8, 9, 10, 9, 8, 6, 4, 2, 1, 0] for i in xrange(len(nums)): head = Node(nums[i], prev, head) if prev is not None: prev.prev = head prev = head head.prev = None printlist(head) head = sort(head) printlist(head)