Stack and queues

A stack is a LIFO (Last in First out) structure like a stack of plastic cups can only add to top and remove from bottom

PUSH(x) adds x to the top

POP() removes and returns the top

PEEK() Returns top item without removing


Procedure push(item)

    if stack is full

        stack full error and stop

    else

        set data at stack pointer

        increment stack pointer


Function pop()

    if stack is empty then

        stack empty error

        return null

    else

        decrement stack pointer

        return value at stack pointer



Queues is a FIFO (First in First out) structure like a queue in a store first to arrive is first to leave


ENQUEUE(x) adds x to rear

DEQUEUE() Removes and returns front

PEEK() Views the front element


array myQueue[5]

headPointer = 0

tailPointer = 0


procedure enqueue(newitem)

    if tailPointer == myQueue.length

        print(‘FULL‘)

    else

        myQueue[tailpointer] = newItem

        tailPointer = tailPointer + 1


procedure dequeue()

    if headPointer == tailPointer

        print(‘EMPTY‘)

        return -1

    else

        item = myQueue[headPointer]

        headPointer = headPointer + 1

        return item