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

3 Stack

The document discusses stacks, including their definition as a LIFO data structure, common operations like push and pop, and examples of stack applications. It then provides details on implementing stacks using arrays and linked lists, including pseudocode for core operations like create, push, pop, and isEmpty. Implementation details cover using a top pointer, checking for full/empty conditions, and the constant time complexities of stack operations.

Uploaded by

sarsor mansor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

3 Stack

The document discusses stacks, including their definition as a LIFO data structure, common operations like push and pop, and examples of stack applications. It then provides details on implementing stacks using arrays and linked lists, including pseudocode for core operations like create, push, pop, and isEmpty. Implementation details cover using a top pointer, checking for full/empty conditions, and the constant time complexities of stack operations.

Uploaded by

sarsor mansor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data Structures and Algorithms

0404201

4. Stack

Dr. Ahmad Adel Abu-Shareha


Department of Computer Science
Faculty of Information Technology
World Islamic Sciences and Education University (WISE)
Introduction
Stack:
A container that has a single exit, which also considered as its entry.
Stack allows operations at one end only.
The primary stack operations are:
Pushing: Placing a new data item into the stack (on top only).
Popping: Removing an item from of the stack (from top only).
Other operations:
create, isFull, isEmpty, retrieveTop.
Some of the applications are:
microprocessors, some older calculators
etc.

A stack is a last in, first out (LIFO) data


structure:
Items are removed from a stack in the reverse
order from the way they were inserted.
Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha
Introduction
Tracing

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Tracing
Example:
– Given a stack of ints that includes a single integer value 6.
– Using illustration, show the content of the stack after implementing
the following operations:

1. Push 5 ----- ----- | 4 | <-- top -----


2. Push 4 | 6 | <-- top | 5 | <-- top ----- | 5 | <-- top
----- ----- |5| -----
3. Pop stack |6| ----- |6|
4. Push 3 ----- |6| -----
stack ----- stack
5. Pop stack
6. Pop | 3 | <-- top
7. Push 9 ----- ----- -----
|5| | 5 | <-- top | 9 | <-- top
----- ----- ----- -----
|6| |6| | 6 | <-- top |6|
----- ----- ----- -----
stack stack stack stack
Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha
Tracing
Applications- Calculator
• A calculator uses a stack to save the numbers and 5+9-3
implement the mathematical operations as
follows: Token Stack
• The input to the calculator is a token (number or []
operations).
5 [5]
• The calculator process the number then the
operator. 9 [9,5]
• The calculator implements the following with + [14]
each input token: 3 [3,14]
– If the token is a number:
push it into the stack. - [11]
– If the token is an operator OP: = [] → 11
second = pop()
first = pop()
compute: result = first OP second
push the result on the stack
– when there are no more tokens, pop the answer off
the stack

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Tracing
(7-3+8)+(5-6*7)
Applications- Calculator
Token Stack
• IF brackets presented, what between []
brackets in inserted first into the
7 [7]
calculator then the operators
3 [3,7]
between brackets.
- [4]
(4+2)*7 5*(6-5) 8 [8, 4]
Token Stack Token Stack + [12]
[] [] 6 [6,12]
4 [4] 5 [5] 7 [7,6,12]
2 [2,4] 6 [6,5] * [42,12]
+ [6] 5 [5,6,5] 5 [5,42,12]
7 [7,6] - [1,5] - [37,12]
* [42] * [5] + [49]
= [] → 42 = [] →5 = [] → 49
Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha
Implementation – General Procedure
Push: Pop:
1. Check if stack is full, 1. Check if stack is empty,
produce error if it is full. produce error if it is empty.
2. If stack is not empty, retrieve
2. If stack is not full, data element from stack
increment top to point at location, where top is
the next empty space. pointing.
3. Add data element to the 3. Decrement top to point at
stack location, where top the previous element in the
is pointing. stack.
4. Return success.
4. Return success.

IsFull IsEmpty
1. If the stack has limited size: 1. Check if the stack location,
2. Check if the stack location, where top is pointing at, is
where top is pointing at, is the not a real location in the
last location in the stack return
true. stack (ex: null, index = -1) .
Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha
Implementation – Inputs and Output
• The following represents the general input and output of the
common utilized operations.

• These operations are the main methods of any stack class


implementation.
• The inputs and output may vary slightly based on the
implementation base (Array vs. Linked-List).
• Other operations can be implemented such as print content, print
top element, etc.
Method Inputs Output
Create A stack
Push A stack and a data element
Pop A stack A data element
isFull A stack True/False
isEmpty A stack True/False
Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha
Implementation using Linked List
• Stack represented by a linked list is a normal linked list with:
– Pushing is inserting operation at the head-pointing location) (first
element).
– Popping is deleting operation from the head-pointing location (first
element).

• Stack representing by a linked list has no limited capacity (isFull


function is not utilized).

head 3 10 4 8

Pop

head 10 4 8
Push
head 15 10 4 8

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Implementation using Linked List
• The following operations are commonly utilized in the
Linked-List implementation of the stack.

• Other operations such as, top, print and deleteAll can be
implemented as well.

Method Inputs Output


Create Head
Push The head’s name of the stack and a
data element
Pop The head’s name of the stack A data element
isEmpty The head’s name of the stack True/False

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Implementation using Linked List
Pseudo-Code
Creating isEmpty
Algorithm create() Algorithm isEmpty(node* head)
head = null if(head ==null)
end return True
else
Push return false
Algorithm Push(node* head, value x) end
node* p
p->info = x Pop
p->link = null Algorithm pop(node* head)
p-> link = head x = head -> info
head = p head = head ->link
end return x
end

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Implementation using Array
Stack represented by an array is a limited capacity array
with:
– Top pointer (virtual) saves the location of the stack’s top
element in the array.
– OR an integer indicates the number of elements.
– Pushing is inserting operation at the top-pointing location.
– Popping is deleting operation from the top-pointing location.

0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

Stack: 17 23 97 44

top = 4 or count = 4

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Implementation using Array
• The following operations are commonly utilized in the Array
implementation of the stack.

• Other operations such as, top, print and count can be implemented
as well.
• In the array there is no actual delete of the element (even for poping
operation).
Method Inputs Output
Create An array and variable
top
Push The array name, the top variable,
and the element to be pushed.
Pop The array name, the top variable. A data element
isFull The array name, the top variable. True/False
isEmpty The array name, the top variable. True/False

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Implementation using Array
• To create empty Stack
• Create an empty array and set top = 0 or count = 0
• C++: top is -1.

• To add (push) an element, either:


• Increment top/count and store the element in stack[top]
or stack[count],.
• C++: can be store then increase if count is used.

• To remove (pop) an element, either:


• Get the element from stack[top] or stack[count] and
decrement top/count .
• C++: can be decrement then get the element if count is
used.
Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha
Implementation using Array
Creating Pseudo-Code
Algorithm create()
isEmpty
S[1..n]
Algorithm isEmpty(S[1..n],top))
top = 0
if(top ==0)
end
return True
Push else
Algorithm Push(S[1..n],top, value x ) return false
top = top +1 end
S[top] = x
isEmpty
end
Algorithm isFull(S[1..n],top))
Pop if(top ==n)
Algorithm Pop(S[1..n],top) return True
x = S[top] else
top = top -1 return false
end end

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Calculator Implementation using Stack
The high level implementation of a calculator using stack is given as:
Algorithm calculate(token)
IF (token is a number)
push (token)
endIF
IF (token is an OP)
second = pop()
first = pop()
result = first OP second
push (result)
endIF
IF(token == “=“)
retrieve top element.
endIF
end

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha


Questions
• To be added …..

Data Structures and Algorithms Dr. Ahmad Adel Abu-Shareha

You might also like