Stack ADT
Stack ADT
Handwritten Notes- Click Here
A Stack is a linear data structure in which insertion and deletion of elements are performed only at one end. This end is called the TOP.
Stack follows the LIFO (Last In, First Out) principle. It means the element inserted last will be removed first.
A stack can be implemented using:
- Array
- Linked List
Basic Stack Operations:
1. Push-used to insert a new element into the stack.
2. Pop-used to remove an element from the stack.
3. Peek-used to view the element at the TOP without removing it.
4. isEmpty-checks whether the stack contains any elements.
5. isFull-checks whether the stack has reached its maximum capacity
Push() Operation
The process of inserting a new element at the TOP of the
stack is called Push.
Algorithm
- Check
whether the stack is full.
- If
the stack is full, report Stack Overflow.
- Otherwise,
increment TOP.
- Insert the new element at the TOP position.
Before Push
|
|
|
|
|
|
|
A |
|
|
|
|
|
|
|
|
|
|
void push(int data)
{
if (top == MAXSIZE - 1)
{
printf("Stack Overflow");
}
else
{
top++;
stack[top] = data;
}
Pop() Operation
The process of removing the top element from the stack
is called Pop.
The element at the TOP is always removed first.
Algorithm
- Check
whether the stack is empty.
- If
the stack is empty, report Stack Underflow.
- Store
the element present at TOP.
- Decrement
TOP.
- Return
the deleted element.
Before Pop
Perform Pop.The element E is removed.
A |
{
int data;
if (top == -1)
{
printf("Stack Underflow");
return -1;
}
data = stack[top];
top--;
return data;
Comments
Post a Comment