/** * Adds an element to the list, inserting it at the given position. The indeces of elements at and * after that position must be updated accordignly. * * <p>If the index is negative or greater or equal than the size of the list, then an appropriate * error must be returned. * * <p>If a null object is provided to insert in the list, the request must be ignored and an * appropriate error must be returned. * * @param index the position at which the item should be inserted in the list * @param item the value to insert into the list * @return an ReturnObject, empty if the operation is successful or containing an appropriate * error message otherwise */ public ReturnObject add(int index, Object item) { ErrorMessage error1; ReturnObject returnobj; if (item.equals(null)) { error1 = ErrorMessage.INVALID_ARGUMENT; } else if (index < 0 || index >= size_list) { error1 = ErrorMessage.INDEX_OUT_OF_BOUNDS; } else { error1 = ErrorMessage.NO_ERROR; } if (error1.equals(ErrorMessage.NO_ERROR)) { Element elem = startElement; for (int j = 0; j < index + 1; j++) { elem = elem.getNext(); } Element newElement = new Element(item); newElement.setNext(elem.getNext()); elem.setNext(newElement); size_list++; returnobj = new ReturnObjectImpl(item); } else { returnobj = new ReturnObjectImpl(error1); } return returnobj; }
public void push(String value) { // wstaw do kolejki if (_first.getValue() == null) _first.setValue(value); else { Element e = _first; while (e.getNext() != null) e = e.getNext(); e.setNext(new Element(value)); } _size++; }
/** * Removes the inner list row elements if the element's value is set to the default value. * * @param currentRow * @param row * @param col */ public void removeRowElement(RowColumnElement currentRow, int row, int col) { Element rowElementHead = currentRow.getNext().getList(); while (rowElementHead.getNext() != null) { if (rowElementHead.getNext().rowIndex() == row) { rowElementHead.setNext(rowElementHead.getNext().getNext()); break; } rowElementHead = rowElementHead.getNext(); } removeEmptyRow(currentRow, row); }
public String peek() throws EmptyStackException { // tylko pokaz try { if (_size == 0) throw new EmptyStackException(); } catch (EmptyStackException e) { e.komunikat(); } Element e = _first; while (e.getNext() != null) { e = e.getNext(); } System.out.println("\nOstatni element ze stosu: " + e.getValue()); pokaz(); return e.getValue(); }
public void setElement(int number, int data) { if (number >= getVectorSize() || number < MIN_VECTOR_SIZE) throw new VectorIndexOutOfBoundsException(); Element current = head; for (int i = -1; i < number; i++) current = current.getNext(); current.setField(data); }
public void deleteElement(int number) { if (number > getVectorSize() && number < MIN_VECTOR_SIZE) throw new VectorIndexOutOfBoundsException(); Element current = head; for (int i = -1; i < number; i++) current = current.getNext(); if (current == head) { head = current.getNext(); head.setPrev(null); } else if (current == tail) { tail = current.getPrev(); tail.setNext(null); } else { current.getPrev().setNext(current.getNext()); current.getNext().setPrev(current.getPrev()); } }
public String pop() throws EmptyStackException { // pobierz i wywal try { if (_size == 0) throw new EmptyStackException(); } catch (EmptyStackException e) { e.komunikat(); } Element e = _first; while (e.getNext().getNext() != null) { e = e.getNext(); } System.out.println("\nZdjecie ostatniego elementu ze stosu: " + e.getNext().getValue()); e.setNext(null); _size--; pokaz(); return e.getValue(); }
/** Returns true if there are more elements in the row or column */ @Override public boolean hasNext() { if (iteratingRow() == true) { if (currentListHead.getNext() != null) { currentListHead = currentListHead.getNext(); return true; } else { return false; } } else { if (currentListHead.getBottom() != null) { currentListHead = currentListHead.getBottom(); return true; } else { return false; } } }
public int getVectorSize() { int result = 0; Element current = head; while (current != tail) { current = current.getNext(); result++; } return result; }
public void pokaz() { System.out.println(""); Element e = _first; int i = 1; while (e != null) { System.out.println(i + ". " + e.getValue()); i++; e = e.getNext(); } }
public double euclideanNorm() { double result = 0; Element current = head; while (current != null) { result += current.getField() * current.getField(); current = current.getNext(); } result = Math.sqrt(result); return result; }
/** * Returns the element at the beginning of the list. * * <p>If the list is empty, an appropriate error is returned. * * @return a copy of the element at the beginning of the list or an error if the list is empty. */ public ReturnObject head() { ErrorMessage error1 = errortype(0); ReturnObject returnobj; if (error1.equals(ErrorMessage.NO_ERROR)) { Element elem = startElement; returnobj = new ReturnObjectImpl(elem.getNext().returnObject()); } else { returnobj = new ReturnObjectImpl(error1); } return returnobj; }
/** * Returns the elements at the given position and removes it from the list. The indeces of * elements after the removed element must be updated accordignly. * * <p>If the index is negative or greater or equal than the size of the list, then an appropriate * error must be returned. * * @param index the position in the list of the item to be retrieved * @return the element or an appropriate error message, encapsulated in a ReturnObject */ public ReturnObject remove(int index) { ErrorMessage error1 = errortype(index); ReturnObject returnobj; if (error1.equals(ErrorMessage.NO_ERROR) && size_list > 0) { Element elem = startElement; // Element elem2=startElement; for (int j = 0; j < index + 1; j++) { elem = elem.getNext(); } elem.setNext(elem.getNext().getNext()); returnobj = new ReturnObjectImpl(elem.getNext().returnObject()); size_list--; } else { returnobj = new ReturnObjectImpl(error1); } return returnobj; }
/** * Returns the element at the given position. * * <p>If the index is negative or greater or equal than the size of the list, then an appropriate * error must be returned. * * @param index the position in the list of the item to be retrieved * @return the element or an appropriate error message, encapsulated in a ReturnObject */ public ReturnObject get(int index) { ErrorMessage error1 = errortype(index); ReturnObject returnobj; if (error1.equals(ErrorMessage.NO_ERROR)) { Element elem = startElement; for (int j = 0; j < index + 1; j++) { elem = elem.getNext(); } returnobj = new ReturnObjectImpl(elem.returnObject()); } else { returnobj = new ReturnObjectImpl(error1); } return returnobj; }
/** * Updates the row's inner list elements if the row element already exists or if a new row was * added. * * @param currentRowEle * @param elem * @param row * @param col * @param value */ public void updateRowListElements( RowColumnElement currentRowEle, Element elem, int row, int col, Object value) { Element temp = elem; Element currentList = currentRowEle.getList(); int elemColIdx = elem.columnIndex(); while (currentList.getNext() != null && currentList.getNext().columnIndex() < elemColIdx) { currentList = currentList.getNext(); } if (currentList.getNext() != null && currentList.getNext().columnIndex() == elemColIdx) { currentList.getNext().value = elem.value; return; } temp.setNext(currentList.getNext()); currentList.setNext(temp); }
/** * Adds an element at the end of the list. * * <p>If a null object is provided to insert in the list, the request must be ignored and an * appropriate error must be returned. * * @param item the value to insert into the list * @return an ReturnObject, empty if the operation is successful or containing an appropriate * error message otherwise */ public ReturnObject add(Object item) { ReturnObject returnobj; if (item.equals(null)) { returnobj = new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT); } else { returnobj = new ReturnObjectImpl(item); Element elem = startElement; for (int j = 0; j < size_list; j++) { elem = elem.getNext(); } Element newElement = new Element(item); elem.setNext(newElement); size_list++; } return returnobj; }
/** * Removes any row elements that do not contain inner list elements leaving the row empty. * * @param currentRow * @param row */ public void removeEmptyRow(RowColumnElement currentRow, int row) { Element currentList = currentRow.getNext().getList(); if (currentList.getNext() == null) { currentRow.setNext(currentRow.getNext().getNext()); } }