• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Singly LinkedList headAdd / headDelete methods

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Algorithm and Java beginner here. I constructed a singly linked list like this:



When this code is tested with an automatic scoring tool, my methods headAdd and headDelete are failing. How can I correct these methods without changing any other part of the code?

Thank you for your help.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cemal Özdemir wrote:When this code is tested with an automatic scoring tool, my methods headAdd and headDelete are failing.


'Fraid that's not enough information.

How are they failing? And please provide exact messages.

One thing I do see though is that you seem to be combining both a 'Node' and a 'List' in your class.

Normally a "linked list" is a list that contains Nodes (actually only one - the first one, or 'head'). Each Node then contains a pointer to the one that follows it - or null if it's the last one in the chain.

Thus you normally have two classes: LinkedList and Node; and the latter is usually a static nested class, because it really only has any meaning inside your list class.

HIH

Winston
 
Cemal Özdemir
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again, these are the test cases:


TEST: linkedlist_headDelete_1
description: delete from 1-node list
Correct: ** null list **
Yours: ** null list **
5 SUCCESS

TEST: linkedlist_headDelete_2
description: delete from 2-node list
Correct: (b0)
Yours: ** null list **
0 FAILED

TEST: linkedlist_headAdd_1
description: Add 1-node list
Correct: (b0)
Yours: (b0)
5 SUCCESS

TEST: linkedlist_headAdd_2
description: Add 2-node list
Correct: (b1)(b0)
Yours: (b0)
0 FAILED

TEST: linkedlist_headAdd_3
description: Add 3-node list
Correct: (b2)(b1)(b0)
Yours: (b0)
0 FAILED
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cemal Özdemir wrote:Hello again, these are the test cases:


Much better. After a cursory glance, one thing I see is that in your headAdd() method you create a newHead object, but never assign it to head, so your list will keep pointing to the original one. Nothing else leaps out at me, so it may be your only problem.

I still say that the structure is wrong though. A single-linked list should only have 1 pointer per value and you have 2: 'head' and 'link'.

Winston
 
and POOF! You're gone! But look, this tiny ad is still here:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic