在不使用内置 methods/importing util 的情况下在 java 中创建链表
Creating linked lists in java without using in-built methods/importing util
The question is to create a linked list that creates nodes and links
them and should have the following methods.
- AddFirst
- AddLast
- Remove/Delete
- Insert before and after...
我已经设法完成了下面的操作,但我似乎无法理解代码有什么问题。错误部分显示为“LinkedList.java [行:16]
错误:变量头可能尚未初始化“
/*Uses the node class to create a linked list of integer type
* nodes and stores them
*/
public class LinkedList
{
public Node head;
public static void main(String [] args) {
}
//Methods adds a link to the head
//Appends to the beginning of the list
public void addFirst(int data) {
Node head = new Node(data, head);
//Because head is the pointer to the first node
// Traversing the list
Node temp = head;
while (temp != null) {
temp = temp.next;
}
}
//Adding at the end of the list
public void addLast(int data) {
if (head == null) {
addFirst(data);
//When the list is empty, i.e, head points to null
} else {//When list is populated
Node temp = head;
while (temp.next != null) {
temp = temp.next;
temp.next = new Node(data, null);
}
}
}
//To insert a new node after a given "key"
//_data is the new node data
public void insAft(int _data, int key) {
Node temp = head;
while (temp != null && temp.data != key) {
temp = temp.next;
}
if (temp != null) {
temp.next = new Node(_data, temp.next);
}
}
}
/*Node class to create the node (object)
* takes integer parameters
*/
class Node{
public int data;
Node next;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
public String toString() {
return data + " ";
}
}
给出错误的变量 head
(new Node(data,
head)
) 指的是新变量 head
你正在创造的过程中。这个错误可以通过添加this
:
来解决
Node head = new Node(data, this.head);
或者,如果您不是尝试创建新变量:
head = new Node(data, head);
The question is to create a linked list that creates nodes and links them and should have the following methods.
- AddFirst
- AddLast
- Remove/Delete
- Insert before and after...
我已经设法完成了下面的操作,但我似乎无法理解代码有什么问题。错误部分显示为“LinkedList.java [行:16] 错误:变量头可能尚未初始化“
/*Uses the node class to create a linked list of integer type
* nodes and stores them
*/
public class LinkedList
{
public Node head;
public static void main(String [] args) {
}
//Methods adds a link to the head
//Appends to the beginning of the list
public void addFirst(int data) {
Node head = new Node(data, head);
//Because head is the pointer to the first node
// Traversing the list
Node temp = head;
while (temp != null) {
temp = temp.next;
}
}
//Adding at the end of the list
public void addLast(int data) {
if (head == null) {
addFirst(data);
//When the list is empty, i.e, head points to null
} else {//When list is populated
Node temp = head;
while (temp.next != null) {
temp = temp.next;
temp.next = new Node(data, null);
}
}
}
//To insert a new node after a given "key"
//_data is the new node data
public void insAft(int _data, int key) {
Node temp = head;
while (temp != null && temp.data != key) {
temp = temp.next;
}
if (temp != null) {
temp.next = new Node(_data, temp.next);
}
}
}
/*Node class to create the node (object)
* takes integer parameters
*/
class Node{
public int data;
Node next;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
public String toString() {
return data + " ";
}
}
给出错误的变量 head
(new Node(data,
head)
) 指的是新变量 head
你正在创造的过程中。这个错误可以通过添加this
:
Node head = new Node(data, this.head);
或者,如果您不是尝试创建新变量:
head = new Node(data, head);