Java 删除任意类型的链表尾部

Java 删除任意类型的链表尾部,java,linked-list,Java,Linked List,我编写了一个节点类,其“data”字段设计用于保存任何类型的数据,而链表类设计用于保存具有任何类型数据的节点。我实现了一个类来删除重复项(“deleteDuplicates”),发现自己在将链接列表的尾部设置为null(“删除尾部”)时遇到了问题,我觉得这是因为我试图将一个类设置为null。我不明白什么?我希望有人能纠正我的想法。具体请参见方法deleteDuplicates中第116行的注释掉的代码TODO import java.io.*; // A node class whose 'd

我编写了一个节点类,其“data”字段设计用于保存任何类型的数据,而链表类设计用于保存具有任何类型数据的节点。我实现了一个类来删除重复项(“deleteDuplicates”),发现自己在将链接列表的尾部设置为null(“删除尾部”)时遇到了问题,我觉得这是因为我试图将一个类设置为null。我不明白什么?我希望有人能纠正我的想法。具体请参见方法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();
    }
}
import java.io.*;
//一种节点类,其“数据”字段设计用于保存任何类型的数据。
类节点{
任意类型数据;
节点下一步;
//构造函数;将此对象的“数据”字段设置为“数据”。
节点(任意类型数据){
这个数据=数据;
}
}
//设计用于保存具有任何类型数据的节点的链表类。
公共类链接列表{
//请注意,当您创建LinkedList对象(例如在main()中)时,
//您可以告诉它将保存什么类型的数据。LinkedList类
//还需要将该信息传递给node类。这就是
//这里发生了什么。
私有节点头、尾;
//在列表末尾插入
无效插入(任意类型数据){
//如果列表为空,则将“head”和“tail”设置为新节点
if(head==null){
头部=尾部=新节点(数据);
}
//否则,将新节点附加到列表的末尾并移动
//尾部参考前进
否则{
tail.next=新节点(数据);
tail=tail.next;
}
}
//在列表的开头插入
空心头嵌件(任意类型数据){
//首先,创建要插入的节点
node YouCanJustMakeANewNode=新节点(数据);
//将其插入列表的开头
你可以只做一个newNode.next=head;
head=您只需创建一个新节点;
//如果在添加此节点之前列表为空,“head”和“tail”
//需要引用此新节点
if(tail==null)
tail=YouCanJustMakeANewNode;
}
//打印链接列表的内容
作废打印列表(){
对于(节点温度=头;温度!=null;温度=下一个温度)
系统输出打印(温度数据+“”);
System.out.println();
}
//删除列表的标题(并返回其“数据”值)。
AnyType removeHead(){
//如果列表为空,则通过返回null表示
if(head==null)
返回null;
//存储来自头部的数据,然后向前移动头部参照。
//Java在实现时将负责内存管理
//不再提及旧的头了。
AnyType temp=头数据;
head=head.next;
//如果列表现在为空(即,如果我们刚刚删除的节点是
//仅列表中的节点),也更新尾部引用!
if(head==null)
tail=null;
//从旧头节点返回值。
返回温度;
}
节点删除节点(节点数据)
{
节点助手=头部;
if(helper.equals(数据))
{
返回head.next;
}
while(helper.next!=null)
{
if(helper.next.equals(数据))
{
helper.next=helper.next.next;
返回助手;
}
helper=helper.next;
}
返回助手;
}
作废删除重复项(链接列表L1)
{   
对于(节点温度=L1.head;温度!=null;温度=temp.next)
{
for(节点helper=temp;helper.next!=null;helper=helper.next)
{
//从helper.next开始,这样temp就不会删除其自身
if(temp.data==helper.next.data&&helper.next.next!=null)
{
helper.next=helper.next.next;
}
/*TODO:删除尾部
//似乎不知道如何删除尾部
if(temp.data==helper.next.data&&helper.next.next==null)
{
//helper.next=null;
}
*/
}
}
}
//如果列表为空,则返回true,否则返回false
布尔isEmpty(){
返回值(head==null);
}
公共静态void main(字符串[]args){
//创建包含整数的新链表
LinkedList L1=新LinkedList();
/*      
对于(int i=0;i<10;i++)
{
//这将在[1100]范围内插入随机值
int-someRandomBunk=(int)(Math.random()*100)+1;
System.out.println(“插入”+someRandomBunk);
L1.插入(一些垃圾);
}
*/      
//8,24,15,15,9,9,25,9
L1.插入(8);
L1.插入(24);
L1.插入(15);
L1.插入(15);
L1.插入(9);
L1.插入(9);
L1.插入(25);
L1.插入(9);
//L1.插入(9);
//L1.插入(9);
//打印列表以验证所有内容是否正确
System.out.println(“打印整数链表”);
L1.打印列表();
System.out.println(“打印DEL repeaded LL”);
L1.删除重复项(L1);
L1.打印列表();
//创建另一个链表(这次是一个包含字符串的链表)
LinkedList L2=新建LinkedList();
L2.插入(“骆驼”);
L2.插入(“eat”);
L2.插入(“非常性感”);
L2.插入(“批判性思维”);
L2.插入(“回形针和现在的我”);
L2.插入(“白日梦”);
//打印新列表,以验证所有内容是否正确
而(!L2.isEmpty())
System.out.print(L2.removeHead()+);
System.out.println();
//将旧列表打印到verif
        for (node<Integer> helper = temp; helper != null && 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;
            }
            //can't seem to figure out how to delete the tail
            if( temp.data == helper.next.data && helper.next.next == null )
            {
                    helper.next = null;
            }
        }
helper.next != null
helper != null && helper.next != null