Tuesday, March 29, 2016

staque, a stack+queue combo data structure

staque:
    a
    count = start = end = 0
    staque(capacity):
        a = new [capacity]

    push(x):
        if isfull():
            throw
        a[end] = x
        count++
        end = mod(end+1, a.length())
    pop(x):
        if isempty():
            throw
        end = mod(end-1, a.length())
        count--
        return a[end]
    enqueue(x):
        if isfull():
            throw
        a[end] = x
        count++
        end = mod(end+1, a.length())
    x dequeue():
        if isempty():
            throw
        x = a[start]
        start = mod(start+1, a.length())
        count--
        return x
    isempty():
        return count == 0
    isfull():
        return count == a.length()
    clear():
        count = start = end = 0
    
    top():
        if isempty():
            throw
        return a[end]
    start():
        if isempty():
            throw
        return a[start]
    end():
        if isempty():
            throw
        return a[end]
    
    mod(x, n):
        m = x % n
        if m < 0:
            m += n
        return m

No comments:

Post a Comment