You are currently viewing Python Stack LifoQueue Methods

In Python, we can implement the stack using Lifoqueue methods very efficiently. Stack is a one-dimensional data structure that will also call as Last In First Out (LIFO) data structure.

Advertisements

The element inserted at last will come out first, we can implement Stack in several ways. One way to implement Stack in Python is using LifoQueue. It is available in the queue module.

In this article, we will discuss the methods supported by LifoQueue.

  1. Insert elements one by one into stack using put() method
  2. Return total number of elements present in stack using qsize()
  3. Check whether the stack is full or not using full() method
  4. Popping elements from stack using get() method
  5. Check whether the stack is empty or not using empty()

It is important to specify the size of the Stack using maxsize parameter at the time of creating a Stack using LifoQueue(). It will take an integer that represents the stack size.

1. Quick Examples of Stack Lifo Queue methods

Let’s see the methods supported by LifoQueue in the implementation of Stack in python.


# Below are some quick exasamples.

# Initializing a stack with size 10
stack = queue.LifoQueue(maxsize=10)

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')
stack.put('Italy')

# Return total number of elements
print(stack.qsize())

# Check the stack is full or not
print("Full Stack: ", stack.full())

# Use the get() function to pop element from stack inLIFO order
print('3 elements popped from the stack are:')
print(stack.get())
print(stack.get())
print(stack.get())

# Check the stack empty or not
print("Empty Stack: ", stack.empty())

2. put() method of Stack Lifo Queue

put() method is used to insert an item to the stack. At a time, we can insert only one item. It takes item to be inserted as a parameter.

2.1 Syntax for put() method

Let’s see the syntax of put() method.


# Here, stack is the input stack implemented using LifoQueue().
stack.put(item)

2.2 Example

Let’s create a stack using LifoQueue() with maxsize – 10 and insert 5 countries one by one to it.


import queue

# Initializing a stack with size 10
stack = queue.LifoQueue(maxsize=10)

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')
stack.put('Italy')

3. get() method of Stack Lifo Queue

get() method is used to pop/remove only one item from the stack at a time. It won’t take any parameter. It will remove the lastly inserted item from stack.

3.1 Syntax for get() method

Let’s see the syntax of get() method.


# Here, stack is the input stack implemented using LifoQueue().
stack.get()

3.2 Example

Let’s create a stack using LifoQueue() with 5 elements and get 3 elements by popping them.


import queue

# Initializing a stack with size 6
stack = queue.LifoQueue(maxsize=6)

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')
stack.put('Italy')

# Use the get() function to pop element from stack in LIFO order
print('3 elements popped from the stack are:')
print(stack.get())
print(stack.get())
print(stack.get())

Yields below output.

Stack Lifo

First we inserted the countries in the following order – [‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Italy’].

We applied get() method three times. First ‘Italy’ is popped, then ‘Nepal’ is popped and lastly ‘England’ – is popped.

4. qsize() method of Stack Lifo Queue

qsize() method will return the total number of elements present in the stack. It won’t takes any parameter.

4.1 Syntax for qsize() method

Let’s see the syntax of qsize() methodd.


# Here, stack is the input stack implemented using LifoQueue().
stack.qsize()

4.2 Examples

Example 1: Let’s create a stack using LifoQueue() with 5 elements and return the total number of elements.


import queue

# Initializing a stack with size 10
stack = queue.LifoQueue(maxsize=10)

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')
stack.put('Italy')

# Return total number of elements
print(stack.qsize())

# Output:
# 5

Example 2: Let’s pop 2 items and return the total number of elements present in the stack.


import queue

# Initializing a stack with size 10
stack = queue.LifoQueue(maxsize=10)

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')
stack.put('Italy')

# Return total number of elements
print(stack.qsize())

# Pop 2 items
stack.get()
stack.get()

# Return total number of elements
print(stack.qsize())

# Output:
# 5
# 3

First time, there are 5 elements in the stack. After that we popped 2 items, when we try to display total number of elements, there were only 3.

5. full() method of Stack Lifo Queue

full() method return True if the stack is full i.e if total number of elements in the stack is equal to maxsize, Otherwise False is returned.

5.1 Syntax for full() method

Let’s see the syntax of full() method.


# Here, stack is the input stack implemented using LifoQueue().
stack.full()

5.2 Example

Let’s create a stack using LifoQueue() with 4 elements and check whether the stack is full or not.


import queue

# Initializing a stack with size 4
stack = queue.LifoQueue(maxsize=4)

# Check the stack is full or not
print("Full Stack: ", stack.full())

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')

# Check the stack is full or not
print("Full Stack: ", stack.full())

# Output:
# Full Stack:  False
# Full Stack:  True

Firstly, the stack is not full. So False is returned. After that we inserted 4 items into the stack. Now full() returned True (equal to maxsize).

6. empty() method of Stack Lifo Queue

empty() method return True if the stack is empty. Otherwise False is returned.

6.1 Syntax for empty() method

Let’s see the syntax of empty() method.


# Here, stack is the input stack implemented using LifoQueue().
stack.empty()

6.2 Example

Let’s create a stack using LifoQueue() with 4 elements and check whether the stack is empty or not.


import queue

# Initializing a stack with size 4
stack = queue.LifoQueue(maxsize=4)

# Check the stack is empty or not
print("Empty Stack: ", stack.empty())

# Using put() function to insert elements
stack.put('China')
stack.put('Russia')
stack.put('England')
stack.put('Nepal')

# Check the stack is empty or not
print("Empty Stack: ", stack.empty())

# Output:
# Empty Stack:  True
# Empty Stack:  False

Firstly, the stack is empty. So True is returned. After that we inserted 4 items into the stack. Now empty() returned False..

7. Conclusion

We have seen how to insert elements to stack using the put() method of Python LifoQueue. To pop elements from the stack we used the get() method. To check whether the stack is empty or not, we used full() and empty() methods. qsize() method will return the total number of elements present in the stack.