【问题标题】:How do I hand trace a Linked List X through the following operations?如何通过以下操作手动跟踪链接列表 X?
【发布时间】:2019-06-02 18:18:14
【问题描述】:

我正在尝试在下面回答这个问题,但我被卡住了。

通过以下操作手工追踪一个链表X:

X.add("Fast"); 
X.add("Boy"); 
X.add("Doctor"); 
X.add("Event"); 
X.add("City"); 
X.addLast("Zoo"); 
X.addFirst("Apple"); 
X.add (1, "Array");
X.remove("Fast");
X.remove (2);
X.removeFirst ();
X.removeLast ();

这就是我手动追踪它的方式,我试图了解我是否/在哪里做错了,因为我在其他地方看到了令人困惑的答案:

男孩快

医生男孩快

事件医生男孩快

城市活动医生男孩快

动物园城市活动医生男孩快

动物园城市活动医生男孩快速苹果

动物园城市活动医生男孩快速数组苹果

动物园城市活动医生男孩数组苹果

动物园城市事件医生阵列苹果

动物园城市事件医生数组

城市事件医生数组

【问题讨论】:

    标签: data-structures linked-list stack


    【解决方案1】:

    执行操作后会得到如下列表:

    您的 LinkedList 从头部开始。现在首先,你必须从头部插入,否则到最后。

    男孩->快

    医生->男孩->快

    事件->医生->男孩->快

    城市->事件->医生->男孩->快速

    City->Event->Doctor->Boy->Fast->Zoo // 错误 Zoo->City->Event->Doctor->Boy->Fast

    Apple->City->Event->Doctor->Boy->Fast->Zoo // 错误 Zoo->City->Event-> Doctor->Boy->Fast->Apple

    Apple->Array->City->Event->Doctor->Boy->Fast->Zoo // 错误,因为链表应该从苹果开始,但它不完全正确,因为 LinkedList 中没有索引它应该在你身上。

    同样,您可以执行其他操作。

    【讨论】:

    • 谢谢。你能解释一下吗?我以为会是 City-->Event-->Doctor-->Array。我在这里做错了什么?
    • 您可以查看更新后的答案,如果您理解解释,请将其标记为正确。
    【解决方案2】:
    X.add("Fast"); 
    X.add("Boy"); 
    X.add("Doctor"); 
    X.add("Event"); 
    X.add("City"); 
    City->Event->Doctor->Boy->Fast
    tail    ----- head
    top   ---- bottom
    top........0
    
    X.addLast("Zoo"); 
    X.addFirst("Apple");
    Zoo->City->Event->Doctor->Boy->Fast->Apple
    
    X.add (1, "Array");
    Zoo->City->Event->Doctor->Boy->Fast->Array->Apple
    
    X.remove("Fast");
    Zoo->City->Event->Doctor->Boy->Array->Apple
    
    X.remove (2);
    Zoo->City->Event->Doctor->Array->Apple
    
    X.removeFirst ();
    Zoo->City->Event->Doctor->Array
    
    X.removeLast ();
    City->Event->Doctor->Array
    
    Answer depends on , what each operation means,
    Here is how I perceived them:
    add("A") -> will append "A" as head to the linked list.
    addFirst("A") -> will add "A" as head to the linked list i.e. first element.
    addLast("A") -> will append "A" as tail to the linked list.
    add (i, "A") -> will add "A" at i th position from head(i=0,1,2,..)
    remove("A") ->  will remove "A" from LL
    remove(i) -> will remove element from i th position
    removeFirst() -> will remove (head)first element from LL.
    removeLast() -> will remove (tail)last element from LL.
    It will be helpful if you can provide this sort of detail.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多