Java 如何在泛型类中将节点添加到链表的末尾? 类链接队列{ 私有类节点{ 项目(t)项目;; 节点链接; 公共节点(项目t,节点l){ 项目=t; link=l; } 私有节点前端=null; 私有节点后部=空; 公共作废插入(任何项目){ this.link=新节点(任意,this.link); }
insert方法应该在队列的末尾添加“any”。它只在node类中起作用,但现在它在linkedqueue类中,我不知道如何修复“this.link”part..LinkedList中的最后一个节点将指向空值。因此,抓取列表中的最后一个节点,将其下一个节点设置为要传入的新节点,最后将新节点的下一个指针设置为空,表示列表的结束Java 如何在泛型类中将节点添加到链表的末尾? 类链接队列{ 私有类节点{ 项目(t)项目;; 节点链接; 公共节点(项目t,节点l){ 项目=t; link=l; } 私有节点前端=null; 私有节点后部=空; 公共作废插入(任何项目){ this.link=新节点(任意,this.link); },java,generics,linked-list,queue,Java,Generics,Linked List,Queue,insert方法应该在队列的末尾添加“any”。它只在node类中起作用,但现在它在linkedqueue类中,我不知道如何修复“this.link”part..LinkedList中的最后一个节点将指向空值。因此,抓取列表中的最后一个节点,将其下一个节点设置为要传入的新节点,最后将新节点的下一个指针设置为空,表示列表的结束 class linkedqueue <item_t> { private class node{ item_t item; node
class linkedqueue <item_t> {
private class node{
item_t item;
node link;
public node(item_t t, node l){
item=t;
link=l;
}
private node front = null;
private node rear = null;
public void insert (item_t any) {
this.link=new node(any,this.link);
}
private void addLast(节点阳极)
{
节点头,最后一个节点;
head=this.getHead();
mySize++;
if(head==null)
{
这是第一个(阳极);
}
其他的
{
lastNode=this.getPrevious(null);//获取最后一个节点,即null之前的节点
lastNode.setNext(阳极);//将新节点添加到列表的末尾
阳极.setNext(null);//将新节点的下一个指针设置为null,指示列表的结尾
}
}
简单点..如何编写“insert”方法…将值为“any”的节点插入队列末尾..很抱歉,我是链接列表的新手..您的代码中有很多方法我不确定它们是如何实现的..好的,我很高兴能够提供帮助。查看此链接以获取更多信息。
private void addLast(Node<T> aNode)
{
Node<T> head, lastNode;
head = this.getHead();
mySize++;
if(head == null)
{
this.addFirst(aNode);
}
else
{
lastNode = this.getPrevious(null); // get last Node, which is the Node previous to Null
lastNode.setNext(aNode); // add the new node to the end of the list
aNode.setNext(null); //set the new node's next pointer to null, indicating the end of the list
}
}