A stack is basically a data object.
The Operational semantic of stack is LIFO, i.e Last In First Out.
Definition: It is an ordered list of elements n, such that n>0 in which all the insertion and deletion are made at one end called "Top".
Primary Operations on Stack:
PUSH AND POP ALGORITHM
Push:
push(item, array, n , top)
{
if(n >= top)
{ print("Stack is full"); }
else
{
top=top+1;
array[top] = item;
}
}
POP:
pop(item,array,top)
{
if(top <= 0)
{
print "Stack is empty.";
}
else
{
item=array[top];
top=top-1;
}
}
VISUAL DEMO:
| |
| |
| |
| | isempty() = TRUE
| | isfull() = FALSE
|____|
Empty Stack
Push 36
| |
| |
| |
| | isempty() = FALSE
| 36 | isfull() = FALSE
|____|
Push 42
| |
| |
| |
| 42 |
| 36 |
|____|
Push 86
| |
| |
| 86 |
| 42 |
| 36 |
|____|
Push 23
| |
| 23 |
| 86 |
| 42 |
| 36 |
|____|
Push 18
| 18 |
| 23 | isfull() = TRUE
| 86 | Another push will cause error.
| 42 |
| 36 |
|____|
POP 18
| |
| 23 |
| 86 |
| 42 |
| 36 |
|____|
POP 23
| |
| |
| 86 |
| 42 |
| 36 |
|____|
POP 86
| |
| |
| |
| 42 |
| 36 |
|____|
POP 42
| |
| |
| |
| |
| 36 |
|____|
POP 36
| |
| |
| |
| |
| |
|____|
The Operational semantic of stack is LIFO, i.e Last In First Out.
Definition: It is an ordered list of elements n, such that n>0 in which all the insertion and deletion are made at one end called "Top".
Primary Operations on Stack:
- PUSH: Push (Add) an elemrnt to the top.
- POP: Pop (Remove) an element from the top.
- Also "IsEmpty()" and "IsFull()" operations to check whether the stack is empty or full.
- Daily Life: A pile of books in vertical box , dishes kept on top of one another.
- Computer: In processing subroutines calls and returns, there is an explicit use of stack data structure.
PUSH AND POP ALGORITHM
Push:
push(item, array, n , top)
{
if(n >= top)
{ print("Stack is full"); }
else
{
top=top+1;
array[top] = item;
}
}
POP:
pop(item,array,top)
{
if(top <= 0)
{
print "Stack is empty.";
}
else
{
item=array[top];
top=top-1;
}
}
VISUAL DEMO:
| |
| |
| |
| | isempty() = TRUE
| | isfull() = FALSE
|____|
Empty Stack
Push 36
| |
| |
| |
| | isempty() = FALSE
| 36 | isfull() = FALSE
|____|
Push 42
| |
| |
| |
| 42 |
| 36 |
|____|
Push 86
| |
| |
| 86 |
| 42 |
| 36 |
|____|
Push 23
| |
| 23 |
| 86 |
| 42 |
| 36 |
|____|
Push 18
| 18 |
| 23 | isfull() = TRUE
| 86 | Another push will cause error.
| 42 |
| 36 |
|____|
POP 18
| |
| 23 |
| 86 |
| 42 |
| 36 |
|____|
POP 23
| |
| |
| 86 |
| 42 |
| 36 |
|____|
POP 86
| |
| |
| |
| 42 |
| 36 |
|____|
POP 42
| |
| |
| |
| |
| 36 |
|____|
POP 36
| |
| |
| |
| |
| |
|____|
No comments:
Post a Comment