0% found this document useful (0 votes)
5 views10 pages

Stack 1

The document provides an overview of stacks, a LIFO data structure where elements are added and removed from the top. It discusses basic operations such as PUSH and POP, along with array and linked representations of stacks. Additionally, it addresses minimizing overflow through time-space trade-offs in stack management.

Uploaded by

Muhammad Wasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Stack 1

The document provides an overview of stacks, a LIFO data structure where elements are added and removed from the top. It discusses basic operations such as PUSH and POP, along with array and linked representations of stacks. Additionally, it addresses minimizing overflow through time-space trade-offs in stack management.

Uploaded by

Muhammad Wasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Structures

Lecture 16: Stacks

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Basic Operations
• Array Representation of Stacks
• Minimizing Overflow
• Linked Representation of Stacks
• Review Questions
Introduction
Stacks:
• A stack is a list of elements in which an element may
be inserted or deleted only at one end, called the TOP
of the Stack.

• Stack is a LIFO (Last In First Out) data structure.

• Elements are removed from a stack in the reverse order


of that in which they were inserted into the stack.
Examples
• Stack of Dishes
• Stack of Books
• A packet of Biscuits etc.

• Note: AVAIL List is also implemented using STACK.


STACK
PUSH
Anuj

TOP = 1
TOP = 0
Array Representation of Stacks

STACK

Rahul Anil Deepak Neha

1 2 3 4 5 6 7 8

4 8

TOP MAXSTK
Array Representation of Stacks
PUSH (STACK, TOP, MAXSTK, ITEM)

1. [Stack already filled?]


If TOP = MAXSTK, then: Print: OVERFLOW and Return.

2. Set TOP = TOP +1. [Increase TOP by 1.]

3. Set STACK [TOP] = ITEM. [Insert ITEM into the TOP.]

4. Return
Array Representation of Stacks
POP (STACK, TOP, ITEM)

1. [Stack has an ITEM to be removed?]


If TOP = 0, then: Print: UNDERFLOW and Return.

2. Set ITEM = STACK [TOP]. [ITEM is the TOP element.]

3. Set TOP = TOP - 1. [Decrease TOP. by 1.]

4. Return
Minimizing Overflow
• Stack involves a Time-space Trade-off.

• By reserving a great deal of space for stack, we can


minimize number of overflows.

You might also like