Results 1 to 11 of 11
  1. #1
    Srcee is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default Single linked lists - help

    Java can't compile the next program segment because it should return Object. What can I do to make it compile? May I write return null after if?

    public Object deleteFirst(){
    if (first != null){
    SLLNode tmp = first;
    first = first.succ;
    return tmp.element;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Srcee is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What do you want it to return as a default if first == null?
    The code is for deleting a first node in a single linked list. If first==null it should throw Exception.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Srcee View Post
    The code is for deleting a first node in a single linked list. If first==null it should throw Exception.
    Then do that.

    Java Code:
    if (first == null) {
      throw new // my exception...
    }
    // ... rest of code of method goes here.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are not throwing that exception. Think about the error message you are getting.
    Java is smart enough to realize that your if test might return false meaning your if block is never entered. In that case there is nothing to return. But the method says that it returns an Object.

  6. #6
    Srcee is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Then do that.

    Java Code:
    if (first == null) {
      throw new // my exception...
    }
    // ... rest of code of method goes here.
    Thank you a lot ;)

  7. #7
    Srcee is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    I do not want to open a new topic, so I will ask here. Can you send mi a link to a site with solved exercises about Single linked lists, Double linked lists, stacks, queues, decks, hash tables and maps?

    Tnx

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Trail: Collections (The Java™ Tutorials)
    Not really solved exercises though.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Srcee is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Not really solved exercises though.

    Oh but I need solved exercises, for example:"write a class named Playlist as Double linked list that should allow i don't know some functions". Or "employers in a company as a tree". Help pls

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I don't know such a site, I've never needed that.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    Srcee is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    I need help again. The next program segment is for adding a first node in a Double linked list:

    public void insertFirst(Object o){
    DLLNode ins = new DLLNode(o, first, null);//1
    if (first == null)
    last = ins;
    else
    first.succ.prev = first;//2
    first = ins;
    }

    Can someone explain me:
    what does the line marked with //1 and //2 mean? Possibly with pictures :) I mean, I know what they mean but I can't see how that adds node in the list.

Similar Threads

  1. Problem sending linked lists
    By deepthought015 in forum Networking
    Replies: 1
    Last Post: 04-26-2009, 12:47 PM
  2. Sending linked lists via sockets
    By deepthought015 in forum Networking
    Replies: 1
    Last Post: 04-26-2009, 02:33 AM
  3. Doubly Linked Lists
    By stevenson15 in forum New To Java
    Replies: 6
    Last Post: 04-21-2009, 12:35 PM
  4. Single and Multi Selection Lists
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:03 PM
  5. question about linked lists
    By jkurth in forum Advanced Java
    Replies: 1
    Last Post: 11-11-2007, 08:33 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
  •