删除链接列表中的元素 [英] Delete elements within a Linked List

查看:37
本文介绍了删除链接列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次寻求帮助,因为我的教授似乎在解释事情上做得很糟糕(假设我们对Java编程了解得太多,实际上,这是我们大多数人都在学习的第一个Java类).>

不是要找人为我编写代码,而是要找人让我知道我是否在正确的轨道上,并指导我朝正确的方向发展.我真的很想学习这些东西,而不是一味地喂它,但是教授很难做到这一点,所以我在这里寻求帮助.

问题是获取一个LinkedList并创建一个函数以删除该列表中的第k个元素.我想出了如果k == 0时如何删除第一项,但是我迷路于如何在循环中访问"k"的适当元素.这是我到目前为止的内容:

 公共类MyLinked {静态类节点{公共节点(重复项,下一节点){this.item = item;this.next =下一个;}公共双重项目;接下来是公共节点;}int N;节点优先;//删除第k个元素(其中k在0到N-1之间(包括0和N-1)public void delete(int k){如果(k< 0 || k> = N)抛出新的IllegalArgumentException();{如果(k == 0){remove(first.item);} else if (k > 0) {kElem = LinkedList.get();remove(kElem);}}}} 

我正在尝试为.get函数分配一个变量,但是我在那肯定是错误的,但是还不确定如何去做.我知道我需要获取第k个元素的值并将其删除.

我还知道,此后,我需要调整LinkedList中的指针,以填补我删除的元素所在的空白.

谢谢.

解决方案

首先转到k-1元素.设置element.next = element.next.next.这就是您跳过应删除的元素的方式.

例外:当k = 0(head元素)时,只需设置head = head.next.

(可选)您可以为已删除的元素设置next = null(当您使用k-1个元素时,在设置element.next = element.next.next之前先删除Deleted = element.next.然后说出delete.next = null即可清除它的下一个指针.


还有第二种常见的方式,您可以转到第k个元素,但始终将前一个(k-1)元素保存在变量中.从性能角度来看,情况更糟,因为您需要在每个步骤中更新2个变量.它可能更直观.检查该视频: https://www.youtube.com/watch?v=2RwWsHePdr8(我希望可以在SO上使用yt链接)


顺便说一句,您的

 静态类Node {公共节点(重复项,下一节点){this.item = item;this.next =下一个;}公共双重项目;接下来是公共节点;}int N;节点优先; 

是列表的实现.LinkedList是java提供的实现.https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html.您不能将它们混合.


提示:

  • 别忘了减小列表的大小(N-;)
  • 您可以使用节点current = head; for(int i = 0; i< n; i ++)current = current.next;"转到"第n个元素.
  • 我所说的头"是您的第一".这是列表的第一个元素.

Looking for help again as my professor seems to do an awful job of explaining things (assumes that we know way too much about java programming, when in fact, it's the first java class most of us are taking).

Not looking for someone to write the code for me, but rather someone who can let me know if I'm on the right track and guide me in the right direction. I really want to learn this stuff, not be spoon-fed it, but the professor is making it very hard to do so, so I turn here for help.

The question is to take a LinkedList and create a function to delete the k-th element in that list. I have figured out how to remove the first item if k == 0, but I'm getting lost on how to access the proper element for the "k" within my loop. Here's what I have so far:

public class MyLinked {
  static class Node {
    public Node(double item, Node next) {
      this.item = item;
      this.next = next;
    }

    public double item;
    public Node next;
  }

  int N;
  Node first;
  // delete the kth element (where k is between 0 and N-1 inclusive)
  public void delete(int k) {
    if (k < 0 || k >= N) throw new IllegalArgumentException();
    {
      if (k == 0) {
        remove(first.item);
      } else if (k > 0) {
        kElem = LinkedList.get();
        remove(kElem);
      }
    }
  }
}

I'm trying to assign a variable to the .get function but I am definitely wrong there, but not quite sure how to go about this. I know I need to get the value of the k-th element and delete it, however.

I'm also aware that after this, I need to adjust the pointers within the LinkedList to fill the gap where the element I deleted would have been.

Thank you.

解决方案

First go to the k-1 element. Set element.next=element.next.next. Thats how you skip the element, which should be deleted.

Exception: When k=0 (the head element), just set head=head.next.

Optionally you can set next = null for the deleted element (when you went for k-1 elements, deleted=element.next before setting element.next=element.next.next. then say deleted.next=null to clear its next-pointer.


There is also a second common way where you go to the kth element, but you always save the previous (k-1) element in a variable. Performance wise it is worse, because you update 2 variables in each step. It could be more intuitive. Check that video: https://www.youtube.com/watch?v=2RwWsHePdr8 (I hope yt-links are allowed on SO)


By the way, your

  static class Node {
    public Node(double item, Node next) {
      this.item = item;
      this.next = next;
    }

    public double item;
    public Node next;
  }

  int N;
  Node first;

is your implementation of the list. LinkedList is the implementation provided by java. https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html. You can not mix them.


Hints:

  • Don't forget to decrease the size of the list (N--;)
  • You can "go" to the n-th element with "Node current=head; for(int i = 0; i < n; i++) current=current.next;"
  • What I mean with "head" is your "first". It is the first element of the list.

这篇关于删除链接列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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