将第一个节点插入空双链表中[如何] [英] inserting first node into empty doubly-linked list [how to]

查看:37
本文介绍了将第一个节点插入空双链表中[如何]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是先前帖子.我现在正在研究如何将第一个节点插入到空的双向链接列表中.乍一看似乎有点棘手...对于我的addFirst方法中缺少的内容,我将不胜感激

This is a follow-on to a previous post. I am now looking at how to insert a first node into an empty doubly-linked list. It's kind of tricky at first it seems...i would be grateful for a hint as to what is missing in my addFirst method

...
public DLL()
{
    first = null ;
    last = null ;
}

...
DLL myList = new DLL() ;
DLLNode A = new DLLNode("Hello", null, null) ;
...

myList.addFirst(A) ;

...
public void addFirst(DLLNode v)
{
    v.pred = first ;
    v.succ = last ; 
}

typo.pl提出的解决方案:

Solution as proposed by typo.pl:

public void addFirst(DLLNode v)
{
    v.pred = first ;
    v.succ = last ;
    first = v ;
    last = v ;
}

推荐答案

您仅更新了节点的信息.

You've only updated the node's information.

现在,您需要更新DLL的有关列表中第一个/最后一个节点是什么的信息.将一个节点添加到一个空列表时,更新确实很容易.第一个节点只有一个选择,最后一个节点只有一个选择.

Now you need to update the DLL's information as to what the first/last nodes in the list are. And it's really easy to update when you're adding one node to an empty list. There's only one choice for first node and one choice for last node.

这篇关于将第一个节点插入空双链表中[如何]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆