Skip to content

Added dequeue.java and dequeue.py#3302

Open
varshika03 wants to merge 14 commits intoOpenGenus:masterfrom
varshika03:master
Open

Added dequeue.java and dequeue.py#3302
varshika03 wants to merge 14 commits intoOpenGenus:masterfrom
varshika03:master

Conversation

@varshika03
Copy link

Fixes issue: : #3243

Changes: : Added dequeue's implementation in java

Copy link

@soc221b soc221b left a comment

Choose a reason for hiding this comment

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

Please rename file/directory to lowercase.

System.out.println("Underflow");
return -1;
}
int front_value=front.getValue();
Copy link

Choose a reason for hiding this comment

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

}
int front_value=front.getValue();
Node newFront = front.getNext();
if(newFront!=null){
Copy link

Choose a reason for hiding this comment

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

Please check spaces

private Node prev;
private Node next;

public Node() {
Copy link

Choose a reason for hiding this comment

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

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.

@varshika03 varshika03 changed the title Added Dequeue.java Added dequeue.java and dequeue.py Feb 24, 2018
rear.setNext(n);
n.setPrevious(rear);
rear = n;

Copy link

Choose a reason for hiding this comment

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

Unnecessary line.

int frontValue = front.getValue();
Node newFront = front.getNext();

if(newFront!=null) {
Copy link

Choose a reason for hiding this comment

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

Add space between if and parenthesis.

int rearValue = rear.getValue();
Node newRear = rear.getPrevious();

if(newRear!=null){
Copy link

Choose a reason for hiding this comment

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

As above.


public int removeRear() {
if (front == null) {
System.out.println("Underflow");
Copy link

Choose a reason for hiding this comment

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

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){
Copy link

Choose a reason for hiding this comment

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

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);
Copy link

Choose a reason for hiding this comment

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

Should be -1 * i.
Add space around operators


for i in range(1,11):
print(dq.remove_front())
print(dq.remove_rear())
Copy link

Choose a reason for hiding this comment

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

Remove spaces.

dq.insert_at_rear(-1*i);

for i in range(1,11):
print(dq.remove_front())
Copy link

Choose a reason for hiding this comment

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

Remove spaces.


if __name__ == '__main__':
dq = Dequeue();
for i in range(1,11):
Copy link

Choose a reason for hiding this comment

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

Add space after comma.

public class Dequeue {

private Node front = null;
private Node rear = null;
Copy link
Member

Choose a reason for hiding this comment

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

The usual notation is head and tail.


public int removeFront() {
try {
int frontValue = front.getValue();
Copy link
Member

Choose a reason for hiding this comment

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

Indentation fix.

front = newFront;
return frontValue;
}
catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

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

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;
Copy link
Member

Choose a reason for hiding this comment

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

If you throw an exception, you won't need to return anything.

rear = newRear;
return rearValue;
}
catch(Exception e) {
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

front = newFront;
return frontValue;
}
catch (Exception e) {
Copy link
Member

@arnavb arnavb Feb 25, 2018

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Same here.

# Part of Cosmos by OpenGenus Foundation

# Implementation using lists
class Dequeue:
Copy link

@soc221b soc221b Feb 26, 2018

Choose a reason for hiding this comment

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

I think we need front() and rear() also.

Copy link
Author

Choose a reason for hiding this comment

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

not head and tail as suggested in java's code?

Copy link

@soc221b soc221b Feb 26, 2018

Choose a reason for hiding this comment

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

Sure, it is prefer.

Copy link

Choose a reason for hiding this comment

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

But it is about get-functions above.

container = []
self.__container = container

def insert_at_front(self, d):
Copy link

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Remove trailing spaces at col: 37, and 38.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments