Python Program to Implement a Stack














































Python Program to Implement a Stack



Description:
Stack data structure follows the LIFO (Last In First Out ) principle. It means we have to put and remove the data from only the end/top of the List. The Stack data structure has two functionality push( add data) and pop(remove data). For implement the stack, we are using the list for push operation append the data in a list("list.append('data')") and for the pop operation, remove the data from the end or top of the list("list.pop()").

For more info you may prefer Python3 Program:

class Stack:
def __init__(self):
self._arr = []

def push(self, d=None):
self._arr.append(d)
print('------Top element is:{}-------'.format(d))

def pop(self):
if self._arr == [] :
print("-------Stack is empty--------")
else:
print("-------poped element is :{}------".format(self._arr.pop()))


def main():
q1 = Stack()
print('1.push()\n2.pop()\n3.exit')
while 1:
O = input("Enter your option:")
if O == "1":
d = int(input("Enter data:"))
q1.push(d)
elif O == "2":
q1.pop()
else:
break


if __name__ == "__main__":
main()


Output:
1.push()
2.pop()
3.exit
Enter your option:1
Enter data:7
------Top element is:7-------
Enter your option:1
Enter data:8
------Top element is:8-------
Enter your option:1
Enter data:9
------Top element is:9-------
Enter your option:2
-------poped element is :9------
Enter your option:2
-------poped element is :8------
Enter your option:2
-------poped element is :7------
Enter your option:2
-------Stack is empty--------
Enter your option:1
Enter data:5
------Top element is:5-------
Enter your option:2
-------poped element is :5------
Enter your option:2
-------Stack is empty--------
Enter your option:3

More Articles of Dilkhush Kumar:

Name Views Likes
test1 256 4
test 328 9
Python DB2 connector get started 417 1
Python PostgreSQL 388 4
Python PostgreSQL prevent SQL injection in UPDATE 387 3
Python PostgreSQL prevent SQL injection in DELETE 431 4
Python PostgreSQL prevent SQL injection in SELECT 473 4
Python PostgreSQL join two table 368 3
Python PostgreSQL limit the no of records in a table 405 7
Python PostgreSQL update existing record 400 7
Python PostgreSQL drop table only if exists 409 9
Python PostgreSQL drop table 350 7
Python PostgreSQL delete record from the table 532 6
Python PostgreSQL sort the result in ascending order 527 7
Python PostgreSQL sort the result in descending order 601 7
Python PostgreSQL sort the result 330 6
Python PostgreSQL Order By 353 7
Python PostgreSQL wildcard select 481 6
Python PostgreSQL select with a Filter 549 6
Python PostgreSQL where example and usgae 391 6
Python PostgreSQL select using fetchone method 390 7
Python PostgreSQL select particlular column from table 348 7
Python PostgreSQL select from table 443 6
Python PostgreSQL insert record into table and get inserted ID 416 6
Python PostgreSQL multiple insert record into table 429 7
Python PostgreSQL insert record into table 439 7
Python PostgreSQL create table 380 7
Python PostgreSQL check database is exist or not 1711 16
Python PostgreSQL create database 411 19
Python PostgreSQL connector get started 403 17
Python Program to Search for a Particular Value in a Binary Tree 370 17
Python Program to Implement Bubble Sort 396 17
Python Program to Implement Queues using Stacks 424 23
Python Program to Reverse a Stack without using Recursion 445 35
Python Program to Implement a Stack using Linked List 579 31
Python Program to Implement Stack Using Two Queues 473 27
Python Program to Implement Stack using One Queue 360 23
Python Program to Implement Queue Data Structure using Linked List 457 29
Python Program to Reverse a Stack using Recursion 433 32
Python Program to Implement a Stack 452 32
Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion 430 26
Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion 366 18
Python Program to Find the Length of the Linked List without using Recursion 340 25
Python Program to Display all the Nodes in a Linked List using Recursion 406 32
Python Program to Create a Linked List & Display the Elements in the List 423 21
Python Program to Find the Length of the Linked List using Recursion 399 20
Python Program to Search for an Element in the Linked List using Recursion 410 27
Python Program to Search for an Element in the Linked List without using Recursion 378 22
Python Program to Find Whether a Number is a Power of Two 374 24
Python Program to Implement Quicksort 412 21
Python Program to Implement Merge Sort 451 25
Python Program to Implement Queue 345 15
Python Program to Print all the Prime Numbers within a Given Range 348 18
Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary 403 25
Python Program to Find the Area of a Triangle Given All Three Sides 399 27
Python Program to Read a File and Capitalize the First Letter of Every Word in the File 561 22
Python Program to Count the Occurrences of a Word in a Text File 587 24
Python Program to Copy the Contents of One File into Another 1329 14
Python Program to Append the Contents of One File to Another File 607 29
Python Program to Count the Number of Lines in a Text File 457 16
Python Program to Count the Number of Words in a Text File 712 24
Python Program to Read the Contents of a File 489 22
Python Program to Read a String from the User and Append it into a File 475 14

Comments