Advertisement
Guest User

Min+

a guest
Mar 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. class ListaRealnih {
  2.    
  3.     class Element {
  4.         double info;
  5.         Element point;
  6.        
  7.         public Element (double nu) {
  8.             this.info = nu;
  9.             this.point = null;
  10.         }
  11.        
  12.         public String toString () {
  13.             return info + (point != null ? " " + point : "");
  14.         }
  15.     }
  16.    
  17.     Element first, last;
  18.    
  19.     public ListaRealnih () {
  20.         this.first = null;
  21.         this.last = null;
  22.     }
  23.    
  24.     public void addFront (double nu) {
  25.         Element el = new Element(nu);
  26.         el.point = first;
  27.         first = el;
  28.         if (last == null) {
  29.             last = first;
  30.         }
  31.     }
  32.    
  33.     public void add (double nu) {
  34.         Element el = new Element(nu);
  35.         if (first == null) {
  36.             addFront(nu);
  37.         }
  38.         else {
  39.             last.point = el;
  40.             last = el;
  41.         }
  42.     }
  43.    
  44.     public void deleteB (double nu) {
  45.         boolean done = false;
  46.         Element runThrough = first.point;
  47.         Element rem = first;
  48.         if (rem.info == nu) {
  49.             first = first.point;
  50.             done = true;
  51.         }
  52.         while (runThrough != null && !done) {
  53.             if (runThrough.info == nu) {
  54.                 rem.point = runThrough.point;
  55.                 done = true;
  56.             }
  57.             else {
  58.                 runThrough = runThrough.point;
  59.                 rem = rem.point;
  60.             }
  61.         }
  62.     }
  63.    
  64.     public void insertCtoB(double nu1, double nu2) {
  65.         Element c = new Element (nu1);
  66.         Element beforeB = new Element(0);
  67.         Element afterB = first;
  68.         boolean none = false;
  69.         while (afterB.point != null) {
  70.             if (afterB.point.info == nu2) {
  71.                 beforeB = afterB;
  72.                 afterB = afterB.point;
  73.                 none = true;
  74.             } else {
  75.                 afterB = afterB.point;
  76.             }
  77.         }
  78.         if (first.info == nu2 && !none) {
  79.             c.point = first.point;
  80.             first = c;
  81.         } else {
  82.             c.point = beforeB.point.point;
  83.             beforeB.point = c;
  84.         }
  85.     }
  86.    
  87.     public void deleteMin() {
  88.         Element beforeMin = first;
  89.         Element runThrough = first;
  90.         while (runThrough.point != null) {
  91.             if (runThrough.point.info < beforeMin.point.info) {
  92.                 beforeMin = runThrough;
  93.                 runThrough = runThrough.point;
  94.             } else {
  95.                 runThrough = runThrough.point;
  96.             }
  97.         }
  98.         if (first.info < beforeMin.info) {
  99.             first = first.point;
  100.         } else {
  101.             beforeMin.point = beforeMin.point.point;
  102.         }
  103.     }
  104.    
  105.     public void firstToLast () {
  106.         last.point = first;
  107.         last = first;
  108.         first = first.point;
  109.         last.point = null;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement