You are on page 1of 17

Amity School of Engineering and Technology

Data Structures using C


(CSIT 124)
Module-2
Stack and Queues

1
Amity School of Engineering and Technology

Contents :​
Stack
Definition
Stack Representation
Array Representation
Linked List Representation
 Operations on Stacks- Push & Pop

2
Amity School of Engineering and Technology

Learning Objectives​
• Impart in-depth knowledge of data structure and its implementation
in computer programs.
• Make students understand the concepts of Stack linear data structure.
• Make students understand the applications of Stack.

3
Amity School of Engineering and Technology

Recommended Reading​
Textbooks: ​
• Yashwant Kanetkar,”Data Structure using C”, BPB Publication, 5th Edition ,2011
• A.Tannenbaum,Y. Lanhgsam and A.J. Augenstein ,” Data Structures Using C And C++ “,Prentice Hall of
India,2nd Edition,2009.
• Jean-Paul Tremblay, P.G Sorenson, “An Introduction to Data Structures with applications”, Mcgraw-
Hill ,2nd Edition ,1984.
​Reference Book: ​
• Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
• Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
• Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2nd Edition AddisonWesley,1996.
• E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition, 2008.
• R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4th Edition,2009
• E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2nd Edition, Universities Press,2008.
 ​

4
Amity School of Engineering and Technology

Module Assessment​
• Quiz (Conceptual and Numerical Based)

• Assignment

5
Amity School of Engineering and Technology

STACK
Definition: Stack is a linear data
structure that principally works on
the Last-in First-out (LIFO). The
element that is inserted first will be
out at last and vice-versa

Examples of Stack
6
Amity School of Engineering and Technology

STACK TERMINOLOGIES
1. Top of Stack: TOP is the pointer
that shows the position of the
topmost element of the stack.
2. MaxStack: MaxStack shows the
maximum size of the stack.
(“How many elements?”)
3. Stack Operations:
PUSH()
POP()

7
Amity School of Engineering and Technology

STACK: ARRAY REPRESENTATION

8
Amity School of Engineering and Technology

ARRAY REPRESENTATION

9
Amity School of Engineering and Technology

STACK: LIST REPRESENTATION

10
Amity School of Engineering and Technology

STACK OPERATIONS
There are two operations of stack:
1) PUSH Operation
2) POP Operation

11
Amity School of Engineering and Technology
PUSH: PUSH operation is used to push (Insert) an element onto the stack

Step 1: If TOP >= MAXSIZE – 1, then:


PRINT: “Stack Overflow”
GOTO Step 4
[end of if structure]
Step 2: TOP = TOP + 1
Step 3: STACK [TOP] = ITEM
Step 4: EXIT

12
Amity School of Engineering and Technology

POP : POP operation is used to pop (delete) an element from the stack

Step 1: If TOP = -1, then:

PRINT: “Stack Underflow”

Goto Step 4

[End of if structure]

Step 2: Temp=STACK [TOP]

Step 3: TOP = TOP – 1

Step 4: EXIT
13
Amity School of Engineering and Technology

STACK OPERATIONS: EXAMPLE


Let as assume a Stack of maximum size=5
TOP
50
TOP
40 40
TOP
30 30 30
TOP
20 20 20 20
TOP
10 10 10 10 10

TOP=-1 TOP=0 TOP=1 TOP=2 TOP=3 TOP=4


MaxSize=5
When TOP= MaxSize-1; then stack becomes full and
PUSH
OPERATION insertion of any new element will result in
“OVERFLOW”. 14
Amity School of Engineering and Technology

STACK OPERATIONS: EXAMPLE


Let as assume a Stack of maximum size=5
50 TOP
TOP
40 40
TOP
30 30 30
TOP
20 20 20 20
TOP
10 10 10 10 10

TOP=4 TOP=3 TOP=2 TOP=1 TOP=1 TOP=-1

When TOP= -1; then stack becomes empty and


POP
OPERATION deletion of any element will result in
“UNDERFLOW”. 15
Amity School of Engineering and Technology

Time Complexity
Push() O(1)

Pop() O(1)

Display() O(n)

16
Amity School of Engineering and Technology

17

You might also like