如何删除,修改和提取列表元素? [英] How to remove, modify and extract elements of a list?

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

问题描述

我是Java初学者,目前正在从事一个我即将完成的项目.

I'm a Java beginner and I'm currently working on a project that I have almost completed.

我需要删除,修改和提取列表的元素(这是一个基本的列表,尽管我知道有arrayList(s).)这使我发疯,因为我确切地知道我需要做的事情,但是我我没有得到我需要开始编程的东西.

I need to remove, modify and extract elements of a list (it's a basic one, though I know there are arrayList(s).) This is driving myself crazy, because I know exactly what I need to do, but I'm not getting what should I need start programming.

package lec05;

import java.util.*;

/**
 *
 * @author ulacit
 */
public class Lista {

Celda head;

public Lista() {
    head = null;
}

public void add(Person aPerson) {
    if (head == null) {  // list = empty
        head = new Celda(aPerson);
    } else if (aPerson.getId() < head.getInfo().getId()) {  // add element - left
        Celda aux = new Celda(aPerson);
        aux.setNext(head);
        head = aux;
    } else if (head.getNext() == null) {  // add 1 element - right
        Celda aux = new Celda(aPerson);
        head.setNext(aux);
    } else { // more than 1 - add at the end or in the middle
        Celda actual = head;
        while (actual.getNext() != null
                && actual.getNext().getInfo().getId() < aPerson.getId()) {
            actual = actual.getNext();
        }
        Celda aux = new Celda(aPerson);
        aux.setNext(actual.getNext());
        actual.setNext(aux);
    }
}

public boolean (int id) {
    Celda aux = head;
    while (aux != null && aux.getInfo().getId() < id) {
        aux = aux.getNext();
    }
    return (aux != null && aux.getInfo().getId() == id);
}

public Person restore(int id) {
    Celda aux = head;
    while (aux != null && aux.getInfo().getId() < id) {
        aux = aux.getNext();
    }
    if (aux != null && aux.getInfo().getId() == id) {
        return aux.getInfo();
    } else {
        return null;
    }
}

public void remove(int id) {

}

public void modify(int id, String name) {

}
public Persona extract(int id) {
}

@Override
public String toString() {
    String s = "List{";
    Celda aux = head;
    while (aux != null) {
        s += aux.getInfo() + ", ";
        aux = aux.getNext();
    }
    return s;

}
}

推荐答案

提示:

  1. 您不应该实现自己的列表数据结构...除非您特别需要执行此操作.最好改用现有的List类型.例如ArrayListLinkedList.

  1. You shouldn't implement your own list data structure ... unless you are specifically required to do this. It is better to use an existing List type instead; e.g. either ArrayList or LinkedList.

您的代码无法编译...

Your code won't compile ...

逐步进行开发:

  • 在尝试对其余方法进行编码之前,请在 之前完成并测试add(Person)get(id)toString()方法.

  • Complete and test the add(Person), get(id) and toString() methods before you try to code the remaining methods.

一次开发/测试其余方法.

Develop / test the remaining methods one at a time.

实施您自己的单元测试. (这不是强制性的,但是它将帮助您系统地测试代码.单元测试不必太漂亮……)

Implement your own unit tests. (This is not mandatory, but it will help you test your code systematically. The unit tests don't need to be beautiful ...)

如果您受困,那么有很多很好的教科书介绍了数据结构和算法",它们解释了链表的工作原理.

If you are stuck, there are lots of good textbooks on "data structures and algorithms" that explain how a linked list works.

您拥有的只是一个链表;即列表中的节点具有到下一个节点的链接,但没有到前一个节点的链接.对单个链接列表进行操作的技巧是,在迭代列表时,您(经常)需要跟踪保存指向您正在查看"的链接的节点.例如,要从列表中删除一个节点,您需要能够在当前节点的之前对其进行修改.

What you have is a single linked list; i.e. nodes in the list have links to the next node ... but not the previous node. The trick for doing operations on a single linked list is that as you are iterating the list you (often) need to keep track of the node that holds the link to the one you are "looking at". For instance, to remove a node from the list you need to be able to modify the node before the current one.

这篇关于如何删除,修改和提取列表元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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