Advertisement
blackpab

ListaUporzadkowana

Jan 12th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. package listod;
  2.  
  3. public class ListoD {
  4.  
  5.     private ListElem first;
  6.     private ListElem last;
  7.  
  8.     public ListoD() {
  9.         first = null;
  10.         last = null;
  11.     }
  12.  
  13.     public boolean isEmpty() {
  14.         return (first == null);
  15.     }
  16.  
  17.     public void print() {
  18.         ListElem current = first;
  19.  
  20.         while (current != null) {
  21.             System.out.print(current + " ");
  22.             current = current.next;
  23.         }
  24.         System.out.println("");
  25.     }
  26.  
  27.     public void insert(int value) {
  28.         ListElem newElem = new ListElem(value); //tworzymy nowy element listy
  29.  
  30.         //lista jest pusta
  31.         if (isEmpty()) {
  32.             first = newElem;
  33.             last = newElem;
  34.         } else { //lista ma co najmniej jeden element
  35.             ListElem current = first; //aktualny element
  36.  
  37.             while (current != null) {
  38.                 if (value < current.data) {
  39.                     break;
  40.                 }                
  41.                 current = current.next;                
  42.             }
  43.            
  44.             //po wyjsciu z petli w miejsce current musimy wstawic nowy element            
  45.             if(current == null) { //tzn ze bedziemy wstawiac element na koncu listy
  46.                 last.next = newElem;
  47.                 newElem.previous = last;              
  48.                 last = newElem;
  49.             } else if(current.previous == null) { //tzn ze bedziemy wstawiac element na poczatku listy
  50.                 first.previous = newElem;
  51.                 newElem.next = first;
  52.                 first = newElem;
  53.             } else {
  54.                 //ani pierwszy ani ostatni
  55.                 newElem.next = current;
  56.                 newElem.previous = current.previous;
  57.                 current.previous.next = newElem;
  58.                 current.previous = newElem;                            
  59.             }                                      
  60.         }      
  61.     }
  62.  
  63.     public ListElem deleteFirst() {
  64.         if (isEmpty()) {
  65.             return null;
  66.         }
  67.         ListElem temp = first;
  68.         if (first.next == null) {
  69.             last = null;
  70.         } else {
  71.             first.next.previous = null;
  72.         }
  73.         first = first.next;
  74.         return temp;
  75.     }
  76.  
  77.     public ListElem deleteLast() {
  78.         if (isEmpty()) {
  79.             return null;
  80.         }
  81.         ListElem temp = last;
  82.         if (first.next == null) {
  83.             first = null;
  84.         } else {
  85.             last.previous.next = null;
  86.         }
  87.         last = last.previous;
  88.         return temp;
  89.     }
  90.    
  91.     public ListElem find(int value){
  92.         if(isEmpty()) return null;
  93.         ListElem current = first;
  94.        
  95.         while(current != null) {
  96.             if(current.data == value){
  97.                 return current;
  98.             }
  99.             current = current.next;
  100.         }
  101.         return null;
  102.     }
  103.    
  104.     public ListElem finds(int elem)     // Wyszukiwanie elementu
  105.     {
  106.         if (isEmpty()) return null;
  107.  
  108.         ListElem current = first;  // Rozpoczynamy od pierwszego elementu
  109.         while (current.data != elem)
  110.         {
  111.             if (current.next == null) return null;
  112.             else
  113.                 current = current.next;
  114.         }
  115.         return current;
  116.     }
  117.  
  118.     public static void main(String[] args) {
  119.         ListoD theList = new ListoD();
  120.  
  121.         theList.insert(22);       // wstawiamy na początek
  122.         theList.insert(44);
  123.         theList.insert(33);
  124.         theList.print();         // wypisujemy zawartość listy
  125.  
  126.         theList.insert(11);        // wstawiamy na koniec
  127.         theList.insert(33);
  128.         theList.insert(55);
  129.         theList.insert(2);
  130.         theList.insert(100);
  131.         theList.insert(99);
  132.         theList.insert(3);
  133.         theList.insert(1);
  134.         theList.insert(1);
  135.         theList.insert(1);
  136.         theList.insert(1000);
  137.         theList.insert(1000);
  138.         theList.insert(1000);
  139.        
  140.         theList.print();         // wypisujemy zawartość listy
  141.  
  142.         theList.deleteFirst();         // usuwamy pierwsze dwa elementy
  143.         theList.deleteFirst();
  144.  
  145.         theList.print();         // wypisujemy ponownie
  146.  
  147.         theList.deleteLast(); //usuwamy ostatni element
  148.  
  149.         theList.print();         // wypisujemy ponownie
  150.        
  151.         System.out.println(theList.find(-9));
  152.         System.out.println(theList.find(22));
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement