import java.util.;
import java.io.;
public interface Lista {
class ElementL{ // klasa wewnętrzna - element listy
int dane;
ElementL nastepny;
public ElementL(int x, ElementL n)
{
dane=x;
nastepny=n;
}
public ElementL getNext(){
return nastepny;
}
public int getWartosc(){
return dane;
}
}
public class list{
ElementL start;
list(){
start=null;
}
public ElementL getStart(){
return start;
}
// dodaje element i na początek listy
public void insert(int i){
ElementL temp = new ElementL(i,start);
start=temp;
}
// ładuje zawartość pliku do listy
public int read(String fileName) throws FileNotFoundException
{
FileReader plik = null;
plik = new FileReader(fileName);
int i=0;
String w;
BufferedReader bfr = new BufferedReader(plik);
try{
while ((w = bfr.readLine()) != null)
{
insert(Integer.parseInt(w));
i++;
}}
catch (IOException e) {
System.out.println("Błąd odczytu z pliku");
}
return i;
}
//wypisuje kolejne elementy listy na ekranie
public void write()
{
if(start!=null){
ElementL temp = start;
while(temp!=null){
System.out.print(temp.getWartosc() + " ");
temp=temp.getNext();
}
System.out.println();
} else {
System.out.println("Stos jest pusty");
}
}
// zwraca true, jeśli x znajduje się w liście
public boolean contains(int x){
boolean z= false;
if(start!=null){
ElementL temp = start;
while(temp!=null){
if (x == temp.getWartosc()) z=true;
temp=temp.getNext();
}
}
return z;
}
//zwraca pozycję pierwszego wystąpienia x w liście, lub -1
public int find(int x)
{
boolean z=false;
int indeks=1;
if(start!=null){
ElementL temp = start;
while(temp!=null && z!=true){
if (x == temp.getWartosc()) z=true;
temp=temp.getNext();
}
}
else indeks=indeks+1;
if (z==true) return indeks;
else return -1;
}
// zwraca wartość pierwszego elementu listy
public int getFirst() //throws Exception
{
if (start!=null)
return start.getWartosc();
else return -1;
}
// zwraca wartość i-tego elementu listy
public int get(int i) //throws Exception
{
boolean z=false;
int indeks=1;
int g=0;
if(start!=null){
ElementL temp = start;
while(temp!=null && z!=true){
if (i == indeks)
{
z=true;
g=temp.getWartosc();
}
else
{
indeks=indeks+1;
temp=temp.getNext();
}
}
}
if (z==true) return g;
else return -1;
}
// usuwa pierwszy element, zwraca jego wartość
public int removeFirst() //throws Exception
{
if(start!=null){
start = start.getNext();
} else {
System.out.println("Lista jest pusta");
}
return start.getWartosc();
}
// usuwa i-ty element z listy, zwraca jego wartość
public int remove(int i) //throws Exception
{
boolean z=false;
int indeks=1;
int g=0;
if(start!=null){
ElementL temp = start;
while(temp!=null && z!=true){
if (i == indeks)
{
z=true;
g=temp.getWartosc();
start = start.getNext();
}
else
{
indeks=indeks+1;
temp=temp.getNext();
}
}
}
if (z==true) return g;
else return -1;
}
public static void main(String[] args) throws FileNotFoundException
{
list q = new list();
PrintWriter zapis = new PrintWriter("zadania.txt");
System.out.println("Podaj kilka liczb, zero kończy wprowadzanie");
int x=5;
while (x!=0)
{
Scanner wp = new Scanner(System.in);
x=wp.nextInt();
if (x!=0) zapis.println(x);
}
zapis.close();
q.read("zadania.txt");
q.write();
if(q.contains(5) == true) System.out.println("Zawiera");
q.removeFirst();
q.remove(6);
q.write();
}
}
}
Mam mały błąd w usuwaniu i-tego elementu listy może ktoś wie dlaczego??? I może ktoś pomoże z wyjątkami, starczy tylko jeden zrobić resztę sam zrobię.