SlideShare a Scribd company logo
Page 1 of 4

STACK ( Array & Linked Implementation)
STACK : In computer science, a stack is an abstract data type and data structure based on the principle of Last
In First Out (LIFO). Stacks are used extensively at every level of a modern computer system. For example, a
modern PC uses stacks at the architecture level, which are used in the basic design of an operating system for
interrupt handling and operating system function calls. Among other uses, stacks are used to run a Java Virtual
Machine, and the Java language itself has a class called "Stack", which can be used by the programmer. The
stack is ubiquitous.
A stack-based computer system is one that stores temporary information primarily in stacks, rather than
hardware CPU registers (a register-based computer system).
A Stack ias a LIFO structure and physically it can be implemented as an array called static data structure or as
a linked list called dynamic data structure. A stack implemented as an array inherits all the properties of an
array and if implemented as a linked list , all characteristics of a linked list are possessed by it. But whatever
way a stack may be implemented , insertions and deletions occur at the top only. An insertion in a stack is
called pushing and a deletion from a stack is called popping.
Very short answer type questions.
1. What is the full form of LIFO ? What is a LIFO list technically called?
Ans:- Last In First Out . Technically it is called STACK.
2. What is POP & PUSH operation in STACK?
Ans: - POP operation means Deletion of an element from the Stack and PUSH means Insert an element to
the Stack.
3. What is the situation called when an insertion and deletion takes place in a full list and from a blank list
respectively?
Ans:- Overflow and Underflow.
4. Explain INFIX, POSTFIX, PREFIX notations.
Ans:- A + B  INFIX
AB+  POSTFIX
+AB  PREFIX
5. When elements os stack are joined using pointers, how is the stack termed as?
Ans:- Linked Stack
6. Suppose STACK is allocated 6 memory locations and initially STACK is empty ( TOP = 0). Give the
output of the program segment:
AAA = 2; BBB = 5 ;
PUSH(STACK,AAA);
PUSH( STACK, 4 );
PUSH(STACK, BBB+ 2 );
PUSH ( STACK, AAA + BBB );
While TOP > 0
{ POP ( STACK, ITEM ); TOP - - ;
cout< item <”n”;
}
Long Answer Type Questions:7. Write a function in C++ to perform a PUSH and POP operation in a STACK as an array.
Ans:- void PUSH ( int Stack [], int &Top, int Val )
{ if ( Top = = size -1 )
{
cout << “ Stack Overflow ” ;
exit(0) ; }
else
{
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 2 of 4
Top + + ;
Stack [ Top ] = Val ;
}
}
void POP ( int Stack [ ] , int & Top )
{ int R ;
if ( Top = = -1 )
{
cout<< “ Stack Underflow” ;
exit(0) ; }
else
{ R = Stack [ Top ] ;
Top - - ;
}
}
8. Complete the class with all function definition.
class Stack
{
int Data [ 10] ;
int Top ;
public:
Stack ( ) { Top = -1; }
void PUSH ( ) ; // to push an element into the stack .
void POP ( ) ;
// to pop an element from the stack.
};
Ans:- void Stack :: PUSH ( )
{
int Val ;
cout <<”Enter the value to be push.” ;
cin>> Val ;
if ( Top = = 9 )
{
cout << “ Stack Overflow”;
exit ( 0) ; }
else
{
Top + + ;
Data [ Top ] = Val ;
}
}
void Stack :: POP( )
{
int R ;
if ( Top = = -1 )
{ cout<<”Stack Underflow”;
exit (0) ;
}
else
{
R = Data [Top ] ;
Top - - ;
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 3 of 4
}
}
9. Define functions Stackpush( ) to insert nodes and Stackpop ( ) to delete nodes for a linked implemented
stack having the following structure for each node.
struct Node
{ char name [ 20 ] ; int age ;
Node * Link ;
};
class Stack
{ Node * Top ;
public :
Stack ( ) { Top = NULL ; }
void Stackpush ( ) ;
void Stackpop ( ) ;
};
void Stack :: Stackpush ( )
{ Node * Ptr ;
Ptr = new Node ;
cin >>Ptr  name ; cin >> Ptr  age ;
Ptr  Link = NULL ;
if ( Top = = NULL )
Top = Ptr ;
else
{ Ptr  Link = Top ; Top = Ptr ; }
}
void Stack :: Stackpop ( )
{ Node * Ptr ;
if ( Top = = NULL )
cout <<”Stack Underflow” ;
else
{ Ptr = Top ; Top = Top  Link ; delete Ptr ; }
}
10. Write a function in C++ to perform the PUSH and POP operation on a dynamically allocated stack
containing real numbers. ( Hint : Same as Question No. 9 )
11. Describe the similarities and differences between queues and stacks.
Ans: - Similarities :
i) Both queues and stacks are special cases of linear lists.
ii) Both can be implemented as arrays or linked lists.
Differences :
i)A Stack is a LIFO list, a Queue is a FIFO list.
ii) There are no variations of stack, a queue, however, may be circular or dequeue.
12. What are the advantages of using postfix notation over infix notation?
Ans:- An infix expression is evaluated keeping in mind the precedence of operators. An infix expression is
difficult for the machine to know and keep track of precedence of operators. A postfix expression itself takes
care of the precedence of operators as the placement of operators. Thus, for the machine , it is easier to carry out
a postfix expression than an infix expression.
13. Given the following class:
char *msg [ ] = {“overflow”, “underflow” } ;
class Stack
{ int top , stk [ 5 ] ;
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 4 of 4
void err_rep ( int e_num ) { cout<< msg [e_num] ; }
public:
void init ( ) { top = 0 ; }
void push ( int) ;
void pop ( ) ;
};
Define push and pop outside the stack. In the definition take care of overflow condition in push and
underflow condition in pop.
Ans:- void Stack :: push ( int a )
{ if ( top > 4 ) err_rep (0) ; else stk [ top + + ] = a ; }
void Stack :: pop ( )
{ if top = = 0 ) err_rep ( 1 ) ; else { cout<<”top element is “<<stk [top] ; top - - } }
14. Translate , following infix expression into its equivalent postfix expression and show the stack status :
i) ((A – B ) * ( D / E )) / ( F * G * H )
Ans: AB - DE / * FG * H * /
ii) ( A + B ^ D ) / ( E – F ) + G
Ans: ABD ^ + E F - / G +
iii) A * ( B + D ) / E – F – ( G + H / K )
Ans: A B D + * E / F – G H K / + iv) A * ( B + ( C + D ) * ( E + F ) / G ) * H
Ans: A B C D + E F + * G / + * H *
v) A + ( ( B + C ) + ( D + E ) * F ) / G
Ans: A B C + D E + F * + G / +
vi) NOT A OR NOT B AND NOT C
Ans: A NOT B NOT C NOT AND OR
vii) NOT ( A OR B ) AND C
Ans: A B OR NOT C AND
15. Write the equivalent infix expression for the following postfix expression.
i) 10 , 3 , * , 7 , 1 , - , * , 23 , +
Ans: 10 * 3 * ( 7 – 1 ) + 23
ii) 12, 7, 3, - , / , 2 , 1 , 5 + , * , +
Ans: 12 / ( 7 – 3 ) + ( 1 + 5 ) * 2
16. Evaluate the following infix expression and show the contents of stack after each operation.
i) 5, 3, + , 2 , * , 6 , 9 , 7 , - , / , Ans : 13
ii) 3, 5, + , 6 , 4 , - , * , 4 , 1 , - , 2 , ^ , +
Ans : 25
iii) 3, 1, + , 2, ^ 7 , 4, - , 2 , * , + , 5 , Ans : 17
iv) 20, 45, + 20 , 10 , - , 15 , + , *
Ans : 1625

Prepared By Sumit Kumar Gupta, PGT Computer Science

More Related Content

What's hot

Stack
StackStack
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
Senthil Kumar
 
ROP
ROPROP
Macroprocessor
MacroprocessorMacroprocessor
Macroprocessor
ksanthosh
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
Piotr Paradziński
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
mua99
 
Lecture6
Lecture6Lecture6
Lecture6
Muhammad Zubair
 
BBS crawler for Taiwan
BBS crawler for TaiwanBBS crawler for Taiwan
BBS crawler for Taiwan
Buganini Chiu
 
Circular queues
Circular queuesCircular queues
Circular queues
Ssankett Negi
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
sahil kumar
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
malasumathi
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorch
Hyungjoo Cho
 
Stack queue
Stack queueStack queue
Stack queue
小均 張
 
Polish
PolishPolish
Polish
joie rocker
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
JeeSa Sultana
 
Data structures
Data structures Data structures
Data structures
Rokonuzzaman Rony
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
A. S. M. Shafi
 
SPADE -
SPADE - SPADE -
SPADE -
Monica Dagadita
 

What's hot (20)

Stack
StackStack
Stack
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
ROP
ROPROP
ROP
 
Macroprocessor
MacroprocessorMacroprocessor
Macroprocessor
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Lecture6
Lecture6Lecture6
Lecture6
 
BBS crawler for Taiwan
BBS crawler for TaiwanBBS crawler for Taiwan
BBS crawler for Taiwan
 
Circular queues
Circular queuesCircular queues
Circular queues
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorch
 
Stack queue
Stack queueStack queue
Stack queue
 
Polish
PolishPolish
Polish
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Data structures
Data structures Data structures
Data structures
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
SPADE -
SPADE - SPADE -
SPADE -
 

Similar to Stack

Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
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 and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
SSE_AndyLi
 
Stack
StackStack
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
Hanif Durad
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
ali rashid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
Dipayan Sarkar
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
BHARATH KUMAR
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
DurgaDeviCbit
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
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
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
mohdjakirfb
 
Stacks
StacksStacks
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
 
Stack
StackStack

Similar to Stack (20)

Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
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 and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
Stack
StackStack
Stack
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.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
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
Stacks
StacksStacks
Stacks
 
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
 
Stack
StackStack
Stack
 

More from Swarup Boro

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++
Swarup Boro
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 students
Swarup Boro
 
Array using recursion
Array using recursionArray using recursion
Array using recursion
Swarup Boro
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++
Swarup Boro
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids
Swarup Boro
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloading
Swarup Boro
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sort
Swarup Boro
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbers
Swarup Boro
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a number
Swarup Boro
 
Canteen management program
Canteen management programCanteen management program
Canteen management program
Swarup Boro
 
C++ program using class
C++ program using classC++ program using class
C++ program using class
Swarup Boro
 
Railway reservation
Railway reservationRailway reservation
Railway reservation
Swarup Boro
 
Boolean
BooleanBoolean
Boolean
Swarup Boro
 
Classes
ClassesClasses
Classes
Swarup Boro
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Swarup Boro
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Pointers
PointersPointers
Pointers
Swarup Boro
 
Queue
QueueQueue
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 

More from Swarup Boro (20)

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 students
 
Array using recursion
Array using recursionArray using recursion
Array using recursion
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloading
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sort
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbers
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a number
 
Canteen management program
Canteen management programCanteen management program
Canteen management program
 
C++ program using class
C++ program using classC++ program using class
C++ program using class
 
Railway reservation
Railway reservationRailway reservation
Railway reservation
 
Boolean
BooleanBoolean
Boolean
 
Classes
ClassesClasses
Classes
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Pointers
PointersPointers
Pointers
 
Queue
QueueQueue
Queue
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Functions
FunctionsFunctions
Functions
 

Recently uploaded

Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 

Recently uploaded (20)

Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 

Stack

  • 1. Page 1 of 4 STACK ( Array & Linked Implementation) STACK : In computer science, a stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used extensively at every level of a modern computer system. For example, a modern PC uses stacks at the architecture level, which are used in the basic design of an operating system for interrupt handling and operating system function calls. Among other uses, stacks are used to run a Java Virtual Machine, and the Java language itself has a class called "Stack", which can be used by the programmer. The stack is ubiquitous. A stack-based computer system is one that stores temporary information primarily in stacks, rather than hardware CPU registers (a register-based computer system). A Stack ias a LIFO structure and physically it can be implemented as an array called static data structure or as a linked list called dynamic data structure. A stack implemented as an array inherits all the properties of an array and if implemented as a linked list , all characteristics of a linked list are possessed by it. But whatever way a stack may be implemented , insertions and deletions occur at the top only. An insertion in a stack is called pushing and a deletion from a stack is called popping. Very short answer type questions. 1. What is the full form of LIFO ? What is a LIFO list technically called? Ans:- Last In First Out . Technically it is called STACK. 2. What is POP & PUSH operation in STACK? Ans: - POP operation means Deletion of an element from the Stack and PUSH means Insert an element to the Stack. 3. What is the situation called when an insertion and deletion takes place in a full list and from a blank list respectively? Ans:- Overflow and Underflow. 4. Explain INFIX, POSTFIX, PREFIX notations. Ans:- A + B  INFIX AB+  POSTFIX +AB  PREFIX 5. When elements os stack are joined using pointers, how is the stack termed as? Ans:- Linked Stack 6. Suppose STACK is allocated 6 memory locations and initially STACK is empty ( TOP = 0). Give the output of the program segment: AAA = 2; BBB = 5 ; PUSH(STACK,AAA); PUSH( STACK, 4 ); PUSH(STACK, BBB+ 2 ); PUSH ( STACK, AAA + BBB ); While TOP > 0 { POP ( STACK, ITEM ); TOP - - ; cout< item <”n”; } Long Answer Type Questions:7. Write a function in C++ to perform a PUSH and POP operation in a STACK as an array. Ans:- void PUSH ( int Stack [], int &Top, int Val ) { if ( Top = = size -1 ) { cout << “ Stack Overflow ” ; exit(0) ; } else { Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 4 Top + + ; Stack [ Top ] = Val ; } } void POP ( int Stack [ ] , int & Top ) { int R ; if ( Top = = -1 ) { cout<< “ Stack Underflow” ; exit(0) ; } else { R = Stack [ Top ] ; Top - - ; } } 8. Complete the class with all function definition. class Stack { int Data [ 10] ; int Top ; public: Stack ( ) { Top = -1; } void PUSH ( ) ; // to push an element into the stack . void POP ( ) ; // to pop an element from the stack. }; Ans:- void Stack :: PUSH ( ) { int Val ; cout <<”Enter the value to be push.” ; cin>> Val ; if ( Top = = 9 ) { cout << “ Stack Overflow”; exit ( 0) ; } else { Top + + ; Data [ Top ] = Val ; } } void Stack :: POP( ) { int R ; if ( Top = = -1 ) { cout<<”Stack Underflow”; exit (0) ; } else { R = Data [Top ] ; Top - - ; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 4 } } 9. Define functions Stackpush( ) to insert nodes and Stackpop ( ) to delete nodes for a linked implemented stack having the following structure for each node. struct Node { char name [ 20 ] ; int age ; Node * Link ; }; class Stack { Node * Top ; public : Stack ( ) { Top = NULL ; } void Stackpush ( ) ; void Stackpop ( ) ; }; void Stack :: Stackpush ( ) { Node * Ptr ; Ptr = new Node ; cin >>Ptr  name ; cin >> Ptr  age ; Ptr  Link = NULL ; if ( Top = = NULL ) Top = Ptr ; else { Ptr  Link = Top ; Top = Ptr ; } } void Stack :: Stackpop ( ) { Node * Ptr ; if ( Top = = NULL ) cout <<”Stack Underflow” ; else { Ptr = Top ; Top = Top  Link ; delete Ptr ; } } 10. Write a function in C++ to perform the PUSH and POP operation on a dynamically allocated stack containing real numbers. ( Hint : Same as Question No. 9 ) 11. Describe the similarities and differences between queues and stacks. Ans: - Similarities : i) Both queues and stacks are special cases of linear lists. ii) Both can be implemented as arrays or linked lists. Differences : i)A Stack is a LIFO list, a Queue is a FIFO list. ii) There are no variations of stack, a queue, however, may be circular or dequeue. 12. What are the advantages of using postfix notation over infix notation? Ans:- An infix expression is evaluated keeping in mind the precedence of operators. An infix expression is difficult for the machine to know and keep track of precedence of operators. A postfix expression itself takes care of the precedence of operators as the placement of operators. Thus, for the machine , it is easier to carry out a postfix expression than an infix expression. 13. Given the following class: char *msg [ ] = {“overflow”, “underflow” } ; class Stack { int top , stk [ 5 ] ; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 4 void err_rep ( int e_num ) { cout<< msg [e_num] ; } public: void init ( ) { top = 0 ; } void push ( int) ; void pop ( ) ; }; Define push and pop outside the stack. In the definition take care of overflow condition in push and underflow condition in pop. Ans:- void Stack :: push ( int a ) { if ( top > 4 ) err_rep (0) ; else stk [ top + + ] = a ; } void Stack :: pop ( ) { if top = = 0 ) err_rep ( 1 ) ; else { cout<<”top element is “<<stk [top] ; top - - } } 14. Translate , following infix expression into its equivalent postfix expression and show the stack status : i) ((A – B ) * ( D / E )) / ( F * G * H ) Ans: AB - DE / * FG * H * / ii) ( A + B ^ D ) / ( E – F ) + G Ans: ABD ^ + E F - / G + iii) A * ( B + D ) / E – F – ( G + H / K ) Ans: A B D + * E / F – G H K / + iv) A * ( B + ( C + D ) * ( E + F ) / G ) * H Ans: A B C D + E F + * G / + * H * v) A + ( ( B + C ) + ( D + E ) * F ) / G Ans: A B C + D E + F * + G / + vi) NOT A OR NOT B AND NOT C Ans: A NOT B NOT C NOT AND OR vii) NOT ( A OR B ) AND C Ans: A B OR NOT C AND 15. Write the equivalent infix expression for the following postfix expression. i) 10 , 3 , * , 7 , 1 , - , * , 23 , + Ans: 10 * 3 * ( 7 – 1 ) + 23 ii) 12, 7, 3, - , / , 2 , 1 , 5 + , * , + Ans: 12 / ( 7 – 3 ) + ( 1 + 5 ) * 2 16. Evaluate the following infix expression and show the contents of stack after each operation. i) 5, 3, + , 2 , * , 6 , 9 , 7 , - , / , Ans : 13 ii) 3, 5, + , 6 , 4 , - , * , 4 , 1 , - , 2 , ^ , + Ans : 25 iii) 3, 1, + , 2, ^ 7 , 4, - , 2 , * , + , 5 , Ans : 17 iv) 20, 45, + 20 , 10 , - , 15 , + , * Ans : 1625 Prepared By Sumit Kumar Gupta, PGT Computer Science