0% found this document useful (0 votes)
11 views51 pages

Fds U 3 Stack 1 1

Uploaded by

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

Fds U 3 Stack 1 1

Uploaded by

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

What is a stack?

 Stores a set of elements in a particular order


 Stack principle: LAST IN FIRST OUT
 = LIFO
 It means: the last element inserted is the first one to be removed
 Example

 Which is the first element to pick up?


Data Structures: A Pseudocode Approach with C 2
3-1 Basic Stack Operations

The stack concept is introduced and three basic stack


operations are discussed.

• Push
• Pop
• Stack Top

Data Structures: A Pseudocode Approach with C 3


Last In First Out

E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Primitive Operations on
Stack
 Push
 the operation to place a new item at the
top of the stack
 Pop
 the operation to remove the next item
from the top of the stack
Primitive Operations on
Stack
 Empty()
 To determine if a stack is empty or not.
 Full
 To determine if a stack is full or not.
Data Structures: A Pseudocode Approach with C 7
Data Structures: A Pseudocode Approach with C 8
Data Structures: A Pseudocode Approach with C 9
Push
void push(int item)
{
if (top !=SIZE-1)
{
top++; //top incremented by 1
stack[top] = item; //Insert the element from stack
}
Pop
int pop( int top)
{

if (top == -1)
printf(“\nstack is empty”);
else
{

top--;
item=stack[top];
}//end else
return(item);
}
Full()
int full(int stack[10],int top)
{
if(top==MAX-1)
{
return(1);
}
return 0;
}
Empty()
int empty(int stack[10],int top)
{
if(top==-1)
{
return(1);
}
return 0;
}
Initialize()
Void initialize(int stack[10],int top)
{
top=-1;
}
Implementing a Stack
 At least three different ways to
implement a stack
 array
 vector
 linked list
 Which method to use depends on the
application
 what advantages and disadvantages
does each implementation have?
Implementing Stacks: Array
 Advantages

best performance
 Disadvantage

fixed size
 Basic implementation

initially empty array

field to record where the next data gets placed
into

if array is full, push() returns false

otherwise adds it into the correct spot

if array is empty, pop() returns null

otherwise removes the next item in the stack
Stack Applications
 Real life
 Pile of books
 Plate trays
 More applications related to
computer science
 Program execution stack
Expression evaluation-infix to
postfix,infix to prefix
 Parsing

 Simulation of Recursion

 Function call
Data Structures: A Pseudocode Approach with C 18
Representation of two stacks in an array

Stack1 Stack 2

Top1 Top2

Data Structures: A Pseudocode Approach with C 19


Data Structures: A Pseudocode Approach with C 20
Data Structures: A Pseudocode Approach with C 21
Data Structures: A Pseudocode Approach with C 22
Data Structures: A Pseudocode Approach with C 23
Data Structures: A Pseudocode Approach with C 24
Data Structures: A Pseudocode Approach with C 25
Data Structures: A Pseudocode Approach with C 26
Data Structures: A Pseudocode Approach with C 27
Data Structures: A Pseudocode Approach with C 28
Data Structures: A Pseudocode Approach with C 29
Data Structures: A Pseudocode Approach with C 30
Data Structures: A Pseudocode Approach with C 31
Data Structures: A Pseudocode Approach with C 32
Data Structures: A Pseudocode Approach with C 33
3-3 C Language Implementations

This section presents a simple non-ADT implementation


of a stack. We develop a simple program that inserts
random characters into the stack and then prints them.

Data Structures: A Pseudocode Approach with C 34


Data Structures: A Pseudocode Approach with C 35
Data Structures: A Pseudocode Approach with C 36
Data Structures: A Pseudocode Approach with C 37
Data Structures: A Pseudocode Approach with C 38
Data Structures: A Pseudocode Approach with C 39
Data Structures: A Pseudocode Approach with C 40
3-4 Stack ADT

We begin the discussion of the stack ADT with a


discussion of the stack structure and its application
interface. We then develop the required functions.

• Data Structure
• ADT Implemenation

Data Structures: A Pseudocode Approach with C 41


Data Structures: A Pseudocode Approach with C 42
Data Structures: A Pseudocode Approach with C 43
Data Structures: A Pseudocode Approach with C 44
Data Structures: A Pseudocode Approach with C 45
Data Structures: A Pseudocode Approach with C 46
Data Structures: A Pseudocode Approach with C 47
Data Structures: A Pseudocode Approach with C 48
Data Structures: A Pseudocode Approach with C 49
Data Structures: A Pseudocode Approach with C 50
Data Structures: A Pseudocode Approach with C 51

You might also like