Dobry wieczór!
Mam taki mały problem- brak weny a może umiejętnośći. Napisałem liste, która zawiera podstawowe funkcje tj. dodaj, usuń etc. Jednakże chciałem ją rozszerzyć o metodę, która umożliwia usuwanie nieparzystych elementów listy i tutaj jest problem- chciałbym utworzyć z usuniętych elementów nową listę. Zastanawiałem się nad rozszerzeniem metod jakie już napisałem (dodaj, dodaj2, dodaj3), ale nie potrafię rozwiązać tego problemu(Może metoda usuńNieparzyste() powinna zwracać na przykład tablicę tych elementów, czy może jakoś można to zrobić prościej, albo jakiś fajny triczek). Prosiłbym o konkretne wskazówki :)
Oto mój kod, który się składa z 3 klas (Lista, main, element):
Main:
public class main
{
public static void main(String[] args)
{
element element1= new element(3);
element element2= new element(15);
element element3= new element(11);
element1.next=element2;
element2.next=element3;
Lista lista = new Lista(element1);
// lista.usuńNieparzyste();
// lista.drukuj();
lista.dodaj(2);
// lista.dodaj2(3);
lista.dodaj3(123, element3);
lista.drukuj();
lista.usuńNieparzyste(lista.długość());
lista.usuń(3);
System.out.println("");
lista.drukuj();
System.out.println("Długość listy wynosi: " + lista.długość() );
}
}
Lista:
public class Lista {
element head;
Lista() {
}
Lista(element head) {
this.head = head;
}
public void drukuj() {
element p = this.head;
while (p != null) {
System.out.println(p.value);
p = p.next;
}
}
public int długość() {
int licznik = 0;
element p = this.head;
while (p != null) {
p = p.next;
licznik++;
}
return licznik;
}
public void dodaj(int x) {
element p = this.head;
while (true) {
if (p.next == null) {
p.next = new element(x);
break;
} else {
p = p.next;
}
}
}
public void dodaj2(int x) {
element a = new element();
a.next = head;
a.value = x;
head = a;
}
public void dodaj3(int x, element v) {
element l;
if (head == null) {
l = new element(x, null);
head = l;
} else {
l = new element(x, v.next);
v.next = l;
}
}
public void usuń(int x) {
element l = head;
element wcześniej = null;
while (l != null && l.value != x) {
wcześniej = l;
l = l.next;
}
if (l != null) {
if (wcześniej == null) {
head = l.next;
} else {
wcześniej.next = l.next;
}
}
}
public void usuńNieparzyste(int licznik) {
for (int a = 0; a < licznik; a++) {
element l = head;
element wcześniej = null;
while (l != null && (l.value % 2) == 0) {
wcześniej = l;
l = l.next;
}
if (l != null) {
if (wcześniej == null) {
head = l.next;
} else {
wcześniej.next = l.next;
}
}
}
}
public int następny(int x)
{
element l = head;
int k;
element wcześniej = null;
while (l != null && l.value != x) {
wcześniej = l;
l = l.next;
}
l=l.next;
k=l.value;
return k;
}
public int wcześniejszy(int x)
{
element l = head;
int k;
element wcześniej = null;
while (l != null && l.value != x) {
wcześniej = l;
l = l.next;
}
k=wcześniej.value;
return k;
}
}
element:
public class element {
int value;
element next;
element()
{}
public element(int value)
{
this.value=value;
}
public element(int value, element next)
{
this.value=value;
this.next=next;
}
}