Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: DoublyLinkedList out of control!

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy DoublyLinkedList out of control!

    Ok, my addFirst(T data) method is so screwed up!!! I know I have silly references from changing and rechanging it, but what do I do to get it to work?

    package Assignment4;
     
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
     
     
     
    public class DoublyLinkedList<T>
    {
        private class Node<T>
        {
            private T data;
            private Node<T> next;
                   private Node<T> previous;
     
            public Node(T data,Node<T> next, Node<T> previous)
            {
                this.data = data;
                this.next = next;
                           this.previous=previous;
            }
     
            public T getData()
            {
                return data;
            }
     
            public Node<T> getNext()
            {
                return next;
            }
     
    public Node<T> getPrevious()
    {
    return previous;
    }
     
            public void setNext(Node<T> next)
            {
                this.next = next;
            }      
     
    public void setPrevious(Node<T> previous)
    {
    this.previous = previous;
    }
        }
     
        private Node<T> head;//head of the linked list
           private Node<T> tail; // tail of linked list
        private int size;
       private ImageIcon icon;
       private Icon icon2;
        public DoublyLinkedList()
        {
            head = null;
                    tail = null;
            size = 0;
            icon = new ImageIcon("doh3.jpg");
        }
     
        public String toString()
        {
            String str = "[";
     
            Node<T> curr;
     
            for (curr=head;curr!=null;curr = curr.getNext())
            {
                str = str + curr.getData();
                if (curr.getNext()!=null)
                    str = str + " ";
            }
            str = str + "]";
            return str;
     
    // can this stay the same?  I did copy some of this from the SinglyLinkedList code we got in class.  I don't know exactly how to edit it to make it a DoublyLinkedList.
    // i.e. a list that refers to two things, next and previous, rather than just next.  It makes adding something at the end or close to the end more efficient than a SinglyLinkedList.  
        }
     
        public void addFirst(T data)
        {
          //  Node<T> newnode = new Node<T>(data,head, null);
        // Node<T> newnode;
          //  newnode = head;
            //head = newnode.getPrevious(); // sets the head 
          // newnode = head.next; // puts old head after new head
     
     
     
           // size++;
            if (size == 0)
            {
            	Node<T> newNode = new Node<T>(data,head,tail);
            	head.setPrevious(newNode);
                head = newNode;
            }
           Node<T> newNode = new Node<T>(data,head,null);
            head.setPrevious(newNode);
            head = newNode;
     
            newNode = head;
            head = head.getNext();
     
     
     
                size++; 
        }
     
        public void removeFirst()
        {
               if (size == 0)
    {
            	   JOptionPane pane = new JOptionPane();
            	   pane.setIcon(icon);
    pane.showMessageDialog(null, "Cannot remove from an empty list!", "Invalid removal", JOptionPane.ERROR_MESSAGE);
    pane.setMessageType(JOptionPane.ERROR_MESSAGE);
     
    return;
    }
            Node<T> current = head;
     
            head = head.getNext(); //move head to the next element
            current.setNext(null);
        }
     
        public void addLast(T data)
        {
            //If there are no elements, use the addFirst method
            if (tail == null)
            {
                addFirst(data);
                return;
            }
            /* Create the new Node from the data. Set next to null
             * because this will be the last element and will not
             * have a next. Set previous to tail because tail has
             * not been changed yet and is currently referencing
             * that element that will be directly before this element
             */
            Node<T> newNode = new Node(data,null,tail);
            /* Since the tail variable still references the element
             * directly before the new element, we can set that node's
             * next to our new element.
             */
            tail.setNext(newNode);
            //Set tail to our new Node
            tail = newNode;
            //Increment size
            size++;
        }
     
        public void add(int index,T data)
        {
            int i;
     
            if (index>size)
                return;
     
            if (index < 0)
            	return;
     
            if (head==null)
            {
                addFirst(data);
                return;
            }
     
            if (index == size)
            {
            	addLast(data);
            	return;
            }
     
            //step 1
            Node<T> current;
     
            current = head;
     
            for (i=0;i<index-1;i++)
            {
                current = current.getNext();
            }
     
            //current now refers to object immediately before new node
     
            //step 2
            Node<T> newnode = new Node<T>(data,current.getNext(), current.getPrevious());
     
            //step 3
     
            current.setNext(newnode);
     
            size++;
        }  
     
        public void remove(int index)
        {
            if ((index<0) || (index>=size))
    {
    JOptionPane.showMessageDialog(null, "You cannot remove an out-of-bounds value!", "Invalid removal", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Node<T> current,previous2;
     
            current = head;
            previous2 = null;
     
            for (int i=0;i<index;i++)
            {
                previous2 = current;
                current = current.getNext();
            }
     
            if (index!=0)
                previous2.setNext(current.getNext());
            else
            {
                head = head.getNext();
            }
            size--;
        }
     
        public T get(int i)
        {
        	if (i < 0 || i >= size)
        		return null;
     
        	// How do I get it to return the data at i?
        }
        public T front()
        {
        	if (head == null)
        		return null;
     
        	return(get(0));
        }
     
        public T back()
        {
        	if (tail == null)
        		return null;
     
        	return(get(size - 1));
        }
    public String printAlternate()
    {
    /*
    This method returns a string that has
    the data in the following fashion (assume for this example that the list stores String objects)
    If the list is currently [amit is now here], it will return a string “[amit here is now]”
    If the list is currently [amit is now currently here], it will return a string “[amit here is currently now]”
    */
    }
    }

    Also, why won't my icon show in the corner instead of the error icon? Even when I put it there after the message type, it won't show, though it certainly recognizes it and can find it.

    (

    Especially the addFirst(T data) method, everything falls apart there:

    public void addFirst(T data)
        {
          //  Node<T> newnode = new Node<T>(data,head, null);
        // Node<T> newnode;
          //  newnode = head;
            //head = newnode.getPrevious(); // sets the head 
          // newnode = head.next; // puts old head after new head
     
     
     
           // size++;
            if (size == 0)
            {
            	Node<T> newNode = new Node<T>(data,head,tail);
            	head.setPrevious(newNode);
                head = newNode;
            }
           Node<T> newNode = new Node<T>(data,head,null);
            head.setPrevious(newNode);
            head = newNode;
     
            newNode = head;
            head = head.getNext();
     
     
     
                size++; 
        }

    Like hellllllllllllllllllllllllllllllllllllllllllllllll lllllllllllllllllllllllppppppppppppppppppppppppppp !!!!!!!
    Last edited by javapenguin; October 11th, 2010 at 09:16 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: DoublyLinkedList out of control!

    Quote Originally Posted by javapenguin View Post
    Ok, my addFirst(T data) method is so screwed up!!! I know I have silly references from changing and rechanging it, but what do I do to get it to work?
    Try adding a ton of print statements, or better yet stepping through the code with a debugger, until you understand what's going on. Get it boiled down to a few lines- what you've posted is way more than most people are willing to sift through to help you find an answer.

    Also, saying things like "it doesn't work" or "it falls apart" isn't really helpful. What actually happens? What were you expecting to happen? Why do you think it happens? What did you try? Are you getting any Exceptions? Be as specific as possible.

    And a word to the wise- I'd refrain from adding a bunch of smileys or saying things like, "hellllllllp". It doesn't add anything to the question, and chances are it will annoy somebody who was otherwise willing to offer you a hand.

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    javapenguin (October 12th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: DoublyLinkedList out of control!

    Ok, how do I get the alternateString() method to work?

    It's supposed to return a String where it take the first element in the list and the last, then the second, then the second to last, etc.
    If the list is currently [amit is now here], it will return a string “[amit here is now]”
    If the list is currently [amit is now currently here], it will return a string “[amit here is currently now]”

    I would like it to do this:

    String str = "[+ list.get(0) + " " + list.get(Size() -1) + " " + list.get(1) + " " + list.get(Size() -2) + .... + "]";
    return str;

    But how?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: DoublyLinkedList out of control!

    Quote Originally Posted by javapenguin View Post
    But how?
    What have you tried? Perhaps a loop would suffice? Or you could try to do it recursively.

Similar Threads

  1. Applet using .mp3 control in java
    By ychopade in forum Java Applets
    Replies: 0
    Last Post: July 15th, 2010, 07:26 AM
  2. Control Browser Back Button
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: January 1st, 2010, 08:21 AM
  3. control Browser Back Button
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 30th, 2009, 07:49 AM
  4. process control
    By ttsdinesh in forum Java Native Interface
    Replies: 6
    Last Post: October 27th, 2009, 06:29 PM
  5. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 07:10 AM