• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Linked Lists  RSS feed

Jud Matin
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I create an application that uses a linked list. It must be able to do at least the following to the linked list:

check whether the list is empty
display the list
find the length of the list
destroy the list
retrieve the data in the nth node
search the list for a given item
insert an item in the list
delete an item from the list

I need help on how to start it and help in executing the actions from the list above.

Many thanks
Jeff Verdegan
Posts: 6109
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You start by googling for something like java LinkedList tutorial. Once you've done some research on how to use LLs in general, you can also look at the javadocs for java.util.LinkedList.

Then you create a HelloWorld program. Don't even think about putting a List in there until this program compiles and runs without errors. All it should do is print out "Hello, World!"

Then you add code to instantiate a LinkedList. (Do you know how to instantiate an object? If I say, "Create a new instance of a Foo object," do you know how to do that? If not, it's time for some more research.) Once your code that only instantiates a LinkedList (but doesn't do anything with it yet) compiles and runs correctly, then you can move on to the next step.

So pick one. The first one in your list of instructions looks like as good a starting place as any. So...

Add code to your existing program that only checks if that list you just created is empty, and prints out the result. Once that compiles and runs correctly, pick another piece to add.

See the pattern?

If you get stuck along the way, post again, and TellTheDetails(←click) on what you tried and what's giving you trouble.
Steve Luke
Posts: 4181
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, welcome to the Ranch.

1) The first thing you should do is pick just one task,
2) then bring out a pencil and paper.
3) Write down as many details about exactly how that task should be performed. How it might be called, what the expected results should be. Try to find and describe edge cases (like what happens when the list is empty, or the passed-in value is null, etc...)
4) Then start to flesh those details out with short, sequential (but still English) steps about how you might accomplish the task.
5) Using the steps from #4, try to determine what 'things' you would need - and name those things (like if you need a reference to the start of the list, name is startOfList)
6) Using the steps in #4 and names in #5, re-write the steps in even more detail, being as distinct and clear as possible.
7) Review the steps in #6, follow the steps through with a sample list and inputs (still on pencil and paper) and make sure that the requirements you set out in #3 are all met.
8) If #7 proves the simple statements are complete, then move on to #9. If not, then repeat #4-#7 until you get it correct
9) Write some test cases for situations found in #3 for the normal situation, and the corner cases you determined. Make these tests start with a known value, do one step, and make the results readable so you can tell if the test passed or failed.
10) Translate the steps from #6 into Java. Every couple of lines of code, compile and run your tests. See which test fails first, and then translate more from #6 into code so that failure goes away. Repeat this for each test and each failure. By the time you are done this step the task you picked in #1 will be complete.
12) Repeat $#1-#10 for each task on the list you provided
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!