SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 
Macroprocessor
MacroprocessorMacroprocessor
Macroprocessorksanthosh
 
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 examplesmua99
 
BBS crawler for Taiwan
BBS crawler for TaiwanBBS crawler for Taiwan
BBS crawler for TaiwanBuganini Chiu
 
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 Stacksahil 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_solmalasumathi
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfixSelf-Employed
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorchHyungjoo Cho
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 

Tendances (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 -
 

Similaire à Stack

Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2SSE_AndyLi
 
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 MeenaDipayan Sarkar
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and QueuesBHARATH KUMAR
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
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 QueueBalwant Gorad
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURESUsha 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.pdfmohdjakirfb
 
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) .pptxhaaamin01
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 

Similaire à 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++
 
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
 
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
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 

Plus de 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 studentsSwarup Boro
 
Array using recursion
Array using recursionArray using recursion
Array using recursionSwarup 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 overloadingSwarup 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 sortSwarup 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 numbersSwarup 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 numberSwarup Boro
 
Canteen management program
Canteen management programCanteen management program
Canteen management programSwarup Boro
 
C++ program using class
C++ program using classC++ program using class
C++ program using classSwarup Boro
 
Railway reservation
Railway reservationRailway reservation
Railway reservationSwarup Boro
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 

Plus de 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
 

Dernier

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Dernier (20)

Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

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