STACKS implimentarions AND stack applications .pptx
1. Dr. Amna Ali A Mohamed
Faculty of Information Technology
CHAPTER 5: STACKS AND QUEUES
2. 1. Introduction to Stacks
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.
The last element added to the stack will be the first one to be removed.
Key Characteristics:
• Ordered collection of elements
• Two main operations: push (add) and pop (remove)
• Access is restricted to the top element only
• Fixed size when implemented using arrays
Ways to implement Stack
1.Implement stack using Array (discussed in this article)
2.Implement stack using LinkedList
3. 2. Operations on Stack and How to Implement:
The fundamental operations we'll implement:
1. push()- Add an element to the top of the stack
2. pop()- Remove the top element from the stack
3. peek()- View the top element without removing it
4. isEmpty()- Check if stack is empty
5. isFull()- Check if stack is full
6. display()- Display all elements in the stack
5. Push operations on stack
Pop operations on stack
When an element is taken off from the stack, the operation is performed by pop().
The folling Figure shows a stack initially with three elements and shows the deletion of
elements using pop().
6. 3. Array Implementation
3.1 Structure Definition
#define MAX 10 // Maximum size of stack
struct Stack {
int items[MAX]; // Array to store stack elements
int top; // Index of top element
};
3.2 Initialization
void initialize(struct Stack *s) {
s->top = -1; // -1 indicates empty stack
}
7. 3.3 Checking Stack Status
int isEmpty(struct Stack *s) {
return (s->top == -1);
}
int isFull(struct Stack *s) {
return (s->top == MAX - 1);
}
3.4 Push Operation
void push(struct Stack *s, int value) {
if (isFull(s)) {
printf("Stack Overflow! Cannot push %dn", value);
} else {
s->top++;
s->items[s->top] = value;
printf("%d pushed to stackn", value);
}
}
8. 3.5 Pop Operation
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow! Cannot popn");
return -1; // Error value
} else {
int value = s->items[s->top];
s->top--;
return value;
}
}
int peek(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is emptyn");
return -1;
} else {
return s->items[s->top];
}
}
3.6 Peek Operation
9. 3.7 Display Operation
void display(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is emptyn");
} else {
printf("Stack elements:n");
for (int i = s->top; i >= 0; i--) {
printf("%dn", s->items[i]);
}
}
}
4. Complete Example Program
10. #include <stdio.h>
#define MAX 10
struct Stack {
int items[MAX];
int top;
};
// Function prototypes
void initialize(struct Stack *s);
int isEmpty(struct Stack *s);
int isFull(struct Stack *s);
void push(struct Stack *s, int value);
int pop(struct Stack *s);
int peek(struct Stack *s);
void display(struct Stack *s);
int main() {
struct Stack myStack;
initialize(&myStack);
int choice, value;
while (1) {
printf("nStack Operations:n");
printf("1. Pushn");
printf("2. Popn");
printf("3. Peekn");
printf("4. Displayn");
printf("5. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&myStack, value);
break;
case 2:
value = pop(&myStack);
if (value != -1) {
printf("Popped value: %dn", value);
}
break;
case 3:
value = peek(&myStack);
if (value != -1) {
printf("Top element: %dn", value);
}
break;
case 4:
display(&myStack);
break;
case 5:
return 0;
default:
printf("Invalid choicen");
}
}
return 0;
}
11. • Stack is used by compilers to check for balancing of parentheses, brackets and braces.
• Stack is used to evaluate a postfix expression.
• Stack is used to convert an infix expression into postfix/prefix form.
• In recursion, all intermediate arguments and return values are stored on the processor’s stack.
• During a function call the return address and arguments are pushed onto a stack and on return
they are popped off.
Applications of stacks:
12. 1. Stack Pointer (top):
1. Initialized to -1 (empty stack)
2. Points to the current top element
3. Incremented before push, decremented after pop
2. Stack Overflow:
1. Occurs when trying to push to a full stack (top == MAX-1)
2. Prevented by checking isFull() before push
3. Stack Underflow:
1. Occurs when trying to pop from an empty stack (top == -1)
2. Prevented by checking isEmpty() before pop
5. Key Points to Remember