LinkedList code in python -


__author__ = 'jarvis' class node:     def __init__(self, num):         self.next = none         self.data = num  class llist:     def __init__(self, h=none):         self.head = none      def insert_node(self, num):         node = node(num)         if self.head none:             self.head = node         else:             node.next = self.head             self.head = node      def print_list(self):         node = self.head         while not (node none):             print node.data             node = node.next  lis = llist() lis.insert_node(10) lis.insert_node(20) lis.insert_node(30) lis.print_list() 

i new python. trying create linkedlist. no error shown nothing getting displayed. when trying debug. program exiting without happening. not able understand problem is.

you shouldn't setting node.next = self.head. should maintaining pointer both head , tail. i've modified code below.

class node:     def __init__(self, num):         self.next = none         self.data = num  class llist:     def __init__(self, h=none):         self.head = none         self.tail = none      def insert_node(self, num):         node = node(num)         if self.head none:             self.head = node             self.tail = self.head         else:             self.tail.next= node             self.tail = node      def print_list(self):         node = self.head         while not (node none):             print node.data             node = node.next 

Popular posts from this blog