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 6 of 6

Thread: boolean linkedlist help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default boolean linkedlist help

    i have a boolean method i must right. imputing the number and if number doesnt exist return false otherwise return true. this is what i have so far.

    public boolean contains(T x)

    {

    Node current = new Node(x, null, null);


    if(current.next != x)
    {

    return false;

    }


    return true;


    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: boolean linkedlist help

    What is the contains() method searching for? The Node x or a Node with the value of x?
    You need to post a bit more code to show what variables are defined in the linkedlist class and the Node class.
    Its not clear what the 3 args to the Node class are.
    How does the next variable in the Node class get a value? The following statement assumes it has a value:
    if(current.next != x)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: boolean linkedlist help

    to make it short my list contains 1,2,3,4,5 nodes. this method is being called in main as contains(list.3); is 3 is in the list which it is then it will print out true. if 3 is not the it will print false; im extending from another class so putting that up will be a waste of time because of how huge it is. the 3 args are (data, beginMarker, EndMarker). but the variables beginmarker and endmarker are private variables that i cannot call. i am not allowed to change around any pre written code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: boolean linkedlist help

    Without definitions for the classes I can't see what is wrong or how to make it better.

    Linked list classes are normally very short. If yours is big for some reason, remove all the code that is not needed for testing this method. I'd think you'd need an add() method to build the list and a show() method to display its contents. And a driver method to call the other methods for testing.
    Leave the rest out.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: boolean linkedlist help

    the class we are extending from is indeed long for some odd reason. thats called from two classes im developing. i understand what you are talking about how its hard for you to go on what i have. most examples i have found online have a node as a passing arguement. i cant seem to find anything else.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: boolean linkedlist help

    Sorry, without the code I can't make any suggestions, other than those in post#2
    If you post any code, be sure to strip out any unrelated code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Arraylist and LinkedList
    By vilkas123 in forum Collections and Generics
    Replies: 1
    Last Post: November 3rd, 2012, 02:20 AM
  2. [SOLVED] Sorting LinkedList
    By wdh in forum What's Wrong With My Code?
    Replies: 28
    Last Post: April 29th, 2012, 12:52 PM
  3. addInOrder LinkedList
    By PeskyToaster in forum Collections and Generics
    Replies: 1
    Last Post: April 6th, 2012, 06:16 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:56 AM
  5. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM