Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LinkedList {
- private element head = new element(null);
- private int size;
- public LinkedList()
- {
- pusta();
- }
- //Czyszczenie listy:
- public void pusta()
- {
- head.setNext(null);
- size=0;
- }
- public void dodaj(karty pierwsza)
- {
- if(head.getNext()==null) head.setNext(new element(pierwsza));
- element last = head.getNext();
- while(last.getNext()!=null)
- last=last.getNext();
- size++;
- last.setNext(new element(pierwsza));
- }
- //usuwanie obiektu podanego jako parametr
- public boolean usun(karty o)
- {
- if(head.getNext()==null) return false;
- if(head.getNext().getAktualnyObiekt().equals(o))
- {
- head.setNext(head.getNext().getNext());
- size--;
- return true;
- }
- element usun = head.getNext();
- while(usun != null&&usun.getNext()!=null)
- {
- if(usun.getNext().getAktualnyObiekt().equals(o))
- {
- usun.setNext(usun.getNext().getNext());
- size--;
- return true;
- }
- usun = usun.getNext();
- }
- return false;
- }
- //Metoda zwracająca obiekt o podanym indeksie na liście cos tu jest do dodania jak cos :)
- public karty Szukaj(int index) throws IndexOutOfBoundsException
- {
- if(index<0 || index>size)throw new IndexOutOfBoundsException();
- element find = head.getNext();
- for(int i=0;i<=index;i++)
- find = find.getNext();
- return find.getAktualnyObiekt();
- }
- //rozmiar listy
- public int LiczbaElementow()
- {
- return size;
- }
- public boolean czyPusta()
- {
- return size==0;
- }
- public static void main(String[] args)
- {
- karty trol = new karty();
- LinkedList duu = new LinkedList();
- duu.dodaj(trol);
- System.out.println(trol);
- }
- }
- import java.util.Random;
- public class karty {
- Random r = new Random();
- int kolor;
- int atrybut;
- karty(int kolor,int atrybut)
- {
- this.kolor=kolor;
- this.atrybut=atrybut;
- }
- karty()
- {
- kolor=r.nextInt(14);
- atrybut=r.nextInt(4);
- }
- }
- public final class element {
- public karty pierwsza;
- public element next;
- element(karty pier){
- this(pier,null);
- }
- public element(karty pier, element n){
- pierwsza = pier;
- next = n;
- }
- public karty getAktualnyObiekt()
- {
- return pierwsza;
- }
- public element getNext()
- {
- return next;
- }
- public void setNext(element n)
- {
- next = n;
- }
- public void setValue(karty o)
- {
- pierwsza = o;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement