0% found this document useful (0 votes)
10 views9 pages

Lecture 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Lecture 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lecture 4: Stack

Md. Nazmul Abdal


Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Introduction

⚫ A stack is a linear data structure that follows the principle of


Last In First Out (LIFO).
⚫ This means the last element inserted inside the stack is removed
first.
⚫ In programming terms, putting an item on top of the stack is
called push and removing an item is called pop.
Basic Operations

⚫ Push: Add an element to the top of a stack

⚫ Pop: Remove an element from the top of a stack

⚫ IsEmpty: Check if the stack is empty

⚫ IsFull: Check if the stack is full

⚫ Peek: Get the value of the top element without removing it


Working Procedure

⚫ A pointer called TOP is used to keep track of the top element in


the stack.
⚫ When initializing the stack, we set its value to -1 so that we can
check if the stack is empty by comparing TOP == -1.
⚫ On pushing an element, we increase the value of TOP and place
the new element in the position pointed to by TOP.
⚫ On popping an element, we return the element pointed to by
TOP and reduce its value.
⚫ Before pushing, we check if the stack is already full
⚫ Before popping, we check if the stack is already empty
Working Procedure
Example

#include <stdio.h>
#include <stdlib.h> // Check if the stack is empty
#define MAX 10 int isempty(st *s) {
int count = 0; if (s->top == -1)
// Creating a stack return 1;
struct stack { else
int items[MAX]; return 0;
int top; }
};
typedef struct stack st; // Add elements into stack
void createEmptyStack(st *s) { void push(st *s, int newitem) {
s->top = -1; if (isfull(s)) {
} printf("STACK FULL");
// Check if the stack is full } else {
int isfull(st *s) { s->top++;
if (s->top == MAX - 1) s->items[s->top] = newitem;
return 1; }
else count++;
return 0; }
}
Example

// Remove element from stack // Driver code


void pop(st *s) { int main() {
if (isempty(s)) { int ch;
printf("\n STACK EMPTY \n"); st *s = (st *)malloc(sizeof(st));
} else {
printf("Item popped= %d", s->items[s->top]); createEmptyStack(s);
s->top--;
} push(s, 1);
count--; push(s, 2);
printf("\n"); push(s, 3);
} push(s, 4);
// Print elements of stack
void printStack(st *s) { printStack(s);
printf("Stack: ");
for (int i = 0; i < count; i++) { pop(s);
printf("%d ", s->items[i]);
} printf("\nAfter popping out\n");
printf("\n"); printStack(s);
} }
Applications

⚫ To reverse a word - Put all the letters in a stack and pop them
out. Because of the LIFO order of stack, you will get the letters in
reverse order.

⚫ In compilers - Compilers use the stack to calculate the value of


expressions like 2 + 4 / 5 * (7 - 9) by converting the expression to
prefix or postfix form.

⚫ In browsers - The back button in a browser saves all the URLs


you have visited previously in a stack. Each time you visit a new
page, it is added on top of the stack. When you press the back
button, the current URL is removed from the stack, and the
previous URL is accessed.
Thank You

You might also like