Dr. Amna Ali A Mohamed
Faculty of Information Technology
CHAPTER 5: STACKS AND QUEUES
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
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
STACKS implimentarions AND stack applications .pptx
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().
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
}
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);
}
}
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
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
#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;
}
• 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:
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
Any Questions?

More Related Content

PPTX
Data Structure.pptx
PPTX
Stack.pptx
PPTX
Stack,queue and linked list data structure.pptx
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
Data Structures Stack and Queue Data Structures
DOCX
DSA- Unit III- STACK AND QUEUE
PPTX
Stack of Data structure
PPTX
Stack and its applications
Data Structure.pptx
Stack.pptx
Stack,queue and linked list data structure.pptx
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Data Structures Stack and Queue Data Structures
DSA- Unit III- STACK AND QUEUE
Stack of Data structure
Stack and its applications

Similar to STACKS implimentarions AND stack applications .pptx (20)

PPTX
stack_ppt_DSA(sudipta samanta).pptx push,pop,peek operation
PDF
Data structure lab manual
PPTX
6 - STACKS in Data Structure and Algorithm.pptx
PDF
04 stacks
PPTX
CS8391-Data Structures Unit 2
PPTX
DS- Stack ADT
PPTX
Stack and its operations, Queue and its operations
PPTX
Abscddnddmdkwkkstack implementation.pptx
PPT
03 stacks and_queues_using_arrays
PPTX
Stack and Queue.pptx university exam preparation
PPTX
The presentation on stack data structure
PDF
Chapter 4 stack
PDF
PPT
7 stacksqueues
PPT
Stacks
PPTX
Stack data structure
PPTX
Data Structures and Agorithm: DS 06 Stack.pptx
PDF
PPT
Lec-7 Stacks FOR BSCS STUDENTS STACK EXPLANATION
PPT
Stack in Data Structure
stack_ppt_DSA(sudipta samanta).pptx push,pop,peek operation
Data structure lab manual
6 - STACKS in Data Structure and Algorithm.pptx
04 stacks
CS8391-Data Structures Unit 2
DS- Stack ADT
Stack and its operations, Queue and its operations
Abscddnddmdkwkkstack implementation.pptx
03 stacks and_queues_using_arrays
Stack and Queue.pptx university exam preparation
The presentation on stack data structure
Chapter 4 stack
7 stacksqueues
Stacks
Stack data structure
Data Structures and Agorithm: DS 06 Stack.pptx
Lec-7 Stacks FOR BSCS STUDENTS STACK EXPLANATION
Stack in Data Structure
Ad

Recently uploaded (20)

PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PPTX
future_of_ai_comprehensive_20250822032121.pptx
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PDF
SaaS reusability assessment using machine learning techniques
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PPTX
MuleSoft-Compete-Deck for midddleware integrations
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PDF
INTERSPEECH 2025 「Recent Advances and Future Directions in Voice Conversion」
PPTX
Internet of Everything -Basic concepts details
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
future_of_ai_comprehensive_20250822032121.pptx
Improvisation in detection of pomegranate leaf disease using transfer learni...
SaaS reusability assessment using machine learning techniques
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
Rapid Prototyping: A lecture on prototyping techniques for interface design
MuleSoft-Compete-Deck for midddleware integrations
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
4 layer Arch & Reference Arch of IoT.pdf
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Training Program for knowledge in solar cell and solar industry
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
giants, standing on the shoulders of - by Daniel Stenberg
INTERSPEECH 2025 「Recent Advances and Future Directions in Voice Conversion」
Internet of Everything -Basic concepts details
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
EIS-Webinar-Regulated-Industries-2025-08.pdf
Connector Corner: Transform Unstructured Documents with Agentic Automation
Ad

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