SlideShare a Scribd company logo
1 of 4
Download to read offline
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

.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядомAndrey Akinshin
 
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
 
Python for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo CruzPython for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo Cruzrpmcruz
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonakaptur
 
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)HemaArora2
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorchHyungjoo Cho
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)changehee lee
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Andrey Akinshin
 
OPL best practices - Doing more with less easier
OPL best practices - Doing more with less easierOPL best practices - Doing more with less easier
OPL best practices - Doing more with less easierAlex Fleischer
 

What's hot (20)

.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядом
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
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...
 
901230 lecture5&6
901230 lecture5&6901230 lecture5&6
901230 lecture5&6
 
Python for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo CruzPython for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo Cruz
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorch
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
 
Data structures
Data structures Data structures
Data structures
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
Lists
ListsLists
Lists
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?
 
ROP
ROPROP
ROP
 
6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
 
OPL best practices - Doing more with less easier
OPL best practices - Doing more with less easierOPL best practices - Doing more with less easier
OPL best practices - Doing more with less easier
 
Code Generation
Code GenerationCode Generation
Code Generation
 
GCC
GCCGCC
GCC
 

Viewers also liked (17)

Pointers
PointersPointers
Pointers
 
Unit 3
Unit  3Unit  3
Unit 3
 
File handling
File handlingFile handling
File handling
 
Functions
FunctionsFunctions
Functions
 
2-D array
2-D array2-D array
2-D array
 
1-D array
1-D array1-D array
1-D array
 
01 computer communication and networks v
01 computer communication and networks v01 computer communication and networks v
01 computer communication and networks v
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
c++ program for Canteen management
c++ program for Canteen managementc++ program for Canteen management
c++ program for Canteen management
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Queue
QueueQueue
Queue
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 

Similar to Stack

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
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
 
#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docxajoy21
 
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
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
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
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8ecomputernotes
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 

Similar to Stack (20)

Stack
StackStack
Stack
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Stacks
StacksStacks
Stacks
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
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
 
#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
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
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.pptMO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
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
 
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
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 

More from Swarup Kumar Boro (11)

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2
 
computer science sample papers 3
computer science sample papers 3computer science sample papers 3
computer science sample papers 3
 
computer science sample papers 1
computer science sample papers 1computer science sample papers 1
computer science sample papers 1
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Boolean algebra laws
Boolean algebra lawsBoolean algebra laws
Boolean algebra laws
 
Class
ClassClass
Class
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Physics
PhysicsPhysics
Physics
 
Physics activity
Physics activityPhysics activity
Physics activity
 

Recently uploaded

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Recently uploaded (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
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Ữ Â...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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