Professional Documents
Culture Documents
Stack:: Algorithm For PUSH Operation
Stack:: Algorithm For PUSH Operation
Stack:: Algorithm For PUSH Operation
A stack is an Abstract Data Type (ADT) which allows all data operations at one
end only; element which is placed (inserted or added) last, is accessed first. This
feature makes it LIFO (Last-in-first-out) data structure. In stack terminology,
insertion operation is called PUSH operation and removal operation is
called POP operation.
int pop(stack)
if stack is empty
print “UNDERFLOW”
return null
endif
data ← stack[top]
top ← top - 1
return data
struct stack
{
int elements[MAXSIZE];
int top;
};
/* In main() */
int main ()
{
struct stack s; /* Typical declaration of stack */
s.top=-1; /* Top of stack s is initialized to -1 so that,
the first element can be pushed in 0th position
of the array*/
...
...
...
push(&s,value); /* Typical call to push function */
...
...
...
value = pop(&s); /* Typical call to pop function */
...
...
...
display(&s); /* Typical call to display function */
...
...
...
}
Queue is an ADT which is open at both its ends. One end is always used to insert
data (enqueue) and the other is used to remove data (dequeue). Queue
follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.
enqueue(queue, value)
if queue is full
print “OVERFLOW”
return
endif
rear ← rear + 1
queue[rear] ← value
int dequeue(queue)
if queue is empty
print “UNDERFLOW”
return
end if
value = queue[front]
front ← front + 1
return value