SlideShare a Scribd company logo
Implementing Stacks and Queues Using
Linked List

Objectives
In this lesson, you will learn to:
 Define stacks
 Identify applications of stacks
 Implement stacks using linked lists
        Define queues
 Identify the two types of queues:
          Deques
          Priority queue
 Implement queues using linked lists
                             Implementing Stacks and Queues Using Linked
 ©NIIT
                                                List/Lesson 3/Slide 1 of 34
Implementing Stacks and Queues Using
Linked List

Stacks
 Are a form of single linked lists in which addition and
  deletion are performed in the beginning of the list
 Are also called push-down lists




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 2 of 34
Implementing Stacks and Queues Using
Linked List

Stacks (Contd..)
 Enable the item which is added last to be removed
  first (Last In First Out i.e. LIFO)


                          TOP                 PUSH
            POP




                         DataC

                         DataB

                         DataA




                         Implementing Stacks and Queues Using Linked
 ©NIIT
                                            List/Lesson 3/Slide 3 of 34
Implementing Stacks and Queues Using
Linked List

Stacks (Contd..)
 Example:
   Function calls stored in a stack




         PUSH the calling                         POP out the calling
         function values                         function values once
         addresses when the    s u b s tring()    the called function
         function is invoked                      execution is over

                                  m ain()




                                Implementing Stacks and Queues Using Linked
 ©NIIT
                                                   List/Lesson 3/Slide 4 of 34
Implementing Stacks and Queues Using
Linked List

Stack Operations
 Operations that can be carried out on stacks are:
      Addition (PUSH)
      Removal (POP)




                         Implementing Stacks and Queues Using Linked
 ©NIIT
                                            List/Lesson 3/Slide 5 of 34
Implementing Stacks and Queues Using
Linked List

Stack Operations (Contd..)
 Some Common applications of stacks are:
      Parameter tracking and passing values with
       function calls
      Validating arithmetic expressions
      Evaluating prefix and postfix notations




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 6 of 34
Implementing Stacks and Queues Using
Linked List

Application of Stacks
 Towers of Hanoi




             PIN 1   PIN 2             PIN 3



                        Implementing Stacks and Queues Using Linked
 ©NIIT
                                           List/Lesson 3/Slide 7 of 34
Implementing Stacks and Queues Using
Linked List

Application of Stacks
 The rules applicable to the Towers of Hanoi are as
  follows:
      Only one disc at a time should be moved,
       specifically the topmost disc from any pin
      A larger disc should never be placed on a smaller
       disc




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 8 of 34
Implementing Stacks and Queues Using
Linked List

Infix Notation
 The arithmetic expressions that are written with the
  operator placed between the operands are said to be
  in Infix Notation
 Is evaluated using general mathematics rules for
  operator precedence like:
          Parenthesis (highest precedence)
          Exponentiation (second highest precedence)
          Multiplication and division (third highest
           precedence)
          Addition and subtraction (lowest precedence)

                               Implementing Stacks and Queues Using Linked
 ©NIIT
                                                  List/Lesson 3/Slide 9 of 34
Implementing Stacks and Queues Using
Linked List

Postfix Notation
 Precedence values or parenthesis are not required
  to determine the order of operation.
 1+2 would be denoted as 12+
 The evaluation of an arithmetic expression written in
  infix is done in two steps:
          Conversion of an infix expression to a postfix
           expression
          Evaluation of the postfix expression




                              Implementing Stacks and Queues Using Linked
 ©NIIT
                                                List/Lesson 3/Slide 10 of 34
Implementing Stacks and Queues Using
Linked List

Conversion of an Infix Expression to a Postfix
Expression
 Is done by following the listed steps:
   1. Scan the expression from left to right and execute
      steps 2 to 5 until the stack is empty
   2. If the scanned item is an operand, add it to the
      postfix string, which is the string that will contain the
      final converted postfix expression
   3. If the scanned item is a parenthesis, PUSH it to the
      stack
   4. If the scanned item is an operator, add the current
      operator to the stack
                             Implementing Stacks and Queues Using Linked
 ©NIIT
                                               List/Lesson 3/Slide 11 of 34
Implementing Stacks and Queues Using
Linked List

Conversion of an Infix Expression to a Postfix
Expression (Contd..)
   5. If a right parenthesis is encountered then,
     5.1 POP each operator from the top of the stack until
         the left parenthesis is encountered
     5.2 Remove the left parenthesis from the stack
   6. Exit




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                             List/Lesson 3/Slide 12 of 34
Implementing Stacks and Queues Using
Linked List

Evaluation of a Postfix Expression
 Is done by following the listed steps:
   1. Add a ')' to the end of the postfix expression
   2. Scan the expression from left to right
   3. Repeat steps 4 and 5
   4. If an operand is encountered, PUSH every operand
      encountered to the stack
   5. If an operator is encountered,
     5.1 POP the top two elements
     5.2 Evaluate the expression of the two elements
     5.3 PUSH the result to the stack
                             Implementing Stacks and Queues Using Linked
 ©NIIT
                                               List/Lesson 3/Slide 13 of 34
Implementing Stacks and Queues Using
Linked List

Evaluation of a Postfix Expression (Contd..)
   6. Set RESULT = top of the stack
   7. Exit on encountering the ')' value




                            Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 14 of 34
Implementing Stacks and Queues Using
Linked List

Implementing Stacks
 Using linked lists
         Is preferred because of dynamic allocation of
          memory
 Using arrays
       Is disadvantageous because it requires the size
    of the array to be pre-defined




                            Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 15 of 34
Implementing Stacks and Queues Using
Linked List

Implementing Stacks (Contd..)
 Example:
   class Stack
   {
   private:
       //Member data
       Node *top;
   public:
       Stack();
       void PUSH(node *);
       Node *POP();
       ~Stack();
   };
                     Implementing Stacks and Queues Using Linked
 ©NIIT
                                       List/Lesson 3/Slide 16 of 34
Implementing Stacks and Queues Using
Linked List

Problem Statement 3.D.1
Create an application that implements stack by using
single linked list and prints the accepted input in reverse
order.




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                             List/Lesson 3/Slide 17 of 34
Implementing Stacks and Queues Using
Linked List

Problem Statement 3.P.1
Create an application that accepts an expression with
round brackets and validates it. The application should
ensure that the number of opening brackets should be
equivalent to the number of closing brackets.
Hint: An expression (2+3)*5 can be used to evaluate
the given program.




                          Implementing Stacks and Queues Using Linked
 ©NIIT
                                            List/Lesson 3/Slide 18 of 34
Implementing Stacks and Queues Using
Linked List

Queues
 Are sequential lists in which items are inserted into the
  tail end of the queue and taken from the head
 Example:
   Printer queue




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                             List/Lesson 3/Slide 19 of 34
Implementing Stacks and Queues Using
Linked List

Queues (Contd..)
 Are also called First In First Out (FIFO) lists




         REAR     B    C    X     D     G          FRONT




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                             List/Lesson 3/Slide 20 of 34
Implementing Stacks and Queues Using
Linked List

Types of Queues
 Deques
 Priority queues




                    Implementing Stacks and Queues Using Linked
 ©NIIT
                                      List/Lesson 3/Slide 21 of 34
Implementing Stacks and Queues Using
Linked List

Deques
 Stands for double-ended queues
 Are queues in which elements can be added or
  removed from both the ends of the queue
 Example:


  FRONT                                                       REAR



             Ann   Bob     Tim         Joe         Ken




                         Implementing Stacks and Queues Using Linked
 ©NIIT
                                           List/Lesson 3/Slide 22 of 34
Implementing Stacks and Queues Using
Linked List

Deques (Contd..)
 Are of two types:
      Input restricted deques
      Output restricted deques




                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                             List/Lesson 3/Slide 23 of 34
Implementing Stacks and Queues Using
Linked List

Priority Queues
 Are queues which have priority associated with the
  items added to the queue that determines the order in
  which the elements are processed and deleted
 Are used by a time-sharing system
 Are used by operating system schedulers for
  scheduling activities to be processed by the
  microprocessor
 Are used by simulation applications




                         Implementing Stacks and Queues Using Linked
 ©NIIT
                                           List/Lesson 3/Slide 24 of 34
Implementing Stacks and Queues Using
Linked List

Priority Queues (Contd..)
 In priority queues:
      An element with a higher priority is processed first
      If two elements share the same priority, then the
       element that came in first is processed first




                            Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 25 of 34
Implementing Stacks and Queues Using
Linked List

Operations on Queues
 Primitive operations are:
      Insertion of nodes
      Deletion of nodes
 Additional operations are:
      Checking if a queue is empty
      Emptying a queue
      Counting the number of elements in a queue




                            Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 26 of 34
Implementing Stacks and Queues Using
Linked List

Implementing Queues
 Implementing queues by using arrays is not flexible as
  an array is of a fixed size
 Implementing queues by using linked list is flexible as
  when a new item is added, it is simply linked to the
  end of the queue.
 A queue when implemented using linked lists is called
  a linked queue.




                          Implementing Stacks and Queues Using Linked
 ©NIIT
                                            List/Lesson 3/Slide 27 of 34
Implementing Stacks and Queues Using
Linked List

Problem Statement 3.D.2
Create an application that will accept the applicant
names as and when they appear for interview. The list of
applicants should be updated every hour.




                          Implementing Stacks and Queues Using Linked
 ©NIIT
                                            List/Lesson 3/Slide 28 of 34
Implementing Stacks and Queues Using
Linked List

Problem Statement 3.P.2
Create an application that will print the details of the first
five early bird prize winners.
Hint: The details to be accepted are the name and e-mail
id.




                             Implementing Stacks and Queues Using Linked
 ©NIIT
                                               List/Lesson 3/Slide 29 of 34
Implementing Stacks and Queues Using
Linked List

Summary
In this lesson, you learned that:
 A stack is a data structure in which operations like
 adding and removing data are performed from only
 one end called as TOP
 Stacks can be used to:
         Validate arithmetic expressions
         Evaluate infix and postfix notation
       Store function arguments and address whenever
    a                 function calls another function


                              Implementing Stacks and Queues Using Linked
 ©NIIT
                                                List/Lesson 3/Slide 30 of 34
Implementing Stacks and Queues Using
Linked List

Summary (Contd..)
 The operations that can be carried out on stacks are:
         Push
         Pop
 In computers where rules for precedence do not
  exist, the evaluation of an arithmetic expression
  written in infix is done in two steps:
         Conversion of an infix expression to a postfix
                         expression
         Evaluation of the postfix expression
 Stacks can be implemented by using arrays or linked
  lists

                              Implementing Stacks and Queues Using Linked
 ©NIIT
                                                List/Lesson 3/Slide 31 of 34
Implementing Stacks and Queues Using
Linked List

Summary (Contd..)
 Defining stacks by using linked list is preferred
    because of dynamic memory allocation
 A queue is a sequential list where items are inserted
  into the tail end of the queue and taken from the head
 The tail is referred to as the REAR element and the
  head is the FRONT element of a queue, where each
  element is deleted from the end called FRONT and
  added at the other end called REAR
 Queues are called First In First Out (FIFO) lists
 Queues can be categorized into two types:
     Deques (pronounced either deck or deQueue)
     Priority queue
                            Implementing Stacks and Queues Using Linked
 ©NIIT
                                              List/Lesson 3/Slide 32 of 34
Implementing Stacks and Queues Using
Linked List

Summary (Contd..)
 A deques is also called a double-ended queue. In this,
elements are added or removed from both ends of the
queue
 The two types of deques are:
      Output-restricted
      Input-restricted
 Often, the items added to a queue have a priority
  associated with them that determines the order in
  which the processing and deletion of the each
  element in the queue happens. Such a kind of queue
  is called a priority queue
                           Implementing Stacks and Queues Using Linked
 ©NIIT
                                             List/Lesson 3/Slide 33 of 34
Implementing Stacks and Queues Using
Linked List

Summary (Contd..)
 The different applications of queues are:
      Time-sharing systems
      Operating system schedulers for scheduling
         activities to be processed by the microprocessor
      Simulation applications
 A queue can be implemented using arrays or linked
  list
 A queue when implemented using linked lists is called
  a linked queue

                             Implementing Stacks and Queues Using Linked
 ©NIIT
                                               List/Lesson 3/Slide 34 of 34

More Related Content

What's hot

C programming session 07
C programming session 07C programming session 07
C programming session 07
Dushmanta Nath
 

What's hot (18)

Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Tree
TreeTree
Tree
 
Ds 1
Ds 1Ds 1
Ds 1
 
Ds 2
Ds 2Ds 2
Ds 2
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Programming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesProgramming & Data Structure Lecture Notes
Programming & Data Structure Lecture Notes
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Lesson 2.1 array
Lesson 2.1   arrayLesson 2.1   array
Lesson 2.1 array
 
Data and File Structure Lecture Notes
Data and File Structure Lecture NotesData and File Structure Lecture Notes
Data and File Structure Lecture Notes
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Computing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
Computing 9691 Test Paper Paper 3 for May / June 2007 CambridgeComputing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
Computing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
 
Lecture4a dynamic data_structure
Lecture4a dynamic data_structureLecture4a dynamic data_structure
Lecture4a dynamic data_structure
 
Lecture1 data structure(introduction)
Lecture1 data structure(introduction)Lecture1 data structure(introduction)
Lecture1 data structure(introduction)
 
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
 
Data Structure
Data Structure Data Structure
Data Structure
 

Similar to Ds 3

Advanced data structures slide 2 2+
Advanced data structures slide 2 2+Advanced data structures slide 2 2+
Advanced data structures slide 2 2+
jomerson remorosa
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
karymadelaneyrenne19
 

Similar to Ds 3 (20)

Lecture 2c stacks
Lecture 2c stacksLecture 2c stacks
Lecture 2c stacks
 
Data Structure - Stacks
Data Structure - StacksData Structure - Stacks
Data Structure - Stacks
 
Advanced data structures slide 2 2+
Advanced data structures slide 2 2+Advanced data structures slide 2 2+
Advanced data structures slide 2 2+
 
Datastructure
DatastructureDatastructure
Datastructure
 
stack 1.pdf
stack 1.pdfstack 1.pdf
stack 1.pdf
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-8.pdf
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 

Ds 3

  • 1. Implementing Stacks and Queues Using Linked List Objectives In this lesson, you will learn to: Define stacks Identify applications of stacks Implement stacks using linked lists Define queues Identify the two types of queues: Deques Priority queue Implement queues using linked lists Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 1 of 34
  • 2. Implementing Stacks and Queues Using Linked List Stacks Are a form of single linked lists in which addition and deletion are performed in the beginning of the list Are also called push-down lists Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 2 of 34
  • 3. Implementing Stacks and Queues Using Linked List Stacks (Contd..) Enable the item which is added last to be removed first (Last In First Out i.e. LIFO) TOP PUSH POP DataC DataB DataA Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 3 of 34
  • 4. Implementing Stacks and Queues Using Linked List Stacks (Contd..) Example: Function calls stored in a stack PUSH the calling POP out the calling function values function values once addresses when the s u b s tring() the called function function is invoked execution is over m ain() Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 4 of 34
  • 5. Implementing Stacks and Queues Using Linked List Stack Operations Operations that can be carried out on stacks are: Addition (PUSH) Removal (POP) Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 5 of 34
  • 6. Implementing Stacks and Queues Using Linked List Stack Operations (Contd..) Some Common applications of stacks are: Parameter tracking and passing values with function calls Validating arithmetic expressions Evaluating prefix and postfix notations Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 6 of 34
  • 7. Implementing Stacks and Queues Using Linked List Application of Stacks Towers of Hanoi PIN 1 PIN 2 PIN 3 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 7 of 34
  • 8. Implementing Stacks and Queues Using Linked List Application of Stacks The rules applicable to the Towers of Hanoi are as follows: Only one disc at a time should be moved, specifically the topmost disc from any pin A larger disc should never be placed on a smaller disc Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 8 of 34
  • 9. Implementing Stacks and Queues Using Linked List Infix Notation The arithmetic expressions that are written with the operator placed between the operands are said to be in Infix Notation Is evaluated using general mathematics rules for operator precedence like: Parenthesis (highest precedence) Exponentiation (second highest precedence) Multiplication and division (third highest precedence) Addition and subtraction (lowest precedence) Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 9 of 34
  • 10. Implementing Stacks and Queues Using Linked List Postfix Notation Precedence values or parenthesis are not required to determine the order of operation. 1+2 would be denoted as 12+ The evaluation of an arithmetic expression written in infix is done in two steps: Conversion of an infix expression to a postfix expression Evaluation of the postfix expression Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 10 of 34
  • 11. Implementing Stacks and Queues Using Linked List Conversion of an Infix Expression to a Postfix Expression Is done by following the listed steps: 1. Scan the expression from left to right and execute steps 2 to 5 until the stack is empty 2. If the scanned item is an operand, add it to the postfix string, which is the string that will contain the final converted postfix expression 3. If the scanned item is a parenthesis, PUSH it to the stack 4. If the scanned item is an operator, add the current operator to the stack Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 11 of 34
  • 12. Implementing Stacks and Queues Using Linked List Conversion of an Infix Expression to a Postfix Expression (Contd..) 5. If a right parenthesis is encountered then, 5.1 POP each operator from the top of the stack until the left parenthesis is encountered 5.2 Remove the left parenthesis from the stack 6. Exit Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 12 of 34
  • 13. Implementing Stacks and Queues Using Linked List Evaluation of a Postfix Expression Is done by following the listed steps: 1. Add a ')' to the end of the postfix expression 2. Scan the expression from left to right 3. Repeat steps 4 and 5 4. If an operand is encountered, PUSH every operand encountered to the stack 5. If an operator is encountered, 5.1 POP the top two elements 5.2 Evaluate the expression of the two elements 5.3 PUSH the result to the stack Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 13 of 34
  • 14. Implementing Stacks and Queues Using Linked List Evaluation of a Postfix Expression (Contd..) 6. Set RESULT = top of the stack 7. Exit on encountering the ')' value Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 14 of 34
  • 15. Implementing Stacks and Queues Using Linked List Implementing Stacks Using linked lists Is preferred because of dynamic allocation of memory Using arrays Is disadvantageous because it requires the size of the array to be pre-defined Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 15 of 34
  • 16. Implementing Stacks and Queues Using Linked List Implementing Stacks (Contd..) Example: class Stack { private: //Member data Node *top; public: Stack(); void PUSH(node *); Node *POP(); ~Stack(); }; Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 16 of 34
  • 17. Implementing Stacks and Queues Using Linked List Problem Statement 3.D.1 Create an application that implements stack by using single linked list and prints the accepted input in reverse order. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 17 of 34
  • 18. Implementing Stacks and Queues Using Linked List Problem Statement 3.P.1 Create an application that accepts an expression with round brackets and validates it. The application should ensure that the number of opening brackets should be equivalent to the number of closing brackets. Hint: An expression (2+3)*5 can be used to evaluate the given program. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 18 of 34
  • 19. Implementing Stacks and Queues Using Linked List Queues Are sequential lists in which items are inserted into the tail end of the queue and taken from the head Example: Printer queue Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 19 of 34
  • 20. Implementing Stacks and Queues Using Linked List Queues (Contd..) Are also called First In First Out (FIFO) lists REAR B C X D G FRONT Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 20 of 34
  • 21. Implementing Stacks and Queues Using Linked List Types of Queues Deques Priority queues Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 21 of 34
  • 22. Implementing Stacks and Queues Using Linked List Deques Stands for double-ended queues Are queues in which elements can be added or removed from both the ends of the queue Example: FRONT REAR Ann Bob Tim Joe Ken Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 22 of 34
  • 23. Implementing Stacks and Queues Using Linked List Deques (Contd..) Are of two types: Input restricted deques Output restricted deques Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 23 of 34
  • 24. Implementing Stacks and Queues Using Linked List Priority Queues Are queues which have priority associated with the items added to the queue that determines the order in which the elements are processed and deleted Are used by a time-sharing system Are used by operating system schedulers for scheduling activities to be processed by the microprocessor Are used by simulation applications Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 24 of 34
  • 25. Implementing Stacks and Queues Using Linked List Priority Queues (Contd..) In priority queues: An element with a higher priority is processed first If two elements share the same priority, then the element that came in first is processed first Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 25 of 34
  • 26. Implementing Stacks and Queues Using Linked List Operations on Queues Primitive operations are: Insertion of nodes Deletion of nodes Additional operations are: Checking if a queue is empty Emptying a queue Counting the number of elements in a queue Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 26 of 34
  • 27. Implementing Stacks and Queues Using Linked List Implementing Queues Implementing queues by using arrays is not flexible as an array is of a fixed size Implementing queues by using linked list is flexible as when a new item is added, it is simply linked to the end of the queue. A queue when implemented using linked lists is called a linked queue. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 27 of 34
  • 28. Implementing Stacks and Queues Using Linked List Problem Statement 3.D.2 Create an application that will accept the applicant names as and when they appear for interview. The list of applicants should be updated every hour. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 28 of 34
  • 29. Implementing Stacks and Queues Using Linked List Problem Statement 3.P.2 Create an application that will print the details of the first five early bird prize winners. Hint: The details to be accepted are the name and e-mail id. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 29 of 34
  • 30. Implementing Stacks and Queues Using Linked List Summary In this lesson, you learned that: A stack is a data structure in which operations like adding and removing data are performed from only one end called as TOP Stacks can be used to: Validate arithmetic expressions Evaluate infix and postfix notation Store function arguments and address whenever a function calls another function Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 30 of 34
  • 31. Implementing Stacks and Queues Using Linked List Summary (Contd..) The operations that can be carried out on stacks are: Push Pop In computers where rules for precedence do not exist, the evaluation of an arithmetic expression written in infix is done in two steps: Conversion of an infix expression to a postfix expression Evaluation of the postfix expression Stacks can be implemented by using arrays or linked lists Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 31 of 34
  • 32. Implementing Stacks and Queues Using Linked List Summary (Contd..) Defining stacks by using linked list is preferred because of dynamic memory allocation A queue is a sequential list where items are inserted into the tail end of the queue and taken from the head The tail is referred to as the REAR element and the head is the FRONT element of a queue, where each element is deleted from the end called FRONT and added at the other end called REAR Queues are called First In First Out (FIFO) lists Queues can be categorized into two types: Deques (pronounced either deck or deQueue) Priority queue Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 32 of 34
  • 33. Implementing Stacks and Queues Using Linked List Summary (Contd..) A deques is also called a double-ended queue. In this, elements are added or removed from both ends of the queue The two types of deques are: Output-restricted Input-restricted Often, the items added to a queue have a priority associated with them that determines the order in which the processing and deletion of the each element in the queue happens. Such a kind of queue is called a priority queue Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 33 of 34
  • 34. Implementing Stacks and Queues Using Linked List Summary (Contd..) The different applications of queues are: Time-sharing systems Operating system schedulers for scheduling activities to be processed by the microprocessor Simulation applications A queue can be implemented using arrays or linked list A queue when implemented using linked lists is called a linked queue Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 34 of 34