Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********** IMPORTANTE! COMPILARE! **********/
- /* COGNOME:
- * NOME:
- * MATRICOLA:
- * TURNO:
- * NUMERO MACCHINA:
- */
- public class List {
- private Node first;
- public List()
- { first = null; }
- public void insertFirst(int elem)
- { first = new Node(elem, first); }
- public String toString()
- {
- String s = "";
- for (Node p = first; p != null; p = p.getNext()) {
- if (p != first) s += ", ";
- s += p.getElem();
- }
- return s;
- }
- /*
- * eliminaUltimoSePiuGrande
- *
- * Metodo che elimina l'ultimo elemento di una lista se e` il piu`
- * grande.
- *
- * alcuni esempi di risultati attesi
- *
- * lista :
- * atteso :
- *
- * lista : 1
- * atteso :
- *
- * lista : 1, 2
- * atteso : 1
- *
- * lista : 1, 3, 2
- * atteso : 1, 3, 2
- *
- */
- public void eliminaUltimoSePiuGrande() {
- Node succ = null;
- Node prev = null;
- if(first==null) return;
- succ = first;
- int max = 0;
- while(succ!=null){
- if(succ.getElem()>max)
- max = succ.getElem();
- if(succ.getNext()==null ){
- if(prev!=null && succ.getElem()>=max)
- prev.setNext(null);
- }
- else{
- prev = succ;
- succ = prev.getNext();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement