SlavkovB

[АПС] Термин 3 Септември 2019

Sep 19th, 2019
1,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. /**
  2. За дадена листа од А0 до Аn да се испечати како резултат А0->Аn-1->А1->Аn-2->А2->...
  3.        
  4. Влез:
  5. 5
  6. 1 2 3 4 5
  7. Излез:
  8. 1->5->2->4->3
  9. Забелешка:
  10. Не смеат да се креираат нови листи
  11. Работа со јазлите
  12. */
  13.  
  14.  
  15. //CODE
  16.  
  17.  
  18. import java.util.Scanner;
  19.  
  20. class SLLNode<E> {
  21.     protected E element;
  22.     protected SLLNode<E> succ;
  23.  
  24.     public SLLNode(E elem, SLLNode<E> succ) {
  25.         this.element = elem;
  26.         this.succ = succ;
  27.     }
  28.  
  29.     @Override
  30.     public String toString() {
  31.         return element.toString();
  32.     }
  33. }
  34.  
  35. class SLL<E> {
  36.     private SLLNode<E> first;
  37.  
  38.     public SLL() {
  39.         // Construct an empty SLL
  40.         this.first = null;
  41.     }
  42.  
  43.     public void deleteList() {
  44.         first = null;
  45.     }
  46.  
  47.     public int length() {
  48.         int ret;
  49.         if (first != null) {
  50.             SLLNode<E> tmp = first;
  51.             ret = 1;
  52.             while (tmp.succ != null) {
  53.                 tmp = tmp.succ;
  54.                 ret++;
  55.             }
  56.             return ret;
  57.         } else
  58.             return 0;
  59.  
  60.     }
  61.  
  62.     @Override
  63.     public String toString() {
  64.         String ret = new String();
  65.         if (first != null) {
  66.             SLLNode<E> tmp = first;
  67.             if (tmp.succ == null)
  68.                 return ret + tmp.element;
  69.             ret += tmp + "->";
  70.             while (tmp.succ != null) {
  71.                 tmp = tmp.succ;
  72.                 if (tmp.succ == null)
  73.                     return ret + tmp.element;
  74.                 else
  75.                     ret += tmp + "->";
  76.             }
  77.         } else
  78.             ret = "Prazna lista!!!";
  79.         return ret;
  80.     }
  81.  
  82.     public void setFirst(SLLNode<E> first) {
  83.         this.first = first;
  84.     }
  85.  
  86.     public void insertFirst(E o) {
  87.         SLLNode<E> ins = new SLLNode<E>(o, first);
  88.         first = ins;
  89.     }
  90.  
  91.     public void insertAfter(E o, SLLNode<E> node) {
  92.         if (node != null) {
  93.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  94.             node.succ = ins;
  95.         } else {
  96.             System.out.println("Dadenot jazol e null");
  97.         }
  98.     }
  99.  
  100.     public void insertBefore(E o, SLLNode<E> before) {
  101.  
  102.         if (first != null) {
  103.             SLLNode<E> tmp = first;
  104.             if (first == before) {
  105.                 this.insertFirst(o);
  106.                 return;
  107.             }
  108.             // ako first!=before
  109.             while (tmp.succ != before)
  110.                 tmp = tmp.succ;
  111.             if (tmp.succ == before) {
  112.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  113.                 tmp.succ = ins;
  114.             } else {
  115.                 System.out.println("Elementot ne postoi vo listata");
  116.             }
  117.         } else {
  118.             System.out.println("Listata e prazna");
  119.         }
  120.     }
  121.  
  122.     public void insertLast(E o) {
  123.         if (first != null) {
  124.             SLLNode<E> tmp = first;
  125.             while (tmp.succ != null)
  126.                 tmp = tmp.succ;
  127.             SLLNode<E> ins = new SLLNode<E>(o, null);
  128.             tmp.succ = ins;
  129.         } else {
  130.             insertFirst(o);
  131.         }
  132.     }
  133.  
  134.     public E deleteFirst() {
  135.         if (first != null) {
  136.             SLLNode<E> tmp = first;
  137.             first = first.succ;
  138.             return tmp.element;
  139.         } else {
  140.             System.out.println("Listata e prazna");
  141.             return null;
  142.         }
  143.     }
  144.  
  145.     public E delete(SLLNode<E> node) {
  146.         if (first != null) {
  147.             SLLNode<E> tmp = first;
  148.             if (first == node) {
  149.                 return this.deleteFirst();
  150.             }
  151.             while (tmp.succ != node && tmp.succ.succ != null)
  152.                 tmp = tmp.succ;
  153.             if (tmp.succ == node) {
  154.                 tmp.succ = tmp.succ.succ;
  155.                 return node.element;
  156.             } else {
  157.                 System.out.println("Elementot ne postoi vo listata");
  158.                 return null;
  159.             }
  160.         } else {
  161.             System.out.println("Listata e prazna");
  162.             return null;
  163.         }
  164.  
  165.     }
  166.  
  167.     public SLLNode<E> getFirst() {
  168.         return first;
  169.     }
  170.  
  171.     public SLLNode<E> find(E o) {
  172.         if (first != null) {
  173.             SLLNode<E> tmp = first;
  174.             while (tmp.element != o && tmp.succ != null)
  175.                 tmp = tmp.succ;
  176.             if (tmp.element == o) {
  177.                 return tmp;
  178.             } else {
  179.                 System.out.println("Elementot ne postoi vo listata");
  180.             }
  181.         } else {
  182.             System.out.println("Listata e prazna");
  183.         }
  184.         return first;
  185.     }
  186.  
  187. }
  188.  
  189. public class ChangeList {
  190.  
  191.     public static void main(String[] args) {
  192.         Scanner input = new Scanner(System.in);
  193.         SLL<Integer> list = new SLL<Integer>();
  194.  
  195.         int n = input.nextInt();
  196.  
  197.         for (int i = 0; i < n; i++)
  198.             list.insertLast(input.nextInt());
  199.  
  200.         ChangeList(list);
  201.         input.close();
  202.     }
  203.  
  204.     private static void ChangeList(SLL<Integer> list) {
  205.         SLLNode<Integer> current = list.getFirst();
  206.         SLLNode<Integer> next;
  207.  
  208.         while (current.succ != null) {
  209.             next = current.succ;
  210.  
  211.             while (next.succ != null)
  212.                 next = next.succ;
  213.  
  214.             current = current.succ;
  215.             list.insertBefore(next.element, current);
  216.             list.delete(next);
  217.         }
  218.         System.out.println(list);
  219.     }
  220. }
Add Comment
Please, Sign In to add comment