From acbb0648d68a3e03da2d2ab7026719df18ce274f Mon Sep 17 00:00:00 2001 From: Dhakad9 <53108891+Dhakad9@users.noreply.github.com> Date: Mon, 21 Oct 2019 22:00:22 +0530 Subject: [PATCH 1/5] Stack using double linked list --- data_structures/stacks/stack_using_dll.py | 119 ++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 data_structures/stacks/stack_using_dll.py diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py new file mode 100644 index 000000000000..06863a1598e2 --- /dev/null +++ b/data_structures/stacks/stack_using_dll.py @@ -0,0 +1,119 @@ +# A complete working Python program to demonstrate all +# stack operations using a doubly linked list + +# Node class +class Node: + +# Function to initialise the node object + def __init__(self, data): + self.data = data # Assign data + self.next = None # Initialize next as null + self.prev = None # Initialize prev as null + +# Stack class contains a Node object +class Stack: + # Function to initialize head + def __init__(self): + self.head = None + +# Function to add an element data in the stack + def push(self, data): + + if self.head is None: + self.head = Node(data) + else: + new_node = Node(data) + self.head.prev = new_node + new_node.next = self.head + new_node.prev = None + self.head = new_node + + +# Function to pop top element and return the element from the stack + def pop(self): + + if self.head is None: + return None + else: + temp = self.head.data + self.head = self.head.next + self.head.prev = None + return temp + + +# Function to return top element in the stack + def top(self): + + return self.head.data + + +# Function to return the size of the stack + def size(self): + + temp = self.head + count = 0 + while temp is not None: + count = count + 1 + temp = temp.next + return count + + +# Function to check if the stack is empty or not + def isEmpty(self): + + if self.head is None: + return True + else: + return False + + +# Function to print the stack + def printstack(self): + + print("stack elements are:") + temp = self.head + while temp is not None: + print(temp.data, end ="->") + temp = temp.next + + +# Code execution starts here +if __name__=='__main__': + +# Start with the empty stack + stack = Stack() + +# Insert 4 at the beginning. So stack becomes 4->None + print("Stack operations using Doubly LinkedList") + stack.push(4) + +# Insert 5 at the beginning. So stack becomes 4->5->None + stack.push(5) + +# Insert 6 at the beginning. So stack becomes 4->5->6->None + stack.push(6) + +# Insert 7 at the beginning. So stack becomes 4->5->6->7->None + stack.push(7) + +# Print the stack + stack.printstack() + +# Print the top element + print("\nTop element is ", stack.top()) + +# Print the stack size + print("Size of the stack is ", stack.size()) + +# pop the top element + stack.pop() + +# pop the top element + stack.pop() + +# two elements are popped +# Print the stack + stack.printstack() + +# Print True if the stack is empty else False + print("\nstack is empty:", stack.isEmpty()) \ No newline at end of file From 35e545ebba49286ec4696219769fa8cdc0e18c12 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 21 Dec 2019 02:18:46 +0100 Subject: [PATCH 2/5] Test with doctests --- data_structures/stacks/stack_using_dll.py | 131 +++++++++++----------- 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py index 06863a1598e2..9b163f6f558d 100644 --- a/data_structures/stacks/stack_using_dll.py +++ b/data_structures/stacks/stack_using_dll.py @@ -1,24 +1,42 @@ # A complete working Python program to demonstrate all # stack operations using a doubly linked list -# Node class class Node: - -# Function to initialise the node object def __init__(self, data): self.data = data # Assign data self.next = None # Initialize next as null self.prev = None # Initialize prev as null -# Stack class contains a Node object -class Stack: - # Function to initialize head +class Stack: + """ + >>> stack = Stack() + >>> stack.is_empty() + True + >>> stack.print_stack() + stack elements are: + >>> for i in range(4): + ... stack.push(i) + ... + >>> stack.is_empty() + False + >>> stack.printstack() + stack elements are: + 3->2->1->0-> + >>> stack.top() + 3 + >>> stack.size() + 4 + >>> stack.pop() + 3 + >>> stack.printstack() + stack elements are: + 2->1->0-> + """ def __init__(self): self.head = None -# Function to add an element data in the stack - def push(self, data): - + def push(self, data): + """add a Node to the stack""" if self.head is None: self.head = Node(data) else: @@ -26,12 +44,10 @@ def push(self, data): self.head.prev = new_node new_node.next = self.head new_node.prev = None - self.head = new_node + self.head = new_node - -# Function to pop top element and return the element from the stack - def pop(self): - + def pop(self): + """pop the top element off the stack""" if self.head is None: return None else: @@ -40,36 +56,22 @@ def pop(self): self.head.prev = None return temp - -# Function to return top element in the stack - def top(self): - - return self.head.data - - -# Function to return the size of the stack - def size(self): - + def top(self): + """return the top element of the stack""" + return self.head.data + + def __len__(self): temp = self.head count = 0 while temp is not None: - count = count + 1 + count += 1 temp = temp.next return count - - -# Function to check if the stack is empty or not - def isEmpty(self): - - if self.head is None: - return True - else: - return False - - -# Function to print the stack - def printstack(self): - + + def is_empty(self): + return self.head is None: + + def print_stack(self): print("stack elements are:") temp = self.head while temp is not None: @@ -80,40 +82,39 @@ def printstack(self): # Code execution starts here if __name__=='__main__': -# Start with the empty stack - stack = Stack() + # Start with the empty stack + stack = Stack() -# Insert 4 at the beginning. So stack becomes 4->None - print("Stack operations using Doubly LinkedList") - stack.push(4) + # Insert 4 at the beginning. So stack becomes 4->None + print("Stack operations using Doubly LinkedList") + stack.push(4) -# Insert 5 at the beginning. So stack becomes 4->5->None - stack.push(5) + # Insert 5 at the beginning. So stack becomes 4->5->None + stack.push(5) -# Insert 6 at the beginning. So stack becomes 4->5->6->None - stack.push(6) + # Insert 6 at the beginning. So stack becomes 4->5->6->None + stack.push(6) -# Insert 7 at the beginning. So stack becomes 4->5->6->7->None - stack.push(7) + # Insert 7 at the beginning. So stack becomes 4->5->6->7->None + stack.push(7) -# Print the stack - stack.printstack() + # Print the stack + stack.print_stack() -# Print the top element - print("\nTop element is ", stack.top()) + # Print the top element + print("\nTop element is ", stack.top()) -# Print the stack size - print("Size of the stack is ", stack.size()) + # Print the stack size + print("Size of the stack is ", stack.size()) -# pop the top element - stack.pop() + # pop the top element + stack.pop() -# pop the top element - stack.pop() + # pop the top element + stack.pop() -# two elements are popped -# Print the stack - stack.printstack() + # two elements have now been popped off + stack.print_stack() -# Print True if the stack is empty else False - print("\nstack is empty:", stack.isEmpty()) \ No newline at end of file + # Print True if the stack is empty else False + print("\nstack is empty:", stack.is_empty()) From 222a92715cdb2bfb2a090df5907528b35ea5bbb6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 21 Dec 2019 02:31:46 +0100 Subject: [PATCH 3/5] Update stack_using_dll.py --- data_structures/stacks/stack_using_dll.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py index 9b163f6f558d..4c64f830eee8 100644 --- a/data_structures/stacks/stack_using_dll.py +++ b/data_structures/stacks/stack_using_dll.py @@ -69,7 +69,7 @@ def __len__(self): return count def is_empty(self): - return self.head is None: + return self.head is None def print_stack(self): print("stack elements are:") From 00049386d7a2fe2fb5b015124904f022dd69d249 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 21 Dec 2019 02:37:42 +0100 Subject: [PATCH 4/5] Update stack_using_dll.py --- data_structures/stacks/stack_using_dll.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py index 4c64f830eee8..89169aa3148f 100644 --- a/data_structures/stacks/stack_using_dll.py +++ b/data_structures/stacks/stack_using_dll.py @@ -19,7 +19,7 @@ class Stack: ... >>> stack.is_empty() False - >>> stack.printstack() + >>> stack.print_stack() stack elements are: 3->2->1->0-> >>> stack.top() @@ -28,7 +28,7 @@ class Stack: 4 >>> stack.pop() 3 - >>> stack.printstack() + >>> stack.print_stack() stack elements are: 2->1->0-> """ From 297849c52ce31f5dfaa5a36ecf2804b8d8f3fb51 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 21 Dec 2019 02:42:57 +0100 Subject: [PATCH 5/5] Update stack_using_dll.py --- data_structures/stacks/stack_using_dll.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py index 89169aa3148f..10e4c067f6f3 100644 --- a/data_structures/stacks/stack_using_dll.py +++ b/data_structures/stacks/stack_using_dll.py @@ -24,7 +24,7 @@ class Stack: 3->2->1->0-> >>> stack.top() 3 - >>> stack.size() + >>> len(stack) 4 >>> stack.pop() 3 @@ -105,7 +105,7 @@ def print_stack(self): print("\nTop element is ", stack.top()) # Print the stack size - print("Size of the stack is ", stack.size()) + print("Size of the stack is ", len(stack)) # pop the top element stack.pop()