python - How to search and return position of a node in a linked list? -


new here sorry if don't ask in right way. have linked list , want search node it's data using ll.search(data), want return position node in can insert right after it. how that?

this in python.

def search(self,item):     current = self.head     found = false     while current != none , not found:         if current.get_data() == item:             found = true         else:             current = current.get_next()     return found 

here's code search function

full ll implementation:

from node import node class linked_list:     def __init__(self):         self.head = none         def add(self, data):         node = node(data)         if(node != none):             node.next = self.head             self.head = node      def append(self, data):         node = node(data)         if(node != none):             if(self.head == none):                 self.head = node             else:                 trav = self.head                 while(trav.next != none):#find last node                     trav = trav.next                 trav.next = node      def size(self):         count = 0         temp = self.head         while(temp != none):             count = count + 1             temp = temp.next         return count      def search(self,item):         current = self.head         found = false         while current != none , not found:             if current.get_data() == item:                 found = true             else:                 current = current.get_next()         return found      def remove(self, item):         current = self.head         previous = none         found = false         while not found:             if current.get_data() == item:                 found = true             else:                 previous = current                 current = current.get_next()         if previous == none:             self.head = current.get_next()         else:             previous.set_next(current.get_next())       #insert(pos, item) – adds item position pos >= 1.     def insert(self, pos, item):         size = self.size()         if(pos <= 0 or (size - pos )< -1):             print("not enough items in list insert in position. size =",\                   size, "position = ", pos)             return false         if(pos == 1):             newnode = node(item)             newnode.next = self.head             self.head = newnode         else:             count = 2             probe = self.head             while(probe != none , count != pos):                 probe = probe.next                 count += 1             newnode = node(item)             newnode.next = probe.next             probe.next = newnode             return true      #popfirst() – removes , returns first item in list.     def popfirst(self):         if (self.head == none):             return none         else:             curr = self.head             self.head = self.head.next             return curr.data      #pop() – removes , returns last item in list.     def pop(self):         if (self.head == none):             return none         else:             curr = self.head             prev = none             while(curr.next != none):                 prev = curr                 curr = curr.next             if(prev == none):#only 1 item in list                 head = none                 return curr             else:                 prev.next = none                 return curr      def printlist(self, msg):         temp = self.head         print(msg, end = ": ")         while(temp != none):             print(temp.data, end=" ")             temp = temp.next         print()      def isempty(self):         return head.next == none      def peek(self):         if node.next == none:             return false         else:             return node.data 

thanks


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -