Added dequeue.java and dequeue.py#3302
Conversation
soc221b
left a comment
There was a problem hiding this comment.
Please rename file/directory to lowercase.
| System.out.println("Underflow"); | ||
| return -1; | ||
| } | ||
| int front_value=front.getValue(); |
There was a problem hiding this comment.
Please check variable naming: lowerCamelCase.
| } | ||
| int front_value=front.getValue(); | ||
| Node newFront = front.getNext(); | ||
| if(newFront!=null){ |
| private Node prev; | ||
| private Node next; | ||
|
|
||
| public Node() { |
There was a problem hiding this comment.
Do we need this constructor?
I think this will be implicit error. Suppose that if someone compare the value, and the value is set to -1.
| rear.setNext(n); | ||
| n.setPrevious(rear); | ||
| rear = n; | ||
|
|
| int frontValue = front.getValue(); | ||
| Node newFront = front.getNext(); | ||
|
|
||
| if(newFront!=null) { |
| int rearValue = rear.getValue(); | ||
| Node newRear = rear.getPrevious(); | ||
|
|
||
| if(newRear!=null){ |
|
|
||
| public int removeRear() { | ||
| if (front == null) { | ||
| System.out.println("Underflow"); |
There was a problem hiding this comment.
In my opinion, this should implement with Exception.
|
|
||
| public static void main(String args[]) { | ||
| Dequeue dq = new Dequeue(); | ||
| for(int i = 1; i <= 10; ++i){ |
There was a problem hiding this comment.
Every condition should be added space after the keyword.
| Dequeue dq = new Dequeue(); | ||
| for(int i = 1; i <= 10; ++i){ | ||
| dq.insertAtFront(i); | ||
| dq.insertAtRear(-1*i); |
There was a problem hiding this comment.
Should be -1 * i.
Add space around operators
|
|
||
| for i in range(1,11): | ||
| print(dq.remove_front()) | ||
| print(dq.remove_rear()) |
| dq.insert_at_rear(-1*i); | ||
|
|
||
| for i in range(1,11): | ||
| print(dq.remove_front()) |
|
|
||
| if __name__ == '__main__': | ||
| dq = Dequeue(); | ||
| for i in range(1,11): |
| public class Dequeue { | ||
|
|
||
| private Node front = null; | ||
| private Node rear = null; |
There was a problem hiding this comment.
The usual notation is head and tail.
|
|
||
| public int removeFront() { | ||
| try { | ||
| int frontValue = front.getValue(); |
| front = newFront; | ||
| return frontValue; | ||
| } | ||
| catch (Exception e) { |
There was a problem hiding this comment.
A class should not catch an exception. That is the responsibility of the client. If you want to translate an exception, then catch it here, and rethrow your own copy. Don't print to the console.
| } | ||
| catch (Exception e) { | ||
| System.out.println("Underflow"); | ||
| return -1; |
There was a problem hiding this comment.
If you throw an exception, you won't need to return anything.
| rear = newRear; | ||
| return rearValue; | ||
| } | ||
| catch(Exception e) { |
| front = newFront; | ||
| return frontValue; | ||
| } | ||
| catch (Exception e) { |
There was a problem hiding this comment.
I disagree with this approach. Throw an exception for an empty container, as that is the idiomatic way to do things. Don't print to the console. Classes shouldn't even be catching exceptions. Let the client handle it. (It's less work for you anyways).
| rear = newRear; | ||
| return rearValue; | ||
| } | ||
| catch(Exception e) { |
| # Part of Cosmos by OpenGenus Foundation | ||
|
|
||
| # Implementation using lists | ||
| class Dequeue: |
There was a problem hiding this comment.
I think we need front() and rear() also.
There was a problem hiding this comment.
not head and tail as suggested in java's code?
| container = [] | ||
| self.__container = container | ||
|
|
||
| def insert_at_front(self, d): |
There was a problem hiding this comment.
Maybe push_front, push_back, pop_front, and pop_back is more prefer.
|
|
||
| for i in range(1, 11): | ||
| print(dq.remove_front()) | ||
| print(dq.remove_rear()) |
There was a problem hiding this comment.
Remove trailing spaces at col: 37, and 38.
Fixes issue: : #3243
Changes: : Added dequeue's implementation in java