Skip to content

adding some code for stack and queue in python #771

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
95 changes: 95 additions & 0 deletions contents/stacks_and_queues/code/python/stack_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Node:
def __init__(self, val):
self.value = val
self.next = None
Copy link
Member

Choose a reason for hiding this comment

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

Why do you want to recreate a linked list structure inside Python?

Copy link
Author

Choose a reason for hiding this comment

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

ahh, I thought this folder for basic implementation in python and we can contribute in any language, so that's why i tried to make one.
well in python itself already has list() which we can used simply, so yeah back to my first reason..

Copy link
Member

Choose a reason for hiding this comment

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

I'm still not sure this a good idea, but I'll let you use it



class Stack:
def __init__(self):
self.stack_list = None

def push(self, val):
if self.stack_list is None:
self.stack_list = Node(val)
else:
temp = Node(val)
temp.next = self.stack_list
self.stack_list = temp
Copy link
Member

Choose a reason for hiding this comment

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

The way you push to the stack seems weird... You push to the beginning, which is not usual. Why don't you simply use a list object instead of your linked list?

Copy link
Author

Choose a reason for hiding this comment

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

ahh this is back to the first reason.. i'm just implementing the basic way with python, and yeah I thought u're one of the reviewer, bcs I saw your name merged it, sorry abt that

Copy link
Member

Choose a reason for hiding this comment

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

I now have write access to the repo, so I can now merge if I want. Things change a lot in a year (sorry about the delay) 😄


def pop(self):
if self.stack_list is None:
raise ValueError("Stack is empty")
else:
pop_val = self.stack_list.value
self.stack_list = self.stack_list.next
return pop_val

def get_top(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor thing: maybe drop get_ and just call it top - same with get_front -> front?

return self.stack_list.value

def print(self):
print("Current stack list : ")
temp = self.stack_list
while temp.next:
print(temp.value)
temp = temp.next
print(temp.value)


class Queue:
def __init__(self):
self.queue_list = None

def enqueue(self, val):
temp = Node(val)
if self.queue_list is None:
self.queue_list = temp
else:
curr = self.queue_list
while curr.next:
curr = curr.next
curr.next = temp

def dequeue(self):
if self.queue_list is None:
raise ValueError("Queue is empty")
else:
temp = self.queue_list
dequeue_val = self.queue_list.value
self.queue_list = temp.next
return dequeue_val

def get_front(self):
curr = self.queue_list
while curr.next:
curr = curr.next
return curr.value

def print(self):
temp = self.queue_list
while temp.next:
print(temp.value)
temp = temp.next
print(temp.value)
Comment on lines +68 to +73
Copy link
Member

Choose a reason for hiding this comment

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

You might want to do the same as with the Stack.print method, and say what you are printing.

A better (I think) way to implement the printing would be to provide a __str__ method, and let Python handle the printing when doing print(example_stack) and print(example_queue)



example_stack = Stack()
example_stack.push(1)
example_stack.push(2)
example_stack.push(3)
example_stack.print()
print("you just pop ", example_stack.pop())
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
print("you just pop ", example_stack.pop())
print("you just popped", example_stack.pop())


example_stack.push(4)
example_stack.print()
print("current top => ", example_stack.get_top())

example_queue = Queue()
example_queue.enqueue(1)
example_queue.enqueue(2)
example_queue.enqueue(3)
example_queue.print()
example_queue.dequeue()
example_queue.enqueue(4)
example_queue.print()
print("current front => ", example_queue.get_front())
6 changes: 6 additions & 0 deletions contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ The notation for this depends on the language you are using. Queues, for example

## License

## Example Code
{% method %}
Copy link
Contributor

@henrikac henrikac Oct 19, 2021

Choose a reason for hiding this comment

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

I could be wrong but are stack and queue not split into two separate examples?

{% method %}
# add stack example
{% endmethod %}

{% method %}
# add queue example
{% endblock %}

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we'll probably have to change that to fit the current published format indeed.

{% sample lang="py" %}
[import, lang:"python"](code/python/stack_queue.py)
{% endmethod %}

##### Code Examples

The code examples are licensed under the MIT license (found in [LICENSE.md](https://github.com/algorithm-archivists/algorithm-archive/blob/master/LICENSE.md)).
Expand Down