-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
|
||||||
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
Amaras marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
def get_top(self): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor thing: maybe drop |
||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to do the same as with the A better (I think) way to implement the printing would be to provide a |
||||||
|
||||||
|
||||||
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()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,12 @@ The notation for this depends on the language you are using. Queues, for example | |
|
||
## License | ||
|
||
## Example Code | ||
{% method %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 %} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)). | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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..
There was a problem hiding this comment.
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