Day 24 of 100 Days of Python
Practising DSA
Today is Day 24. I practised Stack Data Structures today. It follows Last in First Out (LIFO) principle. I used the Python Lists and its built in functions to get idea of how Stack works. I used .append() function to add a value in the end of the list and .pop() function to get the last index value and also remove it from the list. Here is the code:
1
2
3
4
5
6
7
8
9
10
# Stack
history_stack = []
history_stack.append("google.com")
history_stack.append("wikipedia.org")
history_stack.append("github.com")
print(history_stack)
back_page= history_stack.pop()
print(f"Clicked back. Leaving {back_page}")
print(history_stack)
Output for Program 1:
1
2
3
['google.com', 'wikipedia.org', 'github.com']
Clicked back. Leaving github.com
['google.com', 'wikipedia.org']
The second program I did today is the opposite of Stack Data Structure, Queue. It follows First in First out (FIFO) principle. I tried to replicate a very basic ticketing queue in Python.
1
2
3
4
5
6
7
8
9
10
# Queue
support_queue =[]
support_queue.append("Customer A")
support_queue.append("Customer B")
support_queue.append("Customer C")
print(support_queue)
served_customer = support_queue.pop(0)
print(f"Serving: {served_customer}")
print(support_queue)
Output for Program 2:
1
2
3
['Customer A', 'Customer B', 'Customer C']
Serving: Customer A
['Customer B', 'Customer C']
This post is licensed under CC BY 4.0 by the author.