Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package aiz.list;
- /**
- *
- * @author MaxSylverWolf
- */
- public class ListOne<T> implements IList<T> {
- ElemOne<T> pierwszy;
- ElemOne<T> ostatni;
- int licznik;
- public ListOne() {
- pierwszy = null;
- ostatni = null;
- licznik = 1;
- }
- public void addFirst(T newData) {
- ElemOne<T> eo = new ElemOne(newData, null);
- if (pierwszy == null) {
- pierwszy = eo;
- ostatni = eo;
- } else {
- ElemOne<T> tmp = pierwszy;
- pierwszy = eo;
- pierwszy.setNext(tmp);
- }
- licznik++;
- }
- @Override
- public void addLast(T newData) {
- ElemOne<T> last = new ElemOne(newData, null);
- ElemOne<T> tmp = ostatni;
- ostatni = last;
- tmp.setNext(ostatni);
- licznik ++;
- }
- @Override
- public void addAtPosition(T newData, int position) throws ListException {
- if (position == licznik +1){
- addLast(newData);
- return;
- }
- ElemOne<T> ee = new ElemOne(newData,null);
- if (position > licznik){
- } else {
- ElemOne<T> elementAtPosition = getElementAtPosition(position);
- ElemOne<T> prev = getPrev(pierwszy, elementAtPosition);
- if (prev == null){
- //dodanie jako pierwszy
- ElemOne<T> tmp = pierwszy;
- pierwszy = ee;
- pierwszy.setNext(tmp);
- } else {
- prev.setNext(ee);
- ee.setNext(elementAtPosition);
- }
- licznik ++;
- }
- }
- @Override
- public int size() {
- return licznik;
- }
- @Override
- public T removeFirst() throws ListException {
- if(licznik == 0)System.out.println("Pusta lista");
- ElemOne<T> tmp = pierwszy;
- pierwszy = tmp.getNext();
- return tmp.getData(); }
- @Override
- public T removeLast() throws ListException {
- ElemOne<T> eo = new ElemOne(ostatni);
- ElemOne<T> prev = getPrev(pierwszy, ostatni);
- ostatni = prev;
- ostatni.setNext(null);
- return eo.getData();
- }
- @Override
- public T remove(int position) throws ListException {
- ElemOne<T> elementAtPosition = getElementAtPosition(position);
- ElemOne<T> prev = getPrev(pierwszy, elementAtPosition);
- prev.setNext(elementAtPosition.getNext());
- return elementAtPosition.getData();
- }
- @Override
- public int find(T dataToFind) {
- int index = 1;
- ElemOne<T> tmp = pierwszy;
- while(index != licznik){
- if (tmp.getData().equals(dataToFind)){
- return index;
- } else {
- tmp = tmp.getNext();
- index ++;
- }
- }
- return -1;
- }
- @Override
- public boolean contains(T data) {
- ElemOne<T> tmp = pierwszy;
- int index = 0;
- while(index != licznik){
- if (tmp.getData().equals(data)){
- return true;
- } else {
- index ++;
- tmp = tmp.getNext();
- }
- }
- return false;
- }
- @Override
- public void print() {
- ElemOne<T> tmp = pierwszy;
- int index = 1;
- while(tmp!=null){
- System.out.println("element [" + index + "] = " + tmp.getData().toString());
- index ++;
- tmp = tmp.getNext();
- } }
- private ElemOne<T> getPrev(ElemOne<T> iterator, ElemOne<T> tmp) {
- if (iterator.equals(tmp)){
- return null;
- } else{
- if(iterator.getNext() != null){
- while(!iterator.getNext().equals(tmp)){
- if(iterator.getNext() != null){
- iterator = iterator.getNext();
- }
- }
- }
- return iterator;
- }
- }
- private ElemOne<T> getElementAtPosition(int position) {
- int index = 1;
- ElemOne<T> iterator = pierwszy;
- while(index != position){
- iterator = iterator.getNext();
- index ++;
- }
- return iterator;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement