0% found this document useful (0 votes)
81 views

Chapter 4 - Stack

This document discusses stacks and their implementation. It defines key stack terms like LIFO, push, pop, top, overflow and underflow. It provides examples of stacks in real life like a stack of books. The basic stack operations of push, pop and peek are explained. Finally, it demonstrates how to implement a stack using an array with functions for creating the stack, checking if it's empty/full, and pushing/popping elements.

Uploaded by

Wani Syazwani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Chapter 4 - Stack

This document discusses stacks and their implementation. It defines key stack terms like LIFO, push, pop, top, overflow and underflow. It provides examples of stacks in real life like a stack of books. The basic stack operations of push, pop and peek are explained. Finally, it demonstrates how to implement a stack using an array with functions for creating the stack, checking if it's empty/full, and pushing/popping elements.

Uploaded by

Wani Syazwani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

1

DIT 2014
Chapter 4 :
Stack
Shazana Md Zin, FTMM (2010)
2
What is Stack?
3
Stack related term
• A memory portion, which is used for storing the elements.
• Based on principle of Last-In-First-Out (LIFO).
• TOP – the pointer points to the top element in the stack.
• Stack Underflow – there is no element in the stack.
• Stack Overflow – the stack contains equal or no more
elements to be added.
• PUSH – inserting new element to the top of the stack.
• POP – removing one element from the top of the stack.
4
Introduction
5
Introduction
6
Introduction
7
Basic Stack Operations

 There is no need to search or remove an


arbitrary node
 The last element added is the first to removed
 push – Adds an item to the top of a stack.
 pop – Removes an item from the top of the stack
and returns it to the user.
 stack top – Copies the top item of the stack and
returns it to the user; the item is not removed,
hence the stack is not altered.
Last - In- First - Out
 The last element added is the first to be
removed
 Example – The stack of rice bowls in a
cupboard, the stack of books.

8
9
Process
 Example:
 Push books
 Red Book
 Blue Book
 Green Book
 Yellow Book
 Pop books
 Yellow Book
 Green Book
 Blue Book
 Red Book
10
Stack Operation
 Create Stack
 size = 4

3
2
1
0

top = -1
11
Stack Operation
 Make sure the stack is empty@not – before ‘pop’
 If ‘0’ => cannot ‘pop’

3
2
1
0

top =-1
12
Stack Operation
 Make sure the stack is empty@not – before ‘push’
 If stack is full => cannot ‘push’

3
2
1
0

top = 3
13
Stack Operation
 Push data into the stack
 If stack ≠ full => ‘push’ to add a node

3
2
1
0
top -1
1
0
3
2
top
14
Stack Operation
 Pop data from the stack
 If stack ≠ empty => ‘pop’ to remove a node

3
2
1
0
top -1
1
2
0
3
top
Stack Array Implementation
Struct Declaration

typedef struct STACK


3
{
int top; 2
int list[3]; 1
}stack; 0
top list
Create Stack

2
void create(stack *t)
{ 1
t->top = -1; -1 0
}
top list
Overflow and Underflow Controller

int empty(stack *t)


{
if(t->top == -1)
return (1); int full(stack *t)
else {
return(0); if (t->top ==3)
} return (1);
else
return (0);
}
Push Data

void push(stack *t)


{
int data; 3
if (full(t) == 1)
cout<<“Stack is Full”<<endl; 2
else 1
{ 0 0 10
cout<<“Push Data : “<<endl;
cin>> data; top list
t->top++;
t->list[t->top] = data;
}
}
Pop Data

void pop(stack *t) 3


{
2
if(empty(t) == 1)
cout<<“Stack is Empty”<<endl; 1
else -1 0 10
t->top--;
} top list
Stack Class Implementation

 Create the stack


 Insert element to the stack – PUSH()
 Remove element from the stack – POP()

Using array and pointer implementation


21
Full Coding
Conclusion

 Understand the concept of the stack


 How to create the stack
 Remove your data from the stack POP()
 Insert your data to the stack Push()
 Implementing ARRAY and LINKED LIST to build
your stack
THANK
YOU

You might also like