• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java LinkedList  RSS feed

 
Sohaib Sabir
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iwant to declare a variable of type ListNode in java, to use it for Insertion , but I got an error => ListNode cannot be resolved to a type
I was thinking thiere is a builting class called ListNode

public class Data {


private ListNode head; // ListNode cannot be resolved to a type
private ListNode current; // ListNode cannot be resolved to a type
private ListNode previous; // ListNode cannot be resolved to a type

}

Thanks in advance
Asadun
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Asadun,

Java has a LinkedList class, as described here. Linked List JavaDoc. However, it does not require any special kind of node to use it. In classes I'd taken in the past, the linked list had to be built up out of nodes, and you had to manage head/tail, etc. But Java's Collection classes (located in java.util) provide for all the "linked list" operations, while letting you ignore the details of how a linked list works.

I looked up "ListNode" and it seems to be (as I suspected) the kind of thing students are assigned to write for data structures courses.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Foster wrote:. . . Java has a LinkedList class, . . . it does not require any special kind of node to use it. . . .
It probably supplies its own node class as a private inner class.

You are right that students are expected to provide their own ListNode (or similar) classes. I think a private inner class would be a good way to do it.
You had a broken link which I have corrected. When you use the URL button, simply paste the whole URL with ctrl‑V; that will overwrite the http:// bit which is provided by the URL tags automatically.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sohaib Sabir wrote:I was thinking thiere is a builting class called ListNode

Nope.

So: do you want to use a LinkedList to add an item, or do you want to write your own? Because there's a BIG difference between the two, and the latter is a lot tougher than the first.

The only other thing I would suggest is that if you want to roll your own, then start out with a single-linked list unless you've been told to do otherwise, because it's a lot easier to write.

Also: your implementation seems to be mixing up aspects of a list Node (previous), the List itself (head), and a list Iterator (current), so I'd get those sorted out before you go much further.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!