Lecture 4
Lecture 4
Lecture 4
Lecture 4 – Stack
1. As all the deletion and insertion in a stack is done from top of the stack,
the last added element will be the first to be removed from the stack.
2. This is the reason why the stack is also called Last In – First Out (LIFO)
type of list.
3. It is interesting to notice that the most frequently accessible element in
the stack are the most top elements, while the least accessible elements
located at the bottom of the stack.
4. Stack is an ordered list of similar data type.
Figure 1 represents a stack with the size of six locations and includes five
elements.
|Page1
م مروى اديب الجواهري. أ:اعداد Data Structures
2. POP: is the process of deleting an element from the top of the stack. After
every pop operation, the top is decremented by one. If there are no elements in
the stack and pop operation is performed, the result is called stack underflow.
Figure 3 and 4 show an example of push and pop operations
To use a stack efficiently, we need to check the status of stack as well. For the
same purpose, the following functionality is added to stacks:
• peek() − get the top data element of the stack, without removing it.
|Page3
م مروى اديب الجواهري. أ:اعداد Data Structures
int peek()
{
return stack[top];
}
2-isfull()
Algorithm of isfull () function −
begin procedure isfull
if top equals to MAXSIZE
return true
else
return false
endif
end procedure
Implementation of isfull() function in C# programming language −
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
3-isempty()
Algorithm of isempty() function −
begin procedure isempty
if top less than 1
|Page4
م مروى اديب الجواهري. أ:اعداد Data Structures
return true
else
return false
endif
end procedure
return false;
4-Push Operation
The process of putting a new data element onto stack is known as a Push
Operation. Push operation involves a series of steps :
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
|Page5
م مروى اديب الجواهري. أ:اعداد Data Structures
}}
5- Pop Operation
Accessing the content while removing it from the stack, is known as a Pop
Operation. In an array implementation of pop() operation, the data element is
not actually removed, instead top is decremented to a lower position in the
stack to point to the next value. But in linked-list implementation, pop()
actually removes data element and deallocates memory space. A Pop operation
may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
|Page6
م مروى اديب الجواهري. أ:اعداد Data Structures
Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
Step 4 − Decreases the value of top by 1.
Algorithm for Pop Operation A simple algorithm for Pop operation can be
derived as follows −
begin procedure pop: stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Implementation of this algorithm in C, is as follows −
int pop(int data)
{
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}
else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
|Page7
م مروى اديب الجواهري. أ:اعداد Data Structures
Stack Terminology
1. Size: this term refers to the maximum size of the stack or the number of
possibly added elements.
2. TOP: this term refers to the top of the stack. It is a stack pointer used to
check overflow and under flow conditions. The initial value of TOP is -1 when
the stack is empty.
Example 1:
What is the obtained string after performing the following sequence of push
and pop:
PUSH(A), PUSH(B), POP, PUSH(C), POP, PUSH(D), PUSH(E), POP, POP,
POP
|Page8
م مروى اديب الجواهري. أ:اعداد Data Structures
Solution:
Example 2:
What is the obtained number after performing the following sequence of push
and pop:
PUSH(1), POP, PUSH(2), PUSH(3), POP, PUSH(4), POP, PUSH(5), POP,
POP.
Solution:
step Operation Stack Output
1 PUSH(1) 1
2 POP 1
3 PUSH(2) 2 1
4 PUSH(3) 23 1
5 POP 2 13
6 PUSH(4) 24 13
7 POP 2 134
|Page9
م مروى اديب الجواهري. أ:اعداد Data Structures
8 PUSH(5) 25 134
9 POP 2 1345
10 POP 13452
| P a g e 11