Results 1 to 8 of 8
  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Question LinkedList problem

    Hi ..
    this code is correct:
    public class LinkedList
    {
    // reference to the head node.
    private Node head;
    private int listCount;

    // LinkedList constructor
    public LinkedList()
    {
    // this is an empty list, so the reference to the head node
    // is set to a new node with no data
    head = new Node(null);
    listCount = 0;
    }

    public void add(Object data)
    // post: appends the specified element to the end of this list.
    {
    Node temp = new Node(data);
    Node current = head;
    // starting at the head node, crawl to the end of the list
    while(current.getNext() != null)
    {
    current = current.getNext();
    }
    // the last node's "next" reference set to our new node
    current.setNext(temp);
    listCount++;// increment the number of elements variable
    }

    public void add(Object data, int index)
    // post: inserts the specified element at the specified position in this list.
    {
    Node temp = new Node(data);
    Node current = head;
    // crawl to the requested index or the last element in the list,
    // whichever comes first
    for(int i = 1; i < index && current.getNext() != null; i++)
    {
    current = current.getNext();
    }
    // set the new node's next-node reference to this node's next-node reference
    temp.setNext(current.getNext());
    // now set this node's next-node reference to the new node
    current.setNext(temp);
    listCount++;// increment the number of elements variable
    }

    public Object get(int index)
    // post: returns the element at the specified position in this list.
    {
    // index must be 1 or higher
    if(index <= 0)
    return null;

    Node current = head.getNext();
    for(int i = 1; i < index; i++)
    {
    if(current.getNext() == null)
    return null;

    current = current.getNext();
    }
    return current.getData();
    }

    public boolean remove(int index)
    // post: removes the element at the specified position in this list.
    {
    // if the index is out of range, exit
    if(index < 1 || index > size())
    return false;

    Node current = head;
    for(int i = 1; i < index; i++)
    {
    if(current.getNext() == null)
    return false;

    current = current.getNext();
    }
    current.setNext(current.getNext().getNext());
    listCount--; // decrement the number of elements variable
    return true;
    }

    public int size()
    // post: returns the number of elements in this list.
    {
    return listCount;
    }

    public String toString()
    {
    Node current = head.getNext();
    String output = "";
    while(current != null)
    {
    output += "[" + current.getData().toString() + "]";
    current = current.getNext();
    }
    return output;
    }

    private class Node
    {
    // reference to the next node in the chain,
    // or null if there isn't one.
    Node next;
    // data carried by this node.
    // could be of any type you need.
    Object data;


    // Node constructor
    public Node(Object _data)
    {
    next = null;
    data = _data;
    }

    // another Node constructor if we want to
    // specify the node to point to.
    public Node(Object _data, Node _next)
    {
    next = _next;
    data = _data;
    }

    // these methods should be self-explanatory
    public Object getData()
    {
    return data;
    }

    public void setData(Object _data)
    {
    data = _data;
    }

    public Node getNext()
    {
    return next;
    }

    public void setNext(Node _next)
    {
    next = _next;
    }
    }
    }



    However, my main method is my problem. this code,it will ask the user data.

    import javax.swing.JOptionPane;
    public class List {
    public static void main(String []args) {

    LinkedList list=new LinkedList();

    char ans='y';
    int choice;
    Object data;
    Object next;

    System.out.println("\t\tWhat do you want to do?");
    System.out.print("\n");
    System.out.println("1.Add data at the end of list");
    System.out.println("2.Add data at specific index");
    System.out.println("3.Remove data from list");
    System.out.println("4. Print the list");

    while(ans=='y') {


    choice=Integer.parseInt(JOptionPane.showInputDialo g("Enter your choice"));


    if(choice==1) {

    data=(JOptionPane.showInputDialog("Enter your data"));
    list.add(data);
    System.out.println(list);

    }else
    if(choice==2){
    data=(JOptionPane.showInputDialog("Enter your data"));
    int index=Integer.parseInt(JOptionPane.showInputDialog ("Enter # of index"));
    list.add(data, index);
    System.out.println(list);

    }else
    if(choice==3) {
    int index=Integer.parseInt(JOptionPane.showInputDialog ("Index to be removed"));
    list.remove(index);


    //if(list.remove(index)) {
    //System.out.println("\n\n You have successfully remove the index");

    // }else{

    // System.out.println("\n\n You loose the turn");
    // System.out.println(list);

    }
    if(choice==4) {
    list.size();
    System.out.println(list);
    }

    String x=(JOptionPane.showInputDialog("Do you want to continue?"));
    ans=x.charAt(0);
    }
    }
    }

    The comment is the problem because there is an error.I want to display if the element have succesfully remove otherwise it will display you loose the turn.
    Thank you. Your help is very appreciated.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    choice=Integer.parseInt(JOptionPane.showInputDialo g(null,"Enter your choice"));
    try to use with null )

  3. #3
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    "//if(list.remove(index)) {
    //System.out.println("\n\n You have successfully remove the index");

    // }else{

    // System.out.println("\n\n You loose the turn");
    // System.out.println(list);"


    Actually, this part is my problem because when i remove some index the output is wrong
    Example:
    When the user enter 5 data:
    a b c d e
    If ever the user want to delete or remove some index, it will display "You have successfully remove the index" otherwise "You loose the turn"
    Example:
    Enter index to be remove:
    2
    so, it must the "b" will delete and display " you have....."
    but then, the wrong is the "c" will also delete.So the output will
    "a d e" that actually is not the right output it should be:
    "a c d e".

  4. #4
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    Continuation:
    If it has only one remaining index ,
    Example:
    a - the remaining index
    then,the user want to delete it so that the list now is empty.

    Enter index to be remove:
    1
    so, "a" now will delete but then the wrong is, it will display " Ypu loose a turn". It must be " You have succesfully remove the index". What should i do?Pls help me..

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default size?

    Java Code:
    if(choice==4) {
    list.size();
    System.out.println(list);
    }
    What does list.size() do? It's not assigning a value to a variable and it's not being used.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default It's working...

    Example:
    Enter index to be remove:
    2
    so, it must the "b" will delete and display " you have....."
    but then, the wrong is the "c" will also delete.So the output will
    "a d e" that actually is not the right output it should be:
    "a c d e".
    Remember... you are working with indexes. Indexes start with 0, therefore if you remove index 2, you are removing the third element (0.1.2) in the linked list.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    danver_2009 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    if(choice==4) {
    list.size();
    System.out.println(list);
    }

    actually this code is just print the list after the user remove an index.

  8. #8
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    Im sorry for that, from now on i will tell thank you to all the people who help me for solving any problem. Im soory again. Thank you for the help..

Similar Threads

  1. LinkedList-java
    By kamali in forum New To Java
    Replies: 2
    Last Post: 09-26-2008, 04:29 PM
  2. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 05:57 AM
  3. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM
  4. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 AM

Posting Permissions

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