Tuesday, July 21, 2015

Python: data structure (4)

Abstract: construct a queue using circle array
The rule of queue:
    1. First In First Out: always remove elements on the head, and add elements on the tail of the array.
    2. circle array for saving time.

The result:
Initiate queue: [0, 1, 3, 4, 9, 3, None, None]
Size of the queue: 5
1 6
remove 1 [3, 4, 9, 3]
2 6
remove 3 [4, 9, 3]
3 6
remove 4 [9, 3]
4 6
remove 9 [3]
5 6
remove 3 []
6 6
6 6
6 6
6 6
## [0, 1, 3, 4, 9, 3, 11, None] 6 7
## [0, 1, 3, 4, 9, 3, 11, 22] 6 8
## [0, 33, 3, 4, 9, 3, 11, 22] 6 2
## [0, 33, 44, 4, 9, 3, 11, 22] 6 3
## [0, 33, 44, 55, 9, 3, 11, 22] 6 4
[11, 22, 33, 44, 55]
ok
The script:
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 21 10:52:48 2015

@author: yuan
"""
#a sample method of queue, but time-consuming
#You have to move every other elements for dequeue or enqueue
class queue_1:
    def __init__(self, arr):
        self.q=arr
               
    #add element: add it to the head of queue
    def enqueue(self, value):
        self.q.insert(0, value)
   
    #remove element: remove the element of the tail
    def dequeue(self):
        self.q.pop()
   
    #
    def isEmpty(self):
        j=False
        if self.q==[]:
            j=True
        return j
       
    def size(self):
        return len(self.q)
#####
#record a queue using circling list
class queue:
   
    def __init__(self,arr, q_len=1):
        self.size=len(arr)
        if q_len==1 or q_len<self.size:       
            self.q=[None]*self.size
            self.q_len=self.size
        else:
            self.q=[None]*q_len
            self.q_len=q_len
        self.q[0]=0
        self.q[1:self.size]=arr
        self.head=1       #skip the index 0
        self.tail=len(arr)+1 #the index of next element of arr
        print 'Initiate queue:', self.q
       
    def q_size(self):
        print 'Size of the queue:', self.size
        return self.size
       
    def export_arr(self):
        if self.tail>self.head:
            arr=self.q[self.head:self.tail]
        elif self.tail==self.head:
            arr=[]
        else:
            arr=self.q[self.head:(self.q_len+1)] + self.q[1:self.tail]
        return arr
   
    def enqueue(self, value):
        if self.size<self.q_len:
            #increase size
            self.size +=1
            #increase tail
            if self.tail==(self.q_len+1):
                self.tail=1
            #add value
            self.q[self.tail]=value
            self.tail += 1
        else:
            print "queue is full, fail to adding", value
        print '##', self.q, self.head,self.tail
        #return self.export_arr()
       
    def dequeue(self):
        #increase head
        value='NA'
        print self.head, self.tail
        if self.size>0:
            value=self.q[self.head]
            if self.head==self.q_len:
                self.head=1
            else:
                self.head +=1
                #decrease size
            self.size -= 1
            print 'remove', value, self.export_arr()
        return value
#main
if __name__ == "__main__":
    a=[1,3,4,9,3]
    #take the list as a queue
    q=queue(a,7)

    #size of the queue
    q.q_size()

    #remove element
    for i in range(9):
        r=q.dequeue()
        #print r
    #print q.export_arr()
       
    #add element
    for i in [11,22,33,44,55]:
        q.enqueue(i)
    print q.export_arr()
    #
    print 'ok'

No comments:

Post a Comment