问题描述:

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

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;

}

}

网友答案:

It's not difficult at all you can..

You have the head of your Lista, what you have is a single linked list

Here is an illustration of a single linked list.

So when you call for example remove(int x), (assume x is the id of a Celda)

Some pseudocode
 aux=Head
 1. Check if you have aux (aux !=null)
 2. Check if the aux has that id
 2.1. if not move to the next Celda and start the same question (aux = aux.getNext()) 
 2.2 if it has, you know what to delete, so you have to have a reference to the previous  Celda of aux if it exist in this method, so know the previous.getNext() = aux.getNext() 

If you can visualize with images is much easier then to code it ;).

网友答案:

Hints:

  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.

  2. Your code won't compile ...

  3. Develop this incrementally:

    • 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.

  4. 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 ...)

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

  6. 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.

网友答案:

If you are going to add and remove, you need pointers. You keep referencing "getNext" but you don't have a next pointer. Think of a pointer as the connection for each element. If you have 2 elements, how do you know which is first and which is second. You have to have some relation.

Here is a great tutorial to get started: http://www.dreamincode.net/forums/topic/143089-linked-list-tutorial/

相关阅读:
Top