17
Stack Implementation Mr. Mubashir Ali Lecturer (Dept. of Software Engineering) [email protected] Lahore Garrison University, Lahore 1 Lecture 12

Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

  • Upload
    others

  • View
    6

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Stack Implementation

Mr. Mubashir AliLecturer (Dept. of Software Engineering)

[email protected] Garrison University, Lahore

1

Lecture 12

Page 2: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Previous Lecture

1. Stack

2. Stack Operations

3. Stack Applications

Mubashir Ali - Lecturer (Department of Software Engineering)

2

Page 3: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Outline

1. Array Based Implementation

2. Linked List Based Implementation

3. Stack Member Functions in STL

Mubashir Ali - Lecturer (Department of Software Engineering)

3

Page 4: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Algorithm for PUSH Operation

• Check if the stack is full or not.

• If the stack is full, then print error of overflow andexit the program.

• If the stack is not full, then increment the top andadd the element.

Mubashir Ali - Lecturer (Department of Software Engineering)

4

Page 5: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Algorithm for POP Operation

• Check if the stack is empty or not.

• If the stack is empty, then print error of underflowand exit the program.

• If the stack is not empty, then print the element atthe top and decrement the top.

Mubashir Ali - Lecturer (Department of Software Engineering)

5

Page 6: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Array Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

6

Page 7: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Array Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

7

Page 8: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Array Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

8

Page 9: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Array Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

9

Page 10: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Linked List Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

10

Page 11: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Linked List Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

11

Page 12: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Linked List Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

12

Page 13: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Linked List Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

13

Page 14: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Stack Member Functions in STL

Mubashir Ali - Lecturer (Department of Software Engineering)

14

Page 15: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Lab-6

Write C++ program to perform followingimplementation of Stack with all essentialoperations,

1. Array based implementation

2. Linked List based implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

15

Page 16: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

Summary

1. Array Based Implementation

2. Linked List Based Implementation

3. Stack Member Functions in STL

Mubashir Ali - Lecturer (Department of Software Engineering)

16

Page 17: Lecture 12 Stack Implementation - Learn With …...Lecture 12 Previous Lecture 1. Stack 2. Stack Operations 3. Stack Applications Mubashir Ali - Lecturer (Department of Software Engineering)

References

you will be able to find course resources at

http://www.mubashirali.com/data-structures-algorithms/

Topic 4.1 and 4.4 (Data Structures and Algorithms in C++ byAdam Drozdek)

Mubashir Ali - Lecturer (Department of Software Engineering)

17