Ejemplo n.º 1
0
def createDepthLinkedList():
	nodesAtDepth = LinkedList()
	nodesAtDepth.insertNode(tree.root.data)
	q=[]
	q.append(tree.root)
	
	depthLinkedList =[nodesAtDepth]
	condition = True
	while condition:
		children = []
		while (q):
			node= q.pop(0)
			if(node.left != None):
				children.append(node.left)
			if(node.right != None):
				children.append(node.right)
		condition = True if children else False
		if(condition):
			nodesAtDepth = LinkedList()
			for node in children:
				nodesAtDepth.insertNode(node.data)
			depthLinkedList.append(nodesAtDepth)
			q.extend(children)
	for linkedlist in depthLinkedList:
		 linkedlist.display()
Ejemplo n.º 2
0
def scramble(self):
	fast=self.first
	slow=self.first

	while(fast!=None):
		fast=fast.next.next
		slow=slow.next

	fast=self.first

	scrambledList=LinkedList()
	while(slow!=None):
		scrambledList.insertNode(fast.data)
		scrambledList.insertNode(slow.data)
		fast = fast.next
		slow = slow.next
	return scrambledList
Ejemplo n.º 3
0
from linkedlist import LinkedList

myList= LinkedList()
myList.insertNode(1)
myList.insertNode(2)
myList.insertNode(3)
myList.insertNode(4)
myList.insertNode(5)
myList.insertNode(11)
myList.insertNode(12)
myList.insertNode(13)
myList.insertNode(14)
myList.insertNode(15)

def scramble(self):
	fast=self.first
	slow=self.first

	while(fast!=None):
		fast=fast.next.next
		slow=slow.next

	fast=self.first

	scrambledList=LinkedList()
	while(slow!=None):
		scrambledList.insertNode(fast.data)
		scrambledList.insertNode(slow.data)
		fast = fast.next
		slow = slow.next
	return scrambledList
Ejemplo n.º 4
0
from linkedlist import LinkedList

myList = LinkedList()
for i in range(1,11):
	myList.insertNode(i)

def kthlast(self,k):
	fast = self.first
	slow = self.first
	for i in range(k-1):
		if(fast.next!=None):
			fast=fast.next
		else :
			print "Not enough elements"
			return
	while(fast.next!=None):
		fast=fast.next
		slow= slow.next
	print "kthlast : ",slow.data

LinkedList.kthlast = kthlast

myList.kthlast(3)


Ejemplo n.º 5
0
from linkedlist import LinkedList

palin = raw_input("Enter palin Strng : ")
myList = LinkedList()
for ch in palin:
	myList.insertNode(ch)


def checkPalindrom(self):
	myStack =[]
	slow = myList.first
	fast = myList.first
	myStack.append(slow.data)

	while(fast!=None and fast.next!=None):
		slow=slow.next
		fast=fast.next.next
		if(fast !=None):
			myStack.append(slow.data)
	
	while (slow != None):
		if(slow.data != myStack.pop()):
			print "Not palin"
			return
		slow=slow.next
	print "Palindrome"

LinkedList.checkPalindrom = checkPalindrom
myList.checkPalindrom()

Ejemplo n.º 6
0
from linkedlist import LinkedList, Node
num1 = map(int,raw_input("enter number 1 : "))
num2 = map(int,raw_input("enter number 2 : "))

firstNum = LinkedList()
for x in num1:
	firstNum.insertNode(x)

secondNum = LinkedList()
for x in num2:
	secondNum.insertNode(x)


temp = firstNum.first
firstStack=[]

while(temp!=None):
	firstStack.append(temp.data)
	temp = temp.next

temp = secondNum.first
secondStack=[]

while(temp!=None):
	secondStack.append(temp.data)
	temp = temp.next

def insertFirst(theList,data):
	node = Node(data)
	node.next = theList.first
	theList.first = node
Ejemplo n.º 7
0
from linkedlist import LinkedList
from random import randint

myList = LinkedList()

for i in range(10):
	myList.insertNode(randint(0,9))

def partition(self):
	pivot = self.first.data
	start = self.first
	temp = self.first
	while(temp.next!=None):
		if(temp.next.data < pivot):
			nextElement = temp.next.next
			temp.next.next = start
			start = temp.next
			temp.next=nextElement
		else:
			temp = temp.next
	self.first = start

LinkedList.partition = partition
myList.display()
myList.partition()
myList.display()