Download presentation
1
Data Structures & Algorithms
CHAPTER 6 Stacks Ms. Manal Al-Asmari
2
Stacks and Queues Define two new abstract data types
*Both are restricted lists *Can be implemented using arrays or linked lists Stacks “Last In First Out” (LIFO) The last element inserted will be the first to be retrieved Queues “First In First Out” (FIFO) Ms. Manal Al-Asmari
3
Stack Overview Stack ADT Basic operations of stack
*Pushing, popping etc. Implementations of stacks using *array *linked list Ms. Manal Al-Asmari
4
Stack ADT A stack is a list of the same kind of elements with the restriction that insertions and deletions can only be performed at the top of the list. The other end is called bottom Ms. Manal Al-Asmari
5
The Stack ADT Stack applications can be classified into four broad categories: reversing data, pairing data, postponing data usage backtracking steps Ms. Manal Al-Asmari
6
Conceptual Stacks Ms. Manal Al-Asmari
7
Stacks Stacks are also called Last Input First Output (LIFO) data structures. Operations performed on stacks: Push: Equivalent to insert Pop: Deletes the most recently inserted element Peek: Examines the most recently inserted element Ms. Manal Al-Asmari
8
Stack Operations Ms. Manal Al-Asmari
9
Push and Pop Primary operations: Push and Pop Push
Add an element to the top of the stack B. Pop Remove the element at the top of the stack Ms. Manal Al-Asmari
10
Stacks implementation
Any list implementation can be used to implement a stack Arrays (static: the size of stack is given initially) Linked lists (dynamic: never become full) Ms. Manal Al-Asmari
11
Implementation of Stacks as Arrays
The array implementing a stack is an array of reference variables Each element of the stack can be assigned to an array slot The top of the stack is the index of the last element added to the stack To keep track of the top position, declare a variable called stackTop Ms. Manal Al-Asmari
12
Implementation of Stacks as Arrays
Ms. Manal Al-Asmari
13
Constructors Default constructor Ms. Manal Al-Asmari
14
Initialize Stack Method initializeStack: Ms. Manal Al-Asmari
15
Empty Stack Method isEmptyStack: Ms. Manal Al-Asmari
16
Full Stack Method isFullStack: Ms. Manal Al-Asmari
17
Push Method push: Ms. Manal Al-Asmari
18
Peek Method peek: Ms. Manal Al-Asmari
19
Pop Method pop: Ms. Manal Al-Asmari
20
THE END Ms. Manal Al-Asmari
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.