simple Queue in python

The instructions say,  enter data from the beginning, tail of the list, and remove it from the back/end head of the list....  but I really have a hard time keeping the terminology correct.  When your adding the second item, data into the list, you have to set the link in the head/end ...  creating the new node, and setting it as the self.tail.... is that what they mean by 'entering the data'?  isn't the data being created in the python compiler? 

Anyways, questions for another time I think.  Here it is! 
Bare bones.   Next up in class is 'bubble sort'.   That looks like fun.  It's almost the weekend and I'm downloading Final Fantasy online.  I'm sure I'll be coding.  Especially since I want to try and make something  like this with these nodes:

   ( node[ (namespace)     ---->   [data][data][data]] ) ----->(node[(namespace) ---->[data][data][data]])

PYTHON QUEUE:


class DLLNode(object):
    def __init__(self, prev, value, nxt):
        self.prev = prev
        self.value = value
        self.next = nxt

    def __repr__(self):
        pval = self.prev and self.prev.value or None
        nval = self.next and self.next.value or None
        return f"[{repr(pval)}, {self.value}, {repr(nval)}]"

class Queue(object):
    ###   FIRST IN   ----   LAST OUT   ###
    def __init__(self):
        # head = end
        self.head = None
        # tail = begin
        self.tail = None

        ##  ONLY ALLOWS SHIFT AND UNSHIFT
        ##  The doubly linked list is useful for the computer's memory to traverse 
        ##  through the list and find data
        ##  how the user gets to handle that data is what the class decides. 
        ##  Some computers may be able to do things some can not, so  
        ##  different kinds of lists to do different things depending on what can be 
        ##  handled based on what it's being used for, in what context.
        ##  this is what I didn't know, or understand

################### SHIFT/ add/push #####################################
        ## User gets to enter data in from the tail....the begin of the list
    def shift(self,obj):
        # tail == begin
        # head == end
        node = self.head

        if node == None:
            # EMPTY
            newnode = DLLNode(None, obj, None)
            self.tail = newnode
            self.head = newnode

        elif self.tail == self.head:
            node = self.tail
            newnode = DLLNode(None, obj, node)
            self.head.prev = newnode
            self.tail = newnode

        else:
            node = self.tail
            #[None, oldtail, --->]
            newnode = DLLNode(None, obj, node)
            #[None, newtail, ---oldtail--->][ None, oldtail, ---->]
            self.tail.prev = newnode
            self.tail = newnode



################# END SHIFT ###################################
    def count(self):
        # tail = begin
        node = self.tail
       
        count = 0
        while node:
            node = node.next
            count += 1
        return count
       
       
    def graphical(self):
        # tail = begin
        begin = self.tail
        # head = end
        end = self.head
        length = self.count()

        if begin == None:
            print(" EMPTY ")
        elif begin and not end:
            print(f"[NONE, (1){begin.value}, NONE]")
        else:

            node = self.head
            if node:
                print("\n")
            while node:

                if node.prev != None and node.next == None:
                    print(f"\t[<---{length - 1}, ({length}){node.value},  NONE ]")
                elif node.prev != None and node.next != None:
                    print(f"\t[<---{length-2}, ({length-1}){node.value}, {length}--->]")
                    length -= 1
                    # if the length increment isn't here... its' wrong.
                else:
                    print(f"\t[NONE , (1){node.value}, 2--->]")
                node = node.prev
             
                #[<-- prev, data, next-->]
                #[<----3, (4)stuff, NONE]
                #[<----2, (3)more, 4--->]
                #[<----1, (2)and,  3--->]
                #[NONE, (1)spam, 2--->]





        ## only gets to take data off from the head.....
################# UNSHIFT / remove from end ##########################
    def unshift(self):
        # head == end 
        node = self.head
        ### remove from the end of list...
        length = self.count()

        if length == 2:

            node = self.tail
            newnode = DLLNode(None, node.value, None)
            self.head = newnode
            self.tail = newnode

        elif length == 1:

            self.head = None
            self.tail = None
        else:
           
            self.head = node.prev
            self.head.next = None

         

########
###########  END UNSHIFT ##########################################

burp = Queue()
burp.shift('one')
burp.shift('two')
burp.shift('three')
burp.graphical()
burp.unshift()
burp.graphical()

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