def set(self,index,element):
		newNode = LinkedListNode(element)
		if(index>0):
			prevNode = self.get(index-1)
		newNode.next = prevNode.next.next
		prevNode.next = newNode
		prevNode.next.next = None
	def insert(self,index,element):
		newNode = LinkedListNode(element)
		if(index==0):
			self.push_front(newNode)
		else:
			prevNode = self.get(index-1)
			newNode.next = prevNode.next
			prevNode.next = newNode

		return newNode
Exemplo n.º 3
0
	def insert(self, data):
		""" Insert the data to front list. Returns True if the data was added, False otherwise. """
		curr = Node(data)
		if self.size == 0:
			self.head = curr
			self.size += 1
			return True
		curr.next = self.head
		self.head = curr
		self.size += 1
		return True
Exemplo n.º 4
0
def reverseandclone(headnode):
    temp = headnode
    if temp is None:
        return None

    head = None
    while temp is not None:
        newnode = LinkedListNode(temp.get_data())
        newnode.set_nextnode(head)
        head = newnode
        temp = temp.get_nextnode()
    return head
def test_deleteatpos():
    newlinkedlist = LinkedList()
    newnode = LinkedListNode(2)
    newlinkedlist.addNode(newnode)
    newnode1 = LinkedListNode(3)
    newlinkedlist.addNode(newnode1)
    newnode2 = LinkedListNode(4)
    newlinkedlist.addNode(newnode2)
    newnode3 = LinkedListNode(5)
    newlinkedlist.addNode(newnode3)
    newlinkedlist.deletenodeAtpos(3)
    assert newlinkedlist.get_length() == 3
Exemplo n.º 6
0
def test_intersectingNodes():
    node1 = LinkedListNode(1)
    node2 = LinkedListNode(2)
    node3 = LinkedListNode(3)
    node4 = LinkedListNode(4)
    node5 = LinkedListNode(5)
    node6 = LinkedListNode(6)
    node7 = LinkedListNode(7)
    node8 = LinkedListNode(8)
    LL1 = LinkedList()
    LL1.addNode(node1)
    LL1.addNode(node3)
    LL1.addNode(node4)
    LL1.addNode(node5)
    LL1.addNode(node6)

    LL2 = LinkedList()
    LL2.addNode(node2)
    LL2.addNode(node3)
    LL2.addNode(node4)
    LL2.addNode(node5)
    LL2.addNode(node6)

    intersectNode = getIntersectionNode(LL1.head, LL2.head)
    print(intersectNode.get_data())
 def enqueue(self, value):
     node = LinkedListNode(value)
     if (self.head == None):
         self.head = self.tail = node
     else:
         self.tail.next = node
         self.tail = node
	def push_back(self,element):
		newNode = LinkedListNode(element)

		if(self.root==None):
			self.root = newNode
			return newNode

		self.back().next = newNode
		return newNode
def main():
    test = LinkedListNode(0)
    test.add(0)
    test.add(1)
    test.add(2)
    test.printNodes()

    remove_duplicates(test)
    remove_duplicates_alt(test)
Exemplo n.º 10
0
def createLinkedlist():
    node1 = LinkedListNode(1)
    node2 = LinkedListNode(2)
    node3 = LinkedListNode(3)
    node4 = LinkedListNode(4)
    node5 = LinkedListNode(5)
    node6 = LinkedListNode(6)
    node7 = LinkedListNode(7)
    node8 = LinkedListNode(8)
    ll = LinkedList()
    ll.addNode(node1)
    ll.addNode(node2)
    ll.addNode(node3)
    ll.addNode(node4)
    ll.addNode(node5)
    ll.addNode(node6)
    ll.addNode(node7)
    ll.addNode(node8)
    return ll
def mergeTwoSortedLists(list1, list2):
    temp = LinkedListNode(0)
    pointer = temp
    count = 0
    while list1 is not None and list2 is not None:
        print("count:", count)
        count += 1
        if list1.get_data() < list2.get_data():
            pointer.set_nextnode(list1)
            list1 = list1.next
        else:
            pointer.set_nextnode(list2)
            list2 = list2.next
        pointer = pointer.get_nextnode()
    if list1 == None:
        pointer.set_nextnode(list2)
    else:
        pointer.set_nextnode(list1)
    return temp.nextnode
def single_linked_list_test():
    s = SingleLinkedList()
    s.add_node_at_first(LinkedListNode(4))
    s.add_node_at_first(LinkedListNode(5))
    s.add_node_at_first(LinkedListNode(7))

    s.add_node_at_last(LinkedListNode(6))
    s.add_node_at_last(LinkedListNode(9))
    print("before delete")
    s.traverse()
    s.delete_node(LinkedListNode(4))
    print("after delete")
    print("first.... %s" % s.get_first().get_data())
    print("last....%s " % s.get_last().get_data())
    print("size.....%d" % s.get_size())

    s.traverse()
Exemplo n.º 13
0
def getBuzzfeed(word):
	firstNode = LinkedListNode("")
	firstNode.setNext(None)
	LLlist= LinkedList(firstNode)
	jsonStr = '{ \"results\": ['
	url = "http://www.buzzfeed.com/tag/"+word
	html = urllib.urlopen(url).read()
	htmlObject = BeautifulSoup(html, features="lxml")
	headlines = []
	count =0
	##print ("Second tag")
	##print (htmlObject)
	for item in htmlObject.findAll(re.compile("h2", re.S)):
		if count <3:
		#	#print(item)
			pattern = re.compile("<a href=\"(.*)\" rel:gt_act=\"post/titl.*>(.*)</a>", re.S)
			match = pattern.match(repr(item.a))
			if match != None:
				url = "http://www.buzzfeed.com"+match.groups()[0]
				match= match.groups()[1].replace("\\n\\t\\t\\n\\t\\t\\t","")
				match = match.replace("\\xa0", " ")
				match = match.replace('u201c', ' ')
				match = match.replace('u201d', ' ')
				match = match.replace('u2026', ' ')
				match = match.replace("\\n\\t\\t", "")
				match = re.sub(r'\W+', ' ', match)
				match = match.replace("u2019", "'")
				node = LinkedListNode(url)
				node.setTitle(match)
				LLlist.insertFirst(node)
				count= count+1
	currentJSON = LLlist.deleteFirst()
	while(currentJSON.getNext().getNext() != None):
		jsonStr+= " {\"title\": \" "+currentJSON.getTitle()+ "\", \"url\": \""+currentJSON.getURL() +"\" }, "
		currentJSON = LLlist.deleteFirst()

	jsonStr+= " {\"title\": \" "+currentJSON.getTitle()+ "\", \"url\": \""+currentJSON.getURL() +"\" } "
	jsonStr+= ' ]}'
	sentiment = getSentiment(getBuzzfeedPost(jsonStr), word) 
	return sentiment
	def push_front(self,element):
		newRoot = LinkedListNode(element)
		oldRoot = self.root
		self.root = newRoot
		newRoot.next = oldRoot
def test_checkheadvalue():
    newlinkedlist = LinkedList()
    newnode = LinkedListNode(2)
    newlinkedlist.addNode(newnode)
    assert newlinkedlist.getfirst() == 2
def test_addhead():
    newlinkedlist = LinkedList()
    newnode = LinkedListNode(2)
    newlinkedlist.addNode(newnode)
    assert newlinkedlist.get_length() == 1
 def __init__(self,d):
     LinkedListNode.__init__(self, d)
     self.__prev = None
        count += 1
        if list1.get_data() < list2.get_data():
            pointer.set_nextnode(list1)
            list1 = list1.next
        else:
            pointer.set_nextnode(list2)
            list2 = list2.next
        pointer = pointer.get_nextnode()
    if list1 == None:
        pointer.set_nextnode(list2)
    else:
        pointer.set_nextnode(list1)
    return temp.nextnode


node1 = LinkedListNode(1)
node2 = LinkedListNode(2)
node3 = LinkedListNode(3)
node4 = LinkedListNode(4)
node5 = LinkedListNode(5)
node6 = LinkedListNode(6)
node7 = LinkedListNode(7)
node8 = LinkedListNode(8)

LL1 = LinkedList()
LL2 = LinkedList()

LL1.addNode(node1)
LL2.addNode(node2)
LL1.addNode(node3)
LL2.addNode(node4)
Exemplo n.º 19
0
def createLinkedListNode(request):
    data = request.param
    return LinkedListNode(data)
Exemplo n.º 20
0
def createnodeObj(data):
    return LinkedListNode(data)
Exemplo n.º 21
0
        else:
            # insert at end
            end.next = curr
            end = curr
        curr = next_node
    end.next = None
    return start

def print_node(head):
    curr = head
    while curr is not None:
        print curr.data,
        curr = curr.next

if __name__ == '__main__':
    node1 = LinkedListNode(1)
    node2 = LinkedListNode(2, node1)
    node10 = LinkedListNode(10, node2)
    node5 = LinkedListNode(5, node10)
    node8 = LinkedListNode(8, node5)
    node5_2 = LinkedListNode(5, node8)
    node3 = LinkedListNode(3, node5_2)
    head = node3
    print "before",
    print_node(head)
    newHead = partition(head,5)
    
    print "\nafter",
    print_node(newHead)

Exemplo n.º 22
0
 def __init__(self, d):
     LinkedListNode.__init__(self, d)
     self.__prev = None