how to implement 3 stack in a single array efficiently?
<p>it is a python code..whether implementing using linked list .... is efficient in this way...........</p>

<pre><code>data = []            # data storage for stacks represented as linked lists
stack = [-1, -1, -1] # pointers to each of three stacks (-1 is the "null" pointer)
free = -1            # pointer to list of free stack nodes to be reused

def allocate(value):
    ''' allocate a new node and return a pointer to it '''
    global free
    global data
    if free == -1:
        # free list is empty, need to expand data list
        data += [value,-1]
        return len(data)-2
    else:
        # pop a node off the free list and reuse it
        temp = free
        free = data[temp+1]
        data[temp] = value
        data[temp+1] = -1
        return temp

def release(ptr):
    ''' put node on the free list '''
    global free
    temp = free
    free = ptr
    data[free+1] = temp

def push(n, value):
    ''' push value onto stack n '''
    global free
    global data
    temp = stack[n]
    stack[n] = allocate(value)
    data[stack[n]+1] = temp

def pop(n):
    ''' pop a value off of stack n '''
    value = data[stack[n]]
    temp = stack[n]
    stack[n] = data[stack[n]+1]
    release(temp)
    return value

def list(ptr):
    ''' list contents of a stack '''
    while ptr != -1:
        print data[ptr],
        ptr = data[ptr+1]
    print

def list_all():
    ''' list contents of all the stacks and the free list '''
    print stack,free,data
    for i in range(3):
        print i,":",
        list(stack[i])
    print "free:",
    list(free)

push(0,"hello")
push(1,"foo")
push(0,"goodbye")
push(1,"bar")
list_all()
pop(0)
pop(0)
push(2,"abc")
list_all()
pop(1)
pop(2)
pop(1)
list_all()
</code></pre>

<p>r there is any way to do this efficiently other than this??implement in this way in c /c++ would be eficient???</p>
