a graphical for double linked list

I made this today to print of a better graphical representation of my double linked list.
I was testing my push, 'append', add... whatever you want to call it,  and I thought, this is really hard to look at.  I really wish there was an easier way to look at this...  So I decided to make it easier to look at.    Here's a snippet of what I did and the results in the command prompt.

If the node fails, so will the output to command prompt.  I'm sure it needs more testing, but I'm pretty happy with it.  Try it out!



And here's my code so far:::
Update 12-5-17:
I realized I did not account for size 0, and 1 list,  fixed and updated:  Here's the graphical and code so far:

class DLLNode(object):
    # I changed the order so that it would be easier to visualize
    # and easier to keep track of.
    def __init__(self, prev, value, nxt):
        # the first node will have no next or previous
        self.value = value
        self.next = nxt
        # previous
        self.prev = prev

    def __repr__(self):
        nval = self.next and self.next.value or None
        pval = self.prev and self.prev.value or None
        ##  previous node,  assigned value, link to next node in list ##
        return f"[{repr(pval)}, {self.value}, {repr(nval)}]"

class DLList(object):
    def __init__(self):
        #  Ok  this needs to stay the same...
        #  end holds the last node
        self.end = None
        #  begin holds the first node.....
        self.begin = None


    def push(self, obj):
     

        # if there is an empty list create first node with no next or prev.

        if self.end == None and self.begin == None:

           newnode = DLLNode(None, obj, None)
           self.begin = newnode
           #  I want it to also print something off like 
           ##  ( (--) ,  1 , (--) )  ##  when it prints the list.....
           #  did this in 'my print out methods'  method: graphical()

         
        # if there is only a first node, create next node that links back and forward
        # set.begin node * CHECK ZED'S SOLUTION INTRO *
        elif self.end == None and self.begin != None:
            # get the address of the original node
            node = self.begin
            # create address for new node
            newnode = DLLNode(node, obj, None)
            self.end = newnode
            self.begin.next = self.end
       
        else:
         
            node = self.end
            newnode = DLLNode(node, obj, None)
            self.end.next = newnode
            self.end = newnode

        assert self.end != self.begin

   

#################----------------------------------##############
#######                MY PRINT OUT METHODS             #########        
             ###( <--- previous, obj , next --->)###
           
    def graphical(self):
   
        ## None = (/)
        ## [ link = number in count ]
        ## OBJ = Data or Value?
        ## so, [  1 ,  DATA ,  (/)  ]

        length = self.count()
        node = self.begin
        if self.begin == None:
            print(" EMPTY ")
        elif self.begin != None and self.end == None:
            print("\n\n")
            # [(/), DATA, (/)]
            print(f"{node.value}=")
            print(f"[(/), DATA, (/)]")
        else:
            node = self.end
            if node:
                print("\n\n")
            while node:
                # example, length is 3 
                if node.next == None and node.prev != None:
                    # [ 2 , DATA , (/) ]
                    print(f"{node.value}=")
                    print(f"\t [ {length -1}, DATA , (/) ]")
                elif node.next != None and node.prev != None:
                    # [ 1 , DATA , 2]
                    print(f"{node.value}=")
                    print(f"\t [ {length -2}, DATA, {length -1}]")
                    length -= 1
                else:
                    # [ (/), DATA , 1]
                    print(f"{node.value}=")
                    print(f"\t [ (/), DATA, 1 ]")
                node = node.prev

    ########  <------   prev ---- end 
    def what_start(self):
        print(" back = ")
        node = self.end
        if node:
            print(node.next)
            while node:
            
                print(node)
                node = node.prev

    ########   begin ----->  next 
    def where_end(self):
        node = self.begin
        print(" front = ")
        if node:
            while node:
                print(node)
                node = node.next

###################      END my print outs    #################################

################################## count ######################################
    def count(self):
        count = 0
        node = self.begin
        while node:
            count +=1
            node = node.next
        return count
################################  END count ####################
#################### POP ##################################
    def pop(self):
        # Zed said it would be just like SLL.....
        # [ (/) , DATA , ---->] [<----, DATA, ---->] [<----, DATA, (/)]
        # [ (/) , DATA , ---->] [<----, DATA, (/) ] ....................
        # if list is empty:
        if self.begin == None:
            print(" Nothing to PoP! ")
            return None
        elif self.begin and not self.end:
            print(" That was a lonely little data...")
            carrots = self.begin.value
            self.begin = None
            return carrots
        else:
            carrots = self.end.value
            newEnd = self.end.prev
            self.end = newEnd
            if newEnd == self.begin:
                self.begin.next = None
                self.end = None
            newEnd.next = None
            return carrots

##########################   END POP ##############################

#####################------------------------------------#######################
#################     -- GET --   and it's   _invariant       #######################
####################-------------------------------------########################
   
    def get(self, index):
     
        length = self.count()     
        count = 0
        if length == 0:
            # for testing, None works better then "Empty list!"
            return(None)
        elif index > (length):
            # now I know what this message is all about.
            # the index is greater then the length of the list
            # remember index starts at 0
            return(" index out of range! ")
        else:
            node = self.begin
            while node:
               
                if count == index:
                    return(node.value)
                else:
                    node = node.next
                   
                count += 1
           
    def _get(self, index):
        length = self.count()     
       
        if length == 0:
            # for testing, None works better then "Empty list!"
            return(None)

        elif index > (length):
            # now I know what this message is all about.
            # the index is greater then the length of the list
            # remember index starts at 0
            return(" index out of range! ")

        elif length == 1:
            # when there are 0, or 1 item's on the list, else loop won't work.
            if index == 0:
                node = self.begin
                return(node.value)
         

        else:
            node = self.end
            #  [(/)(0)DATA -->][<---(1)DATA--->][<---(2)DATA--->][<---(3)DATA (/) ]
            # length    1                           2                             3                            4
            while node:
               
                if length == index + 1:
                    return(node.value)
                else:
                    node = node.prev
                   
                length -= 1

########################## END GET###############################
       


       

Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

playing with color in powershell python