package unit2.slist; public class SList implements IList { public SNode first; public SNode last; int size; //checks if the list is empty public boolean isEmpty() { //return (last==null); is also right return (first == null); } public int getSize() { return size; } //adds an element at the beginning of the list public void addFirst(String newElem) { //creates the new node SNode newNode = new SNode(newElem); //we have link it to the list //newNode will be the new first node, //so it should be before the current first node newNode.next = first; //If list is empty, last is also the newnode if (isEmpty()) last=newNode; //finally, we update first to the new node first = newNode; size++; } //removes the first node of the list public void removeFirst() { if (!isEmpty()) { first = first.next; if (first==null) { //this means, the list only had a node //so, we must set last to null last=null; } size--; } else { //if the list is already empty, we do nothing } } //adds a new element at the end of the list public void addLast(String newElem) { //if the list is empty, we can use the addFirst method if (isEmpty()) addFirst(newElem); else { //creates the node SNode node = new SNode(newElem); //we have to link to the list. //the next node of the current last should be the new node last.next=node; //finally, we set last to the new last node last=node; //increases size size++; } } //remove the last element public void removeLast() { if (!isEmpty()) { //if the list only has an element, //you can use the method removeFirst if (size==1) removeFirst(); else { //if the list has several nodes, //we must find the penultimate node of the list SNode penult=first; while (penult.next!=last) { penult=penult.next; } //here, penult is the penultimate penult.next=null; //sets last to penult, the new last last=penult; //decrease size size--; } } else { //if the list is empty, we do nothing } } //checks if elem exists in the list public boolean contains(String elem) { boolean found=false; for (SNode nodeIt=first;nodeIt!=null&&!found;nodeIt=nodeIt.next) { if (nodeIt.elem.equals(elem)) found=true; } return found; } // //version using a for int i // public boolean contains(String elem) { // boolean found=false; // SNode nodeIt=first; // for (int i=0; i=size) { System.out.println("Index out of range"); } else { int i=0; for (SNode nodeIt=first; nodeIt!=null && result==null; nodeIt=nodeIt.next) { if (i==index) result= nodeIt.elem; i++; } } return result; } //version with for i // public String getAt(int index) { // String result=null; // if (index<0 || index>=size) { // System.out.println("Index out of range"); // } else { // SNode nodeIt=first; // for (int i=1; i<=index; i++) { // nodeIt=nodeIt.next; // } // result= nodeIt.elem; // } // return result; // // } //return the first // public int getIndexOf(String elem) { // int index=0; // boolean found=false; // for (SNode nodeIt=first; nodeIt!=null && !found; nodeIt=nodeIt.next) { // if (nodeIt.elem.equals(elem)) found=true; // else index++; // } // if (!found) { // System.out.println(elem + " is not in the list"); // index=-1; // } // return index; // } public int getIndexOf(String elem) { int index=-1; SNode nodeIt=first; for (int i=0;isize) System.out.println("index out of range"); else if (index==0) addFirst(newElem); else if (index==size) addLast(newElem); else { //we have to reach the previous node SNode previous=first; for (int i=1; i=0) { removeAt(index); index=getIndexOf(elem); } } // public void removeAll(String elem) { // // for (SNode nodeIt=first, prev=null;nodeIt!=null; // prev=nodeIt, nodeIt=nodeIt.next) { // if (nodeIt.elem.equals(elem)) { // if (nodeIt==first) removeFirst(); // else if (nodeIt==last) removeLast(); // else { // prev.next=nodeIt.next; // size--; // } // } // } // } @Override public void removeAt(int index) { if (index<0||index>=size) System.out.println("Index out of range"); else if (index==0) removeFirst(); else if (index==size-1) removeLast(); else { //we have to reach the previous node SNode previous=first; for (int i=1; i2) { list.removeAt(2); System.out.println(list.toString()); } list.insertAt(2, "1"); list.addLast("0"); list.addLast("c"); list.addLast("1"); System.out.println(list.toString()); list.removeAll("0"); System.out.println(list.toString()); list.removeAll("1"); System.out.println(list.toString()); } }