Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Stacks: LIFO Data Structure and Its Implementation and Applications, Slides of Data Structures and Algorithms

An introduction to stacks, a Last-In-First-Out (LIFO) linear data structure. the concept of a stack, its examples, stack implementation using static and dynamic methods, and basic operations on stacks. Additionally, the document discusses the application of stacks in recursion and the Tower of Hanoi problem. Polish notation is also briefly mentioned.

Typology: Slides

2020/2021

Uploaded on 11/26/2021

shruti-chavan-1
shruti-chavan-1 🇮🇳

1 document

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction
Of
Stack
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Understanding Stacks: LIFO Data Structure and Its Implementation and Applications and more Slides Data Structures and Algorithms in PDF only on Docsity!

Introduction

Of

Stack

Introduction

■ A stack is a non-primitive linear data structure.

■ It is an ordered list in which addition of new data item and deletion of already existing data items done from only one end, known as top tacks (TOP).

■ As all the deletion and insertion in a stack is done from top of the stack, the last added element will be the first to be removed from the stack.

■ That is the reason why stack is also called LAST-IN-FIRST-OUT (LIFO) type of list.

Introduction

■ Whenever a stack is created the stack base remains fixed, as a new element is added to the stack from the top, the top goes on increasing. ■ Conversely as the top most elements of the stack is removed the stack top is decrementing. ■ Show the next slide figure to various stages of stack top during insertion and during deletion.

Introduction

■ Stack top increases during insertion

Stack empty top=-

4 3 2 1 (^0 ) Insert first element

Top=

4 3 2 1 0

20 10 Top=

Insert second element

0

1

2

3

4

Stack Implementation

■ Stack can be implemented in two ways: ■ Static implementation ■ Dynamic implementation ■ Static implementation uses arrays to create stack. ■ Static implementation though a very simple technique but is not a flexible way of creation, as the size of stack has to be declared during program design, after that the size cannot be varied. ■ Moreover static implementation is not too efficient with respect to memory utilization.

Stack Implementation

■ As the declaration of array is done before the start of the operation, now if there are too few elements to be stored in the stack the statically allocated memory will be wasted. ■ On the other hand if there are more number of elements to be stored in the stack then we can’t be able to change the size of array to increase its capacity.

Operations on Stack

■ The basic operation that can be performed on stack are as follows: ■ PUSH : The process of adding a new element to the top of stack is called PUSH operation. when new element will be inserted at the top after every push operation that top is incremented by one. In case the array is full and no new element can be accommodated, it is called STACK-FULL condition. This condition is called STACK OVERFLOW.

Operations on Stack

■ POP: The process of deleting an element from the top of stack is called POP operation. After every pop operation the stack is decremented by one. If there is no element on the stack and the pop is performed then this will result into STACK UNDERFLOW condition.

■ PEEP: If one is interested only about an information stored at some location in a stack then peep operation is required. In short we can say extract any position information from the stack.

■ UPDATE : Update operation is required when the content of some location in a stack is to be changed.

Stack Terminology

■ STACK UNDERFLOW: This is the situation when the stack contains no element. At this point the top of stack is present at the bottom of the stack. ■ STACK OVERFLOW: This is the situation when the stack becomes full, and no more elements can be pushed onto the stack. At this point the stack top is present at the highest location of the stack.

Application of Stacks

■ There are various application of Stack: ■ Recursion: Recursion is an important facility in many programming language, such as PASCAL and C ect. ■ Some machine are also known which use built-in stack hardware called ‘stack machine’. ■ Representation of Polish Notation

Recursion:

■ There are three popular implementation of recursive computations: ■ Calculation of factorial value ■ Quick sort ■ Tower of Hanoi problem ■ Very simple example is to find Factorial value for an integer n: n!=n(n-1)(n-2)……..32 n!=n*(n-1)!

Recursion:

■ What is the simple implementation of

factorial calculation:

1. fact=

2. For(i=1 to N) do

3. Fact=i*fact

4. End for

5. Return(fact)

6. Stop

Tower of Hanoi Problem

■ Another complex recursive problem is the tower of Hanoi problem.

■ This problem has a historical basis in the ritual of ancient Vietnam.

■ The problem can be described as below:

■ Suppose, there are three pillars A,B,C. there are N discs of decreasing size so that no two discs are of the same size.

■ Initially all the discs are stacked on one pillar in their decreasing order of size.

Tower of Hanoi Problem

■ Let this pillar be A. other two pillars are empty. ■ The problem is to move all the discs from one pillar to other using as auxiliary so that ■ Only one disc may be moved at a time. ■ A disc may be moved from any pillar to another. ■ At no time can a larger disc be placed on a smaller disc.