0
22kviews
Menu driven program for data structure using built-in function for linked list, stack and queue in python
1 Answer
4
266views

Menu-driven Program for Stack Data Structure

  • Python offers various ways to implement the stack. Such as

    • List

    • Collection.deque

    • LifeQueue

  • Here, Python List can be used to implement the stack. For those following, in-build methods are used in the below program.

    • append() method - To PUSH or insert elements into the stack.

    • pop() method - To POP out or remove the element from the stack in LIFO order.

    • len(stack) - To find out the size of the stack.

Python Program for STACK Data Structure:

# Program introduction statement
print("Simple STACK Data Structure Program")  

# Initial empty STACK  
stack = [] 

# Display Menu with Choices 
while True:  
    print("\nSELECT APPROPRIATE CHOICE")                    
    print("1. PUSH Element into the Stack")  
    print("2. POP Element from the Stack") 
    print("3. Display Elements of the Stack")  
    print("4. Exit")  
    choice = int(input("Enter the Choice:"))  # Taking input from the user regarding choice

    # USER enter option 1 then PUSH elements into the STACK
    if choice == 1:   
        # append() function to PUSH elements into the STACK  
        stack.append("Monday")  # PUSH element Monday
        stack.append("Tuesday")  # PUSH element Tuesday 
        stack.append("Wednesday")  # PUSH element Wednesday
        stack.append("Thursday")  # PUSH element Thursday
        stack.append("Friday")  # PUSH element Friday
        stack.append("Saturday")  # PUSH element Saturday
        stack.append("Sunday")  # PUSH element Sunday
        stack.append('8')  # PUSH element 8  
        print('\nTotal 8 elements PUSH into the STACK')

    # USER enter option 2 then POP one element from the STACK     
    elif choice == 2:
        if len(stack) == 0:                       # Check whether STACK is Empty or not
            print('The STACK is EMPTY No element to POP out') 
                                           # Display this ERROR message if STACK is Empty
        else:    
            # pop() function to POP element from the STACK in LIFO order   
            print('\nElement POP out from the STACK is:')  
            print(stack.pop())      # Display the element which is POP out from the STACK 

    # USER enter option 3 then display the STACK
    elif choice == 3:
        if len(stack) == 0:                       # Check whether STACK is Empty or not
            print('The STACK is initially EMPTY') # Display this message if STACK is Empty
        else:
            print("The Size of the STACK is: ",len(stack))  # Compute the size of the STACK            
            print('\nSTACK elements are as follows:')            
            print(stack)                          # Display all the STACK elements    

    # User enter option 4 then EXIT from the program
    elif choice == 4:  
        break  

    # Shows ERROR message if the choice is not in between 1 to 4  
    else:  
        print("Oops! Incorrect Choice")

Output:

Python Stack Program OP


Menu-driven Program for Queue Data Structure

  • Python offers various methods to perform the basic operations on Queue.

  • Here, the following in-build methods are used in the below program.

    • put(item) method - To insert elements into the queue.

    • get() method - To extract the element from the queue in FIFO order

    • empty() method - To check whether a queue is empty or not. It returns true if the queue is empty.

    • qsize() method - To find out the size of the queue.

Python Program for QUEUE Data Structure:

# Import Python Package
from queue import Queue 

# Program introduction statement
print("Simple QUEUE Data Structure Program")  

# Initial empty QUEUE 
queue = Queue();   

# Display Menu with Choices 
while True:  
    print("\nSELECT APPROPRIATE CHOICE")                    
    print("1. PUT Element into the Queue")  
    print("2. GET Element from the Queue") 
    print("3. Display Elements of the Queue")  
    print("4. Exit")  
    choice = int(input("Enter the Choice:"))  # Taking input from the user regarding choice

    # USER enter option 1 then PUT elements into the QUEUE
    if choice == 1:   
        # put() function to PUT elements into the QUEUE 
        queue.put("Monday")  # PUT  element Monday
        queue.put("Tuesday")  # PUT  element Tuesday 
        queue.put("Wednesday")  # PUT element Wednesday
        queue.put("Thursday")  # PUT element Thursday
        queue.put("Friday")  # PUT element Friday
        queue.put("Saturday")  # PUT element Saturday
        queue.put("Sunday")  # PUT element Sunday
        queue.put('8')  # PUT  element 8  
        print('\nTotal 8 elements PUT into the QUEUE')

    # USER enter option 2 then GET one element from the QUEUE     
    elif choice == 2:  
        if (queue.empty() == True):           # Check whether QUEUE is Empty or not
            print('The QUEUE is EMPTY No element to GET out') 
                                         # Display this ERROR message if QUEUE is Empty
        else:        
        # get() function to GET element out from the QUEUE in FIFO order   
            print('\nElement GET out from the QUEUE is:')  
            print(queue.get())   # Display the element which is GET out from the QUEUE 

    # USER enter option 3 then display the QUEUE
    elif choice == 3:
        if (queue.empty() == True):                    # Check whether QUEUE is Empty or not
            print('The QUEUE is initially EMPTY') 
                                           # Display this message if QUEUE is Empty
        else:
            print("The Size of the QUEUE is: ",queue.qsize())  # Compute the size of the QUEUE  
            print('\nQUEUE elements are as follows:')            
            print(list(queue.queue))                        # Display all the QUEUE elements    

    # User enter option 4 then EXIT from the program
    elif choice == 4:  
        break  

    # Shows ERROR message if the choice is not in between 1 to 4  
    else:  
        print("Oops! Incorrect Choice")

Output:

Python Queue Program OP


Menu-driven Program for Linked List Data Structure

  • Python does not have a linked list library built into it like the classical programming languages.

  • Python does have an inbuilt type list that works as a dynamic array but its operation shouldn’t be confused with a typical function of a linked list.

  • Here, the deque() package is used in the collections module.

  • This is an inbuilt class in Python used for dequeue but can be implemented in such a way that it works like a linked list under certain conditions.

  • In the below program, the following in-build methods are used.

    • append() method - To insert elements into the linked list.

    • insert(index, item) method - To insert an element after a certain index place into the linked list.

    • pop() method - To delete the last element in the linked list.

    • remove(item) method - To remove a specific element from the linked list.

Python Program for LINKED LIST Data Structure:

# importing module
import collections

# Program introduction statement
print("Simple LINKED LIST Data Structure Program")  

# initialising a deque() of arbitrary length to create Linked List
linked_lst = collections.deque()

# Display Menu with Choices 
while True:  
    print("\nSELECT APPROPRIATE CHOICE")                     
    print("1. INSERT elements into Linked List") 
    print("2. INSERT elemnt at a Specific Position")
    print("3. Display all the elements of the  Linked List ")   
    print("4. DELETE the last element from the Linked List")
    print("5. DELETE the specific element from the Linked List")  
    print("6. Exit")  
    choice = int(input("Enter the Choice:"))  # Taking input from the user regarding choice

    # USER enter option 1 then INSERT element in the Linked List
    if choice == 1:   
        # append() function to fill deque() with elements and inserting into the Linked List
        linked_lst.append("Monday")  # INSERT element Monday
        linked_lst.append("Tuesday")  # INSERT element Tuesday 
        linked_lst.append("Wednesday")  # INSERT element Wednesday
        linked_lst.append("Sunday")  # INSERT element Sunday
        print('\nTotal 4 elements INSERTED into the Linked List')

    # USER enter option 2 then INSERT element at a specific position in the Linked List
    if choice == 2:   
        # insert() function add element after the specified position in the Linked List
        linked_lst.insert(3, 'Thursday')  
                                  # INSERT element Thursday after 3rd element of Linked List
        linked_lst.insert(5, 'Saturday')  
                                  # INSERT element Saturday after 5th element of Linked List
        linked_lst.insert(4, 'Friday')  
                                 # INSERT element Friday after 4th element of Linked List
        print('\nTotal 3 new elements INSERTED at specific position in the Linked List')

    # USER enter option 3 then display the Linked List
    elif choice == 3:
        if len(linked_lst) == 0:           # Check whether Linked List is Empty or not
            print('The Linked List is initially EMPTY') 
                                       # Display this message if Linked List is Empty
        else:
            print("The Size of the Linked List is: ",len(linked_lst))  
                                     # Compute the size of the Linked List  
            print('\nLinked List elements are as follows:')            
            print(linked_lst) 

    # USER enter option 4 then DELETE last element in the Linked List    
    elif choice == 4:  
        if len(linked_lst) == 0:                 # Check whether Linked List is Empty or not
            print('The Linked List is EMPTY No element to DELETE') 
                                        # Display this ERROR message if Linked List is Empty
        else:
            # pop() function to DELETE last element in the Linked List 
            print('\nLast element DELETED from the Linked List is:')  
            print(linked_lst.pop())   
                                # Display the element which is Deleted from the Linked List

    # USER enter option 5 then DELETE the specific element from the Linked List     
    elif choice == 5:  
        if len(linked_lst) == 0:                # Check whether Linked List is Empty or not
            print('The Linked List is EMPTY No element to DELETE') 
                                        # Display this ERROR message if Linked List is Empty
        else:
            # remove() function to DELETE the specific element from the Linked List
            print('\nSpecific element Monday DELETED from the Linked List')  
            linked_lst.remove('Monday')  # Remove Monday from the Linked List

    # User enter option 6 then EXIT from the program
    elif choice == 6:  
        break

Output:

Python Linked List OP

Please log in to add an answer.