Reputation: 129
I'm a beginner in python and I'm having a problem in this program:
Here's the NodeList first:
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
My problem is in this program: (The print)
from NodeList import Node
class StackLL:
def __init__(self):
self.head = None
def pushSLL(self,item):
temp = Node(str(item))
temp.setNext(self.head)
self.head = temp
node = self.head
print(chr(0x2510)+(chr(0x0000)*(len(item)))+chr(0x250c))
while node!=None:
stackeditem = chr(0x2502)+node.data+chr(0x2502)
print(stackeditem)
node = node.next
print(chr(0x2514)+(chr(0x2500)*(len(item)-1))+chr(0x2518))
Everytime I print, the lines just seems off. I tried to experiment using len() just to make it accurate, but everytime the item adds more characters it gets off again. Any help would be greatly appreciated. Thank you.
Upvotes: 1
Views: 6227
Reputation: 368944
Use a monospace font as @Kable answered.
In addition to that, the last print
print 1 less chr(0x2500)
.
Some other notes:
\u2502
instead of chr(0x2502)
.pushSLL
.class StackLL:
def __init__(self):
self.head = None
def pushSLL(self, item):
temp = Node(str(item))
temp.setNext(self.head)
self.head = temp
def dump(self):
length = max(len(node.data) for node in self.allNodes()) if self.head else 0
print('\u2510{}\u250c'.format(' '*length))
for node in self.allNodes():
print('\u2502{:<{}}\u2502'.format(node.data, length))
print('\u2514{}\u2518'.format('\u2500'*length))
def allNodes(self):
node = self.head
while node is not None:
yield node
node = node.next
s = StackLL()
s.pushSLL('Arc')
s.pushSLL('Circle')
s.pushSLL('Rect')
s.dump()
Upvotes: 3