【发布时间】:2014-12-20 17:58:00
【问题描述】:
我编写了一个节点类,其“数据”字段旨在保存任何类型的数据,而链表类旨在保存具有任何类型数据的节点。我正在实现一个删除重复项(“deleteDuplicates”)的类,发现自己无法将链表的尾部设置为空(“删除尾部”),我觉得这是因为我试图将一个类设置为空.我不明白什么?我希望有人能纠正我的想法。具体看我在方法 deleteDuplicates 中的第 116 行注释掉的代码 TODO。
import java.io.*;
// A node class whose 'data' field is designed to hold any type of data.
class node<AnyType> {
AnyType data;
node<AnyType> next;
// Constructor; sets this object's 'data' field to 'data'.
node(AnyType data) {
this.data = data;
}
}
// A linked list class designed to hold nodes with any type of data.
public class LinkedList<AnyType> {
// Notice that when you create a LinkedList object (in main(), for example),
// you tell it what kind of data it'll be holding. The LinkedList class
// needs to pass that information on to the node class, as well. That's
// what's happening here.
private node<AnyType> head, tail;
// insert at the tail of the list
void insert(AnyType data) {
// if the list is empty, set 'head' and 'tail' to the new node
if (head == null) {
head = tail = new node<AnyType>(data);
}
// otherwise, append the new node to the end of the list and move the
// tail reference forward
else {
tail.next = new node<AnyType>(data);
tail = tail.next;
}
}
// insert at the head of the list
void headInsert(AnyType data) {
// first, create the node to be inserted
node<AnyType> YouCanJustMakeANewNode = new node<AnyType>(data);
// insert it at the beginning of the list
YouCanJustMakeANewNode.next = head;
head = YouCanJustMakeANewNode;
// if the list was empty before adding this node, 'head' AND 'tail'
// need to reference this new node
if (tail == null)
tail = YouCanJustMakeANewNode;
}
// print the contents of the linked list
void printList() {
for (node<AnyType> temp = head; temp != null; temp = temp.next)
System.out.print(temp.data + " ");
System.out.println();
}
// Remove the head of the list (and return its 'data' value).
AnyType removeHead() {
// if the list is empty, signify that by returning null
if (head == null)
return null;
// Store the data from the head, then move the head reference forward.
// Java will take care of the memory management when it realizes there
// are no references to the old head anymore.
AnyType temp = head.data;
head = head.next;
// If the list is now empty (i.e., if the node we just removed was the
// only node in the list), update the tail reference, too!
if (head == null)
tail = null;
// Return the value from the old head node.
return temp;
}
node<AnyType> deleteNode(node<AnyType> data)
{
node<AnyType> helper = head;
if( helper.equals(data) )
{
return head.next;
}
while( helper.next != null )
{
if( helper.next.equals(data) )
{
helper.next = helper.next.next;
return helper;
}
helper = helper.next;
}
return helper;
}
void deleteDuplicates( LinkedList<Integer> L1 )
{
for (node<Integer> temp = L1.head; temp != null; temp = temp.next)
{
for (node<Integer> helper = temp; helper.next != null; helper = helper.next)
{
//start at helper.next so that temp doesn't delete it's self
if( temp.data == helper.next.data && helper.next.next != null )
{
helper.next = helper.next.next;
}
/* TODO: DELETE TAIL
//can't seem to figure out how to delete the tail
if( temp.data == helper.next.data && helper.next.next == null )
{
//helper.next = null;
}
*/
}
}
}
// returns true if the list is empty, false otherwise
boolean isEmpty() {
return (head == null);
}
public static void main(String [] args) {
// create a new linked list that holds integers
LinkedList<Integer> L1 = new LinkedList<Integer>();
/*
for (int i = 0; i < 10; i++)
{
// this inserts random values on the range [1, 100]
int SomeRandomJunk = (int)(Math.random() * 100) + 1;
System.out.println("Inserting " + SomeRandomJunk);
L1.insert(SomeRandomJunk);
}
*/
//8,24,15,15,9,9,25,9
L1.insert(8);
L1.insert(24);
L1.insert(15);
L1.insert(15);
L1.insert(9);
L1.insert(9);
L1.insert(25);
L1.insert(9);
//L1.insert(9);
//L1.insert(9);
// print the list to verify everything got in there correctly
System.out.println("Printing integer linked list");
L1.printList();
System.out.println("Printing DEL-repeaded LL");
L1.deleteDuplicates(L1);
L1.printList();
// create another linked list (this time, one that holds strings)
LinkedList<String> L2 = new LinkedList<String>();
L2.insert("Llamas");
L2.insert("eat");
L2.insert("very sexy");
L2.insert("critical thinking");
L2.insert("Paper clips annd now I'm ");
L2.insert("daydreaming");
// print the new list to verify everything got in there correctly
while (!L2.isEmpty())
System.out.print(L2.removeHead() + " ");
System.out.println();
// print the old list just to verify that there weren't any static
// problems that messed things up when we created L2
L1.printList();
}
}
【问题讨论】:
-
“设置尾部有问题”到底是什么意思?如果您的代码因异常而失败,请显示您得到的确切错误。
-
我收到以下错误:LinkedList.main(LinkedList.java:163) 的 LinkedList.deleteDuplicates(LinkedList.java:108) 的线程“main”java.lang.NullPointerException 中的异常
标签: java linked-list