Skip to content

Stack using double linked list #1413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions data_structures/stacks/stack_using_dll.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Member

@cclauss cclauss Oct 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insert doctests here:

    """
    >>> stack = Stack()
    >>> stack.isEmpty()
    True
    >>> stack.printstack()
    stack elements are:
    >>> for i in range(4):
    ...     stack.push(i)
    ...
    >>> stack.isEmpty()
    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->
    """

And then locally do python3 -m doctest stack_using_dll.py and python3 -m doctest -v stack_using_dll.py and make sure those doctests pass.

# 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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_empty() is more Pythonic.


if self.head is None:
return True
else:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 63 put return self.head is None and then delete lines 64-67.



# Function to print the stack
def printstack(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print_stack() is more Pythonic.


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())