Python data structure. The values are not getting print -
# linked list class node: def __init__(self): self.data=none self.next=none def setdata(self,data): self.data=data def getdata(self): return self.data def setnext(self,next): self.next=next def getnext(self): return self.next class linkedlist: def buildlist(self): print("enter array :-") x=int(input("enter number :-")) newnode=node() start=node() head=node() head=start start=0 while(x!=-1): newnode.setdata(x) if(start==0): newnode.setnext(none) start=newnode else: ptr=start while(ptr.getnext()!=none): ptr=ptr.getnext() ptr.setnext(newnode) newnode.setnext(none) x=int(input()) print("thus list ") ptr=start while(ptr!=none): print("the data %d",ptr.getdata()) ptr=ptr.getnext() charan=linkedlist() charan.buildlist()
two problems:
you adding
newnode
linked list several times, create once. creates multiple references 1 object; wanted single reference each of several independent objects.you got syntax wrong percent-style string interpolation in
print
statement.
move newnode=node()
inside of while loop, , change print statement print("the data %d" % ptr.getdata())
.
# linked list class node: def __init__(self): self.data=none self.next=none def setdata(self,data): self.data=data def getdata(self): return self.data def setnext(self,next): self.next=next def getnext(self): return self.next class linkedlist: def buildlist(self): print("enter array :-") x=int(input("enter number :-")) start=node() head=node() head=start start=0 while(x!=-1): newnode=node() newnode.setdata(x) if(start==0): newnode.setnext(none) start=newnode else: ptr=start while(ptr.getnext()!=none): ptr=ptr.getnext() ptr.setnext(newnode) newnode.setnext(none) x=int(input()) print("thus list ") ptr=start while(ptr!=none): print("the data %d" % ptr.getdata()) ptr=ptr.getnext() charan=linkedlist() charan.buildlist()
result:
enter array :- enter number :-1 2 3 -1 list data 1 data 2 data 3
Comments
Post a Comment