Double-ended queue in Java
Register

We are the best invite forum on the internet! Here you will find free invites, free seedboxes, free bonuses, and much more. Our members know the true meaning of sharing and have created a truly global bittorent community! Our site has the most up to date information on all private trackers and our members will guide you and introduce you to this truly secretive and enlightened club. Ready to get started? Register now!


Results 1 to 4 of 4
Like Tree4Likes
  • 3 Post By mate88
  • 1 Post By chewbiscuit

Thread: Double-ended queue in Java

  1. #1

    Join Date
    May 2011
    Location
    Finland
    Posts
    1,236

    Default Double-ended queue in Java

    I managed to code it myself after all... here's my solution:
    class Deque {

    private Node front, rear;
    private int counter;

    public Deque() {
    front = null;
    rear = null;
    counter = 0;
    }

    public void push(Object x) {
    if (front==null) {
    Node T = new Node(x, null, null);
    rear = T;
    front = T;
    counter++;
    } else {
    Node T = new Node(x, front, null);
    front.next=T;
    front = T;
    counter++;
    }

    }

    public Object pop() {
    if (counter==0) {
    return null;
    } else if (counter==1) {
    counter=0;
    Object x = front.data;
    rear = null;
    front = null;
    return x;
    }
    else {
    Object x = front.data;
    front=front.prev;
    rear.prev = front;
    counter--;
    return x;
    }
    }

    public void inject (Object x) {
    if (rear==null) {
    Node T = new Node(x, null, null);
    rear = T;
    front = T;
    counter++;
    } else {
    Node T = new Node(x, null, rear);
    rear.prev=T;
    rear = T;
    counter++;
    }
    }

    public Object eject() {
    if (counter==0) {
    rear=null;
    front=null;
    return null;
    } else if (counter==1) {
    counter=0;
    Object x = rear.data;
    rear=null;
    front=null;
    return x;
    }
    else {
    Object x = rear.data;
    rear=rear.next;
    front.next = rear;
    counter--;
    return x;
    }
    }

    class Node {

    Node prev;
    Node next;
    Object data;



    Node(Object d, Node p, Node n) {
    next = n;
    prev = p;
    data = d;
    }

    }

    }

    I managed to code it so that all operations are done in constant time, O(1) complexity, but it should be possible implement it without the counter. I also think that there are more unnecessary things in my solution and I would like to know how you can make it more compact.
    Last edited by mate88; September 24th, 2011 at 01:18 PM.
    ASH, jigglypuff and chewbiscuit like this.


  2. To remove ads become VIP. Inquire about advertising here.
  3. #2

    Join Date
    Sep 2011
    Location
    USA!
    Posts
    168

    Default

    Why are you using Objects everywhere and not generics?

  4. #3

    Join Date
    May 2011
    Location
    Finland
    Posts
    1,236

    Default

    Quote Originally Posted by roamer View Post
    Why are you using Objects everywhere and not generics?
    I'm not familiar with the concept and this was an assignment I had to write from scratch. I'm still learning java. This was a task given at a course on data structures.

  5. #4

    Join Date
    Feb 2012
    Location
    Boston
    Posts
    19

    Default

    This thread is old and I'm guessing you've moved on by now. But I'll add my thoughts in case anyone is interested.

    Definitely seems over-complicated to me. I would do something like (e.g.)
    public void push(Object x) {
    Node T = new Node(x, null, null);
    T.next = front;
    front = T;
    }
    And the other functions are similar. With practice, you will be able to write code that addresses the general sense of the problem, rather than addressing cases individually. Keep it up -- you are off to a good start.

    You are correct that the counter is unnecessary. However, you should consider what would happen if you want to ask how many elements are in the deque, and you want the computation of the answer to be O(1). Updating a count variable would help you with that.
    mate88 likes this.

Similar Threads

  1. Torrent Related...Come On In For A Good Laugh
    By minimafiaaj in forum General Discussion
    Replies: 24
    Last Post: November 9th, 2008, 07:20 PM
  2. [Old] Insane Team (INS) Review
    By Cruel in forum Tracker Reviews
    Replies: 0
    Last Post: December 5th, 2007, 04:32 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •