Advertisement
SlavkovB

[АПС] Бришење прв елемент

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