From 896d03f76ce497dea96f667831b92071f917e5f2 Mon Sep 17 00:00:00 2001 From: rakesh kumar Date: Thu, 10 May 2018 22:53:16 +0530 Subject: [PATCH] data structure queue --- DataStructure/queue.py | 34 ++++++++++++++++++++++++++++++++++ DataStructure/stack.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 DataStructure/queue.py diff --git a/DataStructure/queue.py b/DataStructure/queue.py new file mode 100644 index 0000000..def1c7f --- /dev/null +++ b/DataStructure/queue.py @@ -0,0 +1,34 @@ +#function to add element at the end of list +def add_element(stack,x): + stack.append(x) +#function to remove last element from list +def delete_element(stack): + n = len(stack) + if(n<=0): + print("Queue empty....Deletion not possible") + else: + del(stack[0]) + +#function to display stack entry +def display(stack): + if len(stack)<=0: + print("Queue empty...........Nothing to display") + for i in stack: + print(i,end=" ") +#main program starts from here +x=[] +choice=0 +while (choice!=4): + print("\n\tQueue menu \n\n\t1. Add Element \n\t2. Delete Element \n\t3. Display \n\t4. Exit") + choice = int(input("\n Enter your choice : ")) + + if(choice==1): + value = int(input("Enter value : ")) + add_element(x,value) + if(choice==2): + delete_element(x) + if(choice==3): + display(x) + if(choice==4): + print("You selected to close this program") + diff --git a/DataStructure/stack.py b/DataStructure/stack.py index 8f0e3b8..7c6445b 100644 --- a/DataStructure/stack.py +++ b/DataStructure/stack.py @@ -1,4 +1,34 @@ +#function to add element at the end of list def push(stack,x): stack.append(x) +#function to remove last element from list def pop(stack): - del(stack[]) + n = len(stack) + if(n<=0): + print("Stack empty....Pop not possible") + else: + stack.pop() + +#function to display stack entry +def display(stack): + if len(stack)<=0: + print("Stack empty...........Nothing to display") + for i in stack: + print(i,end=" ") +#main program starts from here +x=[] +choice=0 +while (choice!=4): + print("\n\t\t\t stack menu \n\t1. push \n\t2. pop \n\t3. Display \n\t4. Exit") + choice = int(input("Enter your choice")) + + if(choice==1): + value = int(input("Enter value ")) + push(x,value) + if(choice==2): + pop(x) + if(choice==3): + display(x) + if(choice==4): + print("You selected to close this program") +