/** * 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; }
/** * 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); }
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 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(); }
/** * 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; }
/** * 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; }