SlideShare a Scribd company logo
Stacks
• Objective
After this lecture you will be able to:
• Describe a stack
• Describe the representation of stack using linear
array
• Describe the representation of stack using linear
linked list
• Implementation of various operations on stack
• Describe some applications of stacks
Stack
• Stack is one of the commonly used data
structures.
• Stack is also called last in first out (LIFO)
system
• Stack is a linear list in which insertion and
deletion can take place only at one end
called top.
• This structure operates in much the same
way as stack of trays.
Stack of Trays
•The following figure illustrate a stack, which can
accommodate maximum of 10 elements
figure shows stack after pushing elements 8,10,12,-5,6
6
-5
12
10
8
0
1
2
3
4
5
6
7
8
9
top
Stack after popping top two
elements
12
10
8
0
1
2
3
4
5
6
7
8
9
top
stack after pushing elements 8,10,12,-5,6,9,55
55
9
6
-5
12
10
8
0
1
2
3
4
5
6
7
8
9
top
Operations on stacks
• Createstack(s)—to create s as an empty stack
• Push(s,i)--to push elementi onto stack s.
• Pop(s)—to access and remove the top element
of the stack s
• Peek(s)—to access the top element of the
stacks without removing it from the stack s.
• Isfull(s)—to check whether the stack s is full
• isempty—to check whether the stack s is
empty
Representation of stack in
memory
• Representation of stack using array:
Suppose elements of the stack are integer type and stack can
store maximum 10 elements..
#define MAX 10
typedef struct
{
int top;
int elements[MAX];
}stack;
stack s;
• Here we have defined our own data type named stack.
• First element top will be used to index top element
• Array elements hold the elements of the stack
• Last line declares variable s of type stack
stack
• In addition to the previous declaration, we
will use the declaration
typedef enum {false, true } Boolean;
This statement defined new data type
named Boolean which can take value false
or true.
Representation of stack in memory
8 10 12 -5 6
0 1 2 3 4 5 6 7 8 9
4
top
8 10 12
2
top
8 10 12 -5 6 9 55
6
top
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Creating an empty stack
• Before we can use a stack, it is to be initialized.
• As the index of array elements can take any value in the
range 0 to MAX-1, the purpose of initializing the stack is
served by assigning value -1 to the top of variable.
• This simple task can be accomplished by the following
function.
Void createstack( stack *ps)
{
ps=-1;
}
Testing stack for underflow
Boolean isempty(stack *ps)
{
if(ps->top==-1)
return true;
else
return false;
}
or
Boolean is empty(stack *ps)
{
return ((ps->top==-1)?true:false);
}
Testing stack for overflow
Boolean isfull(stack *ps)
{
if(ps->top==MAX-1)
return true;
else
return false;
}
or
Boolean is empty(stack *ps)
{
return ((ps->top==MAX-1)?true:false);
}
Push Operation
Before the push operation, if the stack is empty, then the
value of the top will be -1and if the stack is not empty
then the value of the top will be the index of the element
currently on the top.
Therefore we place the value onto the stack, the value of
top is incremented so that it points to the new top of
stack, where incoming element is placed.
Void push(stack *ps, int value)
{
ps->top++;
ps->elements[ps->top]=value;
}
Pop Operation
The element on the top of the stack is assigned to a local variable,
which later on will be returned via the return statement.
After assigning the top element to a local variable, the variable top is
decremented so that it points to a new top
Int pop(stack *ps)
{
int temp;
temp=ps->elements[ps->top];
ps->top--;
return temp;
}
Accessing top element
There may be instances where we want to
access the top element of the stack
without removing it from the stack.
Int peek( stack *ps)
{
return(ps->elements[ps->top]);
}
Representing a stack using a linked
list
A stack represented using a linked list is also known as
linked stack.
The array based representation of stack suffers from
following limitations.
• Size of the stack must be known in advance
• We may come across situation when an attempt to push
an element causes overflow.
However stack is an abstract data structure can not be full.
Hence, abstractly, it is always possible to push an
element onto stack. Therefore stack as an array prohibits
the growth of the stack beyond the finite number of
elements.
Declaration of stack
The linked list representation allows a stack to grow to a
limit of the computer’s memory.
Typedef struct nodetype
{
int info;
struct nodetype *next;
}stack;
Stack *top;
Here I have defined my own data type named stack,
which is a self referential structure and whose first
element info hold the element of the stack and the
second element next hold the address of the element
under it in the stack.
The last line declares a pointer variable top of type
stack.
Representation of stack in memory
top
6 -5 12 8 X
10
top
6 -5 12 X
top
55 9 6 8 X
-5 12 10
Creating an empty stack
Before we can use a stack, it is to be initialized.
To initialize a stack, we will create an empty
linked list.
The empty linked list is created by setting pointer
variable top to value NULL.
Void createstack(stack **top)
{
*top=NULL;
}
Testing stack for underflow
Boolean isempty(stack *top)
{
if(top==NULL)
return true;
else
return false;
}
or
Boolean is empty(stack *top)
{
return ((top==NULL)?true:false);
}
Testing stack for overflow
Since stack represented using a linked list
can grow to a limit of computers memory,
there overflow condition never occurs.
Hence this operation is not implemented
for linked list.
Push operation
To push a new element onto the stack, the element is inserted in
the beginning of the linked list.
void push(stack **top, int value)
{
stack *ptr;
ptr=(stack*)malloc(sizeof(stack));
if(ptr==NULL)
{
printf(“n unable to allocate memory for new node…”);
printf(“npress any key to exit..”);
getch();
return;
}
ptr->info=value;
ptr->next=*top;
*top=ptr;
}
Pop operation
To pop an element from the stack, the element is removed
from the beginning of the linked list.
Int pop(stack **top)
{
int temp;
stack *ptr;
temp=(*top)->info;
ptr=*top;
*top=(*top)->next;
free(ptr);
return temp;
}
Accessing top element
Int peek(stack *top)
{
return(top->info)
}
Dispose a stack
Because the stack is implemented using linked lists,
therefore it is programmers job to write the code to
release the memory occupied by the stack.
Void disposestack(stack **top)
{
stack *ptr;
while(*top!=NULL)
{
ptr=*top;
*top=(*top)->next;
free(ptr);
}
}
Applications of Stacks
• Stacks are used to pass parameters between
functions. On a call to function, parameter and local
variables are stored on stack.
• High level programming languages, such as Pascal
c etc. that provide support for recursion use stack
for book keeping. In each recursive call, there is
need to save the current values of parameters, local
variables and the return address.
In addition to above stack are used to solve the various
problems….
1. Parenthesis checker
2. Mathematical notation translation
1. Polish (prefix) notation
2. Reverse polish (postfix) Notation
3. Quick sort algorithm
Parenthesis checker
• Parenthesis checker is a program that checks
whether a mathematical expression is properly
parenthesized.
• We will consider three sets of grouping symbols:
– The standard parenthesis ”( )”
– The braces “{ }”
– the brackets “[ ]”
For an input expression, it verifies that for each left
parenthesis, braces or racket, there is a corresponding
closing symbol and the symbols are appropriately nested.
Examples of valid inputs
Valid Input
( )
( { } [ ])
( { [ ] [ ] } )
[ { { { } ( )} [ ] } [ ] ( ) { } ]
invalid Input
[(( )
( { } [ ]))
(( { [ ] [ ] } )
([ { { { } ( )} [ ] } }[ ] ( ) { } ]
Inside parenthesis there can be any valid arithmetic expression.
Mathematical notation
Translation
Symbol used Operation
performed
Precedence
* (asterisk) Multiplication Highest
/ (slash) Division Highest
% (percentage) Modulus Highest
+ (plus) Addition Lowest
- (hyphen) subtraction lowest
Infix notation
In this notation, the operator symbol is placed
between its two operands.
• To add A to B we can write as A+B or B+A
• To subtract D from C we write as C-D, but we
can not write D-C as this operation is not
commutative.
In this notation we must distinguish between
(A+B)/C and A+(B/C)
Polish (prefix) notation
In this notation, named after the polish
mathematician Jan Lukasiewiez, the operator
symbol is placed before its two operands.
• To add A to B we write as +AB or +BA
• To subtract D from C we have to writ as –CD
not as -DC
Infix to polish notation
In order to translate an arithmetic expression in infix notation to
polish notation, we do step by step using rackets ([]) to indicate
the partial translations.
• Consider the following expression in infix notation:
(A-B/C)*(A*K-L)
The partial translation may look like:
(A-[/BC])*([*AK]-L)
[-A/BC]*[-*AKL]
*-A/BC-*AKL
The fundamental property of polish notation is that the order in
which the operations to perform is completely determined by
the position of the operators and operands in the expression.
Accordingly one never needs parenthesis when writing expression
in polish notation.
Reverse Polish (Postfix) Notation
In this notation the operator symbol is placed
after its two operands.
• To add A to B we can write as AB+ or BA+
• To subtract D from C we have to write as CD-
not as DC-.
Infix to reverse polish notation
Consider the following expression in infix
notation:
(A-B/C)*(A/K-L)
The partial translation may look like:]
(A-[BC/])*([AK/]-L)
[ABC/-]*[AK/L-]
ABC/-AK/L-*
Evaluating Mathematical
Expressions
Generally we use infix notation, where one can not tell the
order in which the operator should be applied by looking
at the expression.
The expression in postfix notation is very easy to evaluate,
as the operands appear before the operator, there is no
need of operator precedence or parentheses for
operation.
In order to evaluate a postfix expression it is scanned from
left to right.
As operands are encountered, they are pushed on a stack.
When an operator encountered, pop top one or two
operands depending on the operator, perform the
operation and place the result back on the stack.
Infix to postfix procedure
Consider the following infix expression q:
(7-5)*(9/2)
Character scanned stack Expression p
( (
7 ( 7
- (- 7
5 (- 7 5
) empty 7 5 -
* * 7 5 -
( *( 7 5 -
9 *( 7 5 – 9
/ *(/ 7 5 – 9
2 *(/ 7 5 – 9 2
) * 7 5 – 9 2 /
End of expression Empty 7 5 – 9 2 / *
Evaluating expression in postfix
notation
Consider the following postfix expression p:
7 5 – 9 2 / *
Character Scanned Stack
7 7
5 7 5
- 2
9 2 9
2 2 9 2
/ 2 4.5
* 9
End of expression 9

More Related Content

Similar to lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt

The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
Ashim Lamichhane
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
04 stacks
04 stacks04 stacks
04 stacks
Rajan Gautam
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
stack.ppt
stack.pptstack.ppt
stack.ppt
ssuserec1395
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
iloveyoucarlo0923
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
MouDhara1
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
Ssankett Negi
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
KALPANAC20
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Stacks
StacksStacks
Stack
StackStack
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
VeerannaKotagi1
 

Similar to lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt (20)

The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
04 stacks
04 stacks04 stacks
04 stacks
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Stacks
StacksStacks
Stacks
 
Stack
StackStack
Stack
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 

Recently uploaded

Lessons from operationalizing integrated landscape approaches
Lessons from operationalizing integrated landscape approachesLessons from operationalizing integrated landscape approaches
Lessons from operationalizing integrated landscape approaches
CIFOR-ICRAF
 
原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样
原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样
原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样
mvrpcz6
 
BASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENT
BASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENTBASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENT
BASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENT
AmitKumar619042
 
一比一原版西澳大学毕业证学历证书如何办理
一比一原版西澳大学毕业证学历证书如何办理一比一原版西澳大学毕业证学历证书如何办理
一比一原版西澳大学毕业证学历证书如何办理
yxfus
 
学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样
学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样
学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样
ehfyqtu
 
快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样
快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样
快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样
astuz
 
Environment Conservation Rules 2023 (ECR)-2023.pptx
Environment Conservation Rules 2023 (ECR)-2023.pptxEnvironment Conservation Rules 2023 (ECR)-2023.pptx
Environment Conservation Rules 2023 (ECR)-2023.pptx
neilsencassidy
 
Kinetic studies on malachite green dye adsorption from aqueous solutions by A...
Kinetic studies on malachite green dye adsorption from aqueous solutions by A...Kinetic studies on malachite green dye adsorption from aqueous solutions by A...
Kinetic studies on malachite green dye adsorption from aqueous solutions by A...
Open Access Research Paper
 
Biomimicry in agriculture: Nature-Inspired Solutions for a Greener Future
Biomimicry in agriculture: Nature-Inspired Solutions for a Greener FutureBiomimicry in agriculture: Nature-Inspired Solutions for a Greener Future
Biomimicry in agriculture: Nature-Inspired Solutions for a Greener Future
Dr. P.B.Dharmasena
 
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
xeexm
 
Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...
Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...
Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...
Joshua Orris
 
在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样
在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样
在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样
pjq9n1lk
 
Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...
Open Access Research Paper
 
world-environment-day-2024-240601103559-14f4c0b4.pptx
world-environment-day-2024-240601103559-14f4c0b4.pptxworld-environment-day-2024-240601103559-14f4c0b4.pptx
world-environment-day-2024-240601103559-14f4c0b4.pptx
mfasna35
 
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Joshua Orris
 
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
p2npnqp
 
PACKAGING OF FROZEN FOODS ( food technology)
PACKAGING OF FROZEN FOODS  ( food technology)PACKAGING OF FROZEN FOODS  ( food technology)
PACKAGING OF FROZEN FOODS ( food technology)
Addu25809
 
REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...
REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...
REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...
pareeksulkash
 

Recently uploaded (18)

Lessons from operationalizing integrated landscape approaches
Lessons from operationalizing integrated landscape approachesLessons from operationalizing integrated landscape approaches
Lessons from operationalizing integrated landscape approaches
 
原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样
原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样
原版制作(Manitoba毕业证书)曼尼托巴大学毕业证学位证一模一样
 
BASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENT
BASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENTBASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENT
BASIC CONCEPT OF ENVIRONMENT AND DIFFERENT CONSTITUTENET OF ENVIRONMENT
 
一比一原版西澳大学毕业证学历证书如何办理
一比一原版西澳大学毕业证学历证书如何办理一比一原版西澳大学毕业证学历证书如何办理
一比一原版西澳大学毕业证学历证书如何办理
 
学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样
学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样
学校原版(unuk学位证书)英国牛津布鲁克斯大学毕业证硕士文凭原版一模一样
 
快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样
快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样
快速办理(Calabria毕业证书)卡拉布里亚大学毕业证在读证明一模一样
 
Environment Conservation Rules 2023 (ECR)-2023.pptx
Environment Conservation Rules 2023 (ECR)-2023.pptxEnvironment Conservation Rules 2023 (ECR)-2023.pptx
Environment Conservation Rules 2023 (ECR)-2023.pptx
 
Kinetic studies on malachite green dye adsorption from aqueous solutions by A...
Kinetic studies on malachite green dye adsorption from aqueous solutions by A...Kinetic studies on malachite green dye adsorption from aqueous solutions by A...
Kinetic studies on malachite green dye adsorption from aqueous solutions by A...
 
Biomimicry in agriculture: Nature-Inspired Solutions for a Greener Future
Biomimicry in agriculture: Nature-Inspired Solutions for a Greener FutureBiomimicry in agriculture: Nature-Inspired Solutions for a Greener Future
Biomimicry in agriculture: Nature-Inspired Solutions for a Greener Future
 
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
 
Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...
Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...
Evolving Lifecycles with High Resolution Site Characterization (HRSC) and 3-D...
 
在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样
在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样
在线办理(lboro毕业证书)拉夫堡大学毕业证学历证书一模一样
 
Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...
 
world-environment-day-2024-240601103559-14f4c0b4.pptx
world-environment-day-2024-240601103559-14f4c0b4.pptxworld-environment-day-2024-240601103559-14f4c0b4.pptx
world-environment-day-2024-240601103559-14f4c0b4.pptx
 
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
 
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
 
PACKAGING OF FROZEN FOODS ( food technology)
PACKAGING OF FROZEN FOODS  ( food technology)PACKAGING OF FROZEN FOODS  ( food technology)
PACKAGING OF FROZEN FOODS ( food technology)
 
REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...
REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...
REPORT-PRESENTATION BY CHIEF SECRETARY, ANDAMAN NICOBAR ADMINISTRATION IN OA ...
 

lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt

  • 1. Stacks • Objective After this lecture you will be able to: • Describe a stack • Describe the representation of stack using linear array • Describe the representation of stack using linear linked list • Implementation of various operations on stack • Describe some applications of stacks
  • 2. Stack • Stack is one of the commonly used data structures. • Stack is also called last in first out (LIFO) system • Stack is a linear list in which insertion and deletion can take place only at one end called top. • This structure operates in much the same way as stack of trays.
  • 4. •The following figure illustrate a stack, which can accommodate maximum of 10 elements figure shows stack after pushing elements 8,10,12,-5,6 6 -5 12 10 8 0 1 2 3 4 5 6 7 8 9 top
  • 5. Stack after popping top two elements 12 10 8 0 1 2 3 4 5 6 7 8 9 top
  • 6. stack after pushing elements 8,10,12,-5,6,9,55 55 9 6 -5 12 10 8 0 1 2 3 4 5 6 7 8 9 top
  • 7. Operations on stacks • Createstack(s)—to create s as an empty stack • Push(s,i)--to push elementi onto stack s. • Pop(s)—to access and remove the top element of the stack s • Peek(s)—to access the top element of the stacks without removing it from the stack s. • Isfull(s)—to check whether the stack s is full • isempty—to check whether the stack s is empty
  • 8. Representation of stack in memory • Representation of stack using array: Suppose elements of the stack are integer type and stack can store maximum 10 elements.. #define MAX 10 typedef struct { int top; int elements[MAX]; }stack; stack s; • Here we have defined our own data type named stack. • First element top will be used to index top element • Array elements hold the elements of the stack • Last line declares variable s of type stack
  • 9. stack • In addition to the previous declaration, we will use the declaration typedef enum {false, true } Boolean; This statement defined new data type named Boolean which can take value false or true.
  • 10. Representation of stack in memory 8 10 12 -5 6 0 1 2 3 4 5 6 7 8 9 4 top 8 10 12 2 top 8 10 12 -5 6 9 55 6 top 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
  • 11. Creating an empty stack • Before we can use a stack, it is to be initialized. • As the index of array elements can take any value in the range 0 to MAX-1, the purpose of initializing the stack is served by assigning value -1 to the top of variable. • This simple task can be accomplished by the following function. Void createstack( stack *ps) { ps=-1; }
  • 12. Testing stack for underflow Boolean isempty(stack *ps) { if(ps->top==-1) return true; else return false; } or Boolean is empty(stack *ps) { return ((ps->top==-1)?true:false); }
  • 13. Testing stack for overflow Boolean isfull(stack *ps) { if(ps->top==MAX-1) return true; else return false; } or Boolean is empty(stack *ps) { return ((ps->top==MAX-1)?true:false); }
  • 14. Push Operation Before the push operation, if the stack is empty, then the value of the top will be -1and if the stack is not empty then the value of the top will be the index of the element currently on the top. Therefore we place the value onto the stack, the value of top is incremented so that it points to the new top of stack, where incoming element is placed. Void push(stack *ps, int value) { ps->top++; ps->elements[ps->top]=value; }
  • 15. Pop Operation The element on the top of the stack is assigned to a local variable, which later on will be returned via the return statement. After assigning the top element to a local variable, the variable top is decremented so that it points to a new top Int pop(stack *ps) { int temp; temp=ps->elements[ps->top]; ps->top--; return temp; }
  • 16. Accessing top element There may be instances where we want to access the top element of the stack without removing it from the stack. Int peek( stack *ps) { return(ps->elements[ps->top]); }
  • 17. Representing a stack using a linked list A stack represented using a linked list is also known as linked stack. The array based representation of stack suffers from following limitations. • Size of the stack must be known in advance • We may come across situation when an attempt to push an element causes overflow. However stack is an abstract data structure can not be full. Hence, abstractly, it is always possible to push an element onto stack. Therefore stack as an array prohibits the growth of the stack beyond the finite number of elements.
  • 18. Declaration of stack The linked list representation allows a stack to grow to a limit of the computer’s memory. Typedef struct nodetype { int info; struct nodetype *next; }stack; Stack *top; Here I have defined my own data type named stack, which is a self referential structure and whose first element info hold the element of the stack and the second element next hold the address of the element under it in the stack. The last line declares a pointer variable top of type stack.
  • 19. Representation of stack in memory top 6 -5 12 8 X 10 top 6 -5 12 X top 55 9 6 8 X -5 12 10
  • 20. Creating an empty stack Before we can use a stack, it is to be initialized. To initialize a stack, we will create an empty linked list. The empty linked list is created by setting pointer variable top to value NULL. Void createstack(stack **top) { *top=NULL; }
  • 21. Testing stack for underflow Boolean isempty(stack *top) { if(top==NULL) return true; else return false; } or Boolean is empty(stack *top) { return ((top==NULL)?true:false); }
  • 22. Testing stack for overflow Since stack represented using a linked list can grow to a limit of computers memory, there overflow condition never occurs. Hence this operation is not implemented for linked list.
  • 23. Push operation To push a new element onto the stack, the element is inserted in the beginning of the linked list. void push(stack **top, int value) { stack *ptr; ptr=(stack*)malloc(sizeof(stack)); if(ptr==NULL) { printf(“n unable to allocate memory for new node…”); printf(“npress any key to exit..”); getch(); return; } ptr->info=value; ptr->next=*top; *top=ptr; }
  • 24. Pop operation To pop an element from the stack, the element is removed from the beginning of the linked list. Int pop(stack **top) { int temp; stack *ptr; temp=(*top)->info; ptr=*top; *top=(*top)->next; free(ptr); return temp; }
  • 25. Accessing top element Int peek(stack *top) { return(top->info) }
  • 26. Dispose a stack Because the stack is implemented using linked lists, therefore it is programmers job to write the code to release the memory occupied by the stack. Void disposestack(stack **top) { stack *ptr; while(*top!=NULL) { ptr=*top; *top=(*top)->next; free(ptr); } }
  • 27. Applications of Stacks • Stacks are used to pass parameters between functions. On a call to function, parameter and local variables are stored on stack. • High level programming languages, such as Pascal c etc. that provide support for recursion use stack for book keeping. In each recursive call, there is need to save the current values of parameters, local variables and the return address. In addition to above stack are used to solve the various problems…. 1. Parenthesis checker 2. Mathematical notation translation 1. Polish (prefix) notation 2. Reverse polish (postfix) Notation 3. Quick sort algorithm
  • 28. Parenthesis checker • Parenthesis checker is a program that checks whether a mathematical expression is properly parenthesized. • We will consider three sets of grouping symbols: – The standard parenthesis ”( )” – The braces “{ }” – the brackets “[ ]” For an input expression, it verifies that for each left parenthesis, braces or racket, there is a corresponding closing symbol and the symbols are appropriately nested.
  • 29. Examples of valid inputs Valid Input ( ) ( { } [ ]) ( { [ ] [ ] } ) [ { { { } ( )} [ ] } [ ] ( ) { } ] invalid Input [(( ) ( { } [ ])) (( { [ ] [ ] } ) ([ { { { } ( )} [ ] } }[ ] ( ) { } ] Inside parenthesis there can be any valid arithmetic expression.
  • 30. Mathematical notation Translation Symbol used Operation performed Precedence * (asterisk) Multiplication Highest / (slash) Division Highest % (percentage) Modulus Highest + (plus) Addition Lowest - (hyphen) subtraction lowest
  • 31. Infix notation In this notation, the operator symbol is placed between its two operands. • To add A to B we can write as A+B or B+A • To subtract D from C we write as C-D, but we can not write D-C as this operation is not commutative. In this notation we must distinguish between (A+B)/C and A+(B/C)
  • 32. Polish (prefix) notation In this notation, named after the polish mathematician Jan Lukasiewiez, the operator symbol is placed before its two operands. • To add A to B we write as +AB or +BA • To subtract D from C we have to writ as –CD not as -DC
  • 33. Infix to polish notation In order to translate an arithmetic expression in infix notation to polish notation, we do step by step using rackets ([]) to indicate the partial translations. • Consider the following expression in infix notation: (A-B/C)*(A*K-L) The partial translation may look like: (A-[/BC])*([*AK]-L) [-A/BC]*[-*AKL] *-A/BC-*AKL The fundamental property of polish notation is that the order in which the operations to perform is completely determined by the position of the operators and operands in the expression. Accordingly one never needs parenthesis when writing expression in polish notation.
  • 34. Reverse Polish (Postfix) Notation In this notation the operator symbol is placed after its two operands. • To add A to B we can write as AB+ or BA+ • To subtract D from C we have to write as CD- not as DC-.
  • 35. Infix to reverse polish notation Consider the following expression in infix notation: (A-B/C)*(A/K-L) The partial translation may look like:] (A-[BC/])*([AK/]-L) [ABC/-]*[AK/L-] ABC/-AK/L-*
  • 36. Evaluating Mathematical Expressions Generally we use infix notation, where one can not tell the order in which the operator should be applied by looking at the expression. The expression in postfix notation is very easy to evaluate, as the operands appear before the operator, there is no need of operator precedence or parentheses for operation. In order to evaluate a postfix expression it is scanned from left to right. As operands are encountered, they are pushed on a stack. When an operator encountered, pop top one or two operands depending on the operator, perform the operation and place the result back on the stack.
  • 37. Infix to postfix procedure Consider the following infix expression q: (7-5)*(9/2) Character scanned stack Expression p ( ( 7 ( 7 - (- 7 5 (- 7 5 ) empty 7 5 - * * 7 5 - ( *( 7 5 - 9 *( 7 5 – 9 / *(/ 7 5 – 9 2 *(/ 7 5 – 9 2 ) * 7 5 – 9 2 / End of expression Empty 7 5 – 9 2 / *
  • 38. Evaluating expression in postfix notation Consider the following postfix expression p: 7 5 – 9 2 / * Character Scanned Stack 7 7 5 7 5 - 2 9 2 9 2 2 9 2 / 2 4.5 * 9 End of expression 9