• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedList class problem

 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys...

im' still new to java....and i have a problem with linkedlist ....

Problem:
my teacher asking me to define a linkedlist class and include methods such as insert into the list.....
what i wrote for him was:
---------------------------------------------------------------------
public class LinkedList
{
private Object a;

LinkedList()
{ }

// insert part ???
// use Boolean add(), then does my code inside it correct?
// or i just leave Boolean add() no method body??? which is "public Boolean add(); "

public Boolean add(Object o)
{
a=o;
}

// get part ???
public Object get()
{
return a;
}

// other method inside LinkedList class
public Boolean remove(Object re)
{
// what to insert here?
}
}

----------------------------------------------------------------------------------

maybe my coding will confusing you, but if you are confused, then just based on my problem(red color) and provide me the right coding solution...thanks.

I hope someone can help here....because i dont understand LinkedList...still try to learn it....hope some one could solve my above coding and i can have a better understanding...thanks....
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Soon,

welcome at the Ranch!

Please don't crosspost. As this is really a beginner's questions, I moved your other three posts to the trash.

Have fun!
[ March 26, 2005: Message edited by: Ilja Preuss ]
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for doing that for me.....

i did that because i afraid no one realise about my problem and help me....

but thanks .

nice day to you too!
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you start coding, you should really make sure you understand how a LinkedList operates. Without that, you're coding blind. The class you posted will store exactly one Object value. The add(Object) method sets that value; it doesn't add another Object to a list. There is no list.

A linked list is generally created using two classes: the LinkedList class itself with operations like add(), get(), remove(), size(), isEmpty(), etc. and a ListNode class that holds a single element for a list. Each ListNode has a reference to the next node in the list, and the LinkedList has a reference to the first node of the list (and optionally the last node). Typically, the LinkedList class will perfom all of the work while the ListNode class is just a data holder.

Draw this out on a piece of paper. If you're not familiar with doing this, you draw little boxes for each instance of a class (each Object) in your list for a specific example. A list that currently holds three elements would require a box for the list and three boxes for the three nodes and three boxes for the three objects held by the list. The list box would have an arrow pointing to the first node. Each node would have an arrow pointing to the element it holds and another arrow pointing to the next node in the list. The last node in the list would have its "next node" arrow pointing to an X or some other indicator for null. Since each node will always point to an element, you can omit those if you like.

You'll want to draw out examples for each of the operations you need to perform. For example, when you add an element to the end of the list, you can use the drawing to devise the algorithm you need to use to traverse the nodes until you reach the end of the list (this is where that optional reference to the last node comes in handy) and the code needed to add a new node.

By drawing it out, you'll be able to more easily visualize how all of the object references (pointers in C/C++ terms) relate and how to modify them to perform add/insert/remove operations.
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again for telling me the understanding of linkedlist....
i know operation within linkedlist got add(), addFirst(), isEmpty(), remove(), etc...but i just want to know about the structure or coding it represent for a linkedlist class....

Linkedlist class i want is empty link....i want to know the basic it can re[present to me...like:


public class LinkedList
{
// insert particular variables
private Object item;

public LinkedList()
{
// code here

}

public Boolean isEmpty()
{
//code here
}

public void addFirst(Object o) // takes in parameter
{
//code here
item=o;
}

public Object getFirst()
{
return item;
}

public Object remove()
{
//code here?
}

public Boolean isEmpty()
{
//code here
}
}



i missed some code in above coding, what should i write the code for that?
can help?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Soon Heng Eng:
i missed some code in above coding, what should i write the code for that?
The above is only the set of methods you're supposed to implement. We can't do your homework for you, or you won't learn! We're here to help you, not hurt you.

How will the instance member "item" store a list of Objects? Think about my design above and give it a try yourself first. We can give you hints from there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic