Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Python Stack and Queue
    python

    Python Stack and Queue

    April 2, 2023No Comments2 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    The Stack and Queue Module for Python

    The storage in computers is organized thanks to data structure, which enables us to retrieve and modify data in an efficient manner. Stacks and queues hold the distinction of being the first data structures ever defined in the field of computer science. A straightforward Python list can perform the duties of both a queue and a stack in addition to that. A queue is a data structure that is used in computer programming for the purpose of sorting data according to the FIFO rule (First In First Out). It is usual practice to create stacks and queues using a linked or array-based structure.

    list.

    Stack

    A data structure known as a stack implements the LIFO (Last In First Out) principle in its operation. To put in place a stack, we will need two straightforward

    operations:


    • push –

      It adds an element to the top of the stack.

    • pop –

      It removes an element from the top of the stack.

    Python Stack and Queue

    Python Stack and Queue Methods of operation:

    W01A3@@1 Attributes and Qualities:

    • Insertion order of the stack is preserved.
    • Useful for parsing the operations.
    • Duplicacy is allowed.

    Code



    1. # Code to demonstrate Implementation of





    2. # stack using list




    3. x = [

      “Python”


      ,


      “C”


      ,


      “Android”


      ]


    4. x.push(

      “Java”


      )


    5. x.push(

      “C++”


      )



    6. print


      (x)



    7. print


      (x.pop())



    8. print


      (x)



    9. print


      (x.pop())



    10. print


      (x)

    Output:

    ['Python', 'C', 'Android', 'Java', 'C++']
    C++
    ['Python', 'C', 'Android', 'Java']
    Java
    ['Python', 'C', 'Android']
    

    Queue

    First-in-First-Out, or FIFO, is the principle that governs how a queue works. Due to the fact that it can be opened from either end, we are able to quickly add elements to the back and remove things from the front.

    In order to create a queue, we will need two simple things:

    operations:


    • enqueue –

      It adds an element to the end of the queue.

    • dequeue –

      It removes the element from the beginning of the queue.

    Python Stack and Queue

    Python Stack and Queue Procedures Performed on the Queue

    W01A3@@4 Attributes and Qualities

    • Insertion order of the queue is preserved.
    • Duplicacy is allowed.
    • Useful for parsing CPU task operations.

    Code



    1. import


      queue



    2. # Queue is created as an object ‘L’




    3. L = queue.Queue(maxsize=

      10


      )




    4. # Data is inserted in ‘L’ at the end using put()




    5. L.put(

      9


      )


    6. L.put(

      6


      )


    7. L.put(

      7


      )


    8. L.put(

      4


      )



    9. # get() takes data from





    10. # from the head





    11. # of the Queue





    12. print


      (L.get())



    13. print


      (L.get())



    14. print


      (L.get())



    15. print


      (L.get())

    Output:

    9
    6
    7
    4
    
    Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language Python Stack and Queue
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUsing PyInstaller to Easily Distribute Python Applications
    Next Article clock in Python

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.