mitkomitrov

За Пецо, Карпош 4

Oct 17th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. package petar;
  2.  
  3. import java.util.*;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedInputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import java.io.IOException;
  9.  
  10. import java.util.Iterator;
  11. import java.util.NoSuchElementException;
  12.  
  13. class SLL<E> {
  14.     private SLLNode<E> first;
  15.  
  16.     public SLL() {
  17.         // Construct an empty SLL
  18.         first = null;
  19.     }
  20.  
  21.     public void deleteList() {
  22.         first = null;
  23.     }
  24.  
  25.     public int length() {
  26.         int ret;
  27.         if (first != null) {
  28.             SLLNode<E> tmp = first;
  29.             ret = 1;
  30.             while (tmp.succ != null) {
  31.                 tmp = tmp.succ;
  32.                 ret++;
  33.             }
  34.             return ret;
  35.         } else
  36.             return 0;
  37.  
  38.     }
  39.  
  40.     @Override
  41.     public String toString() {
  42.         String ret = new String();
  43.         if (first != null) {
  44.             SLLNode<E> tmp = first;
  45.             ret += tmp + "->";
  46.             while (tmp.succ != null)
  47.                         {
  48.                             tmp = tmp.succ;
  49.                 ret += tmp + "->";
  50.             }
  51.         } else
  52.             ret = "Prazna lista!!!";
  53.         return ret;
  54.     }
  55.  
  56.     public void insertFirst(E o) {
  57.         SLLNode<E> ins = new SLLNode<E>(o, first);
  58.         first = ins;
  59.     }
  60.  
  61.     public void insertAfter(E o, SLLNode<E> node) {
  62.         if (node != null) {
  63.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  64.             node.succ = ins;
  65.         } else {
  66.             System.out.println("Dadenot jazol e null");
  67.         }
  68.     }
  69.  
  70.     public void insertBefore(E o, SLLNode<E> before) {
  71.        
  72.         if (first != null) {
  73.             SLLNode<E> tmp = first;
  74.             if(first==before){
  75.                 this.insertFirst(o);
  76.                 return;
  77.             }
  78.             //ako first!=before
  79.             while (tmp.succ != before)
  80.                 tmp = tmp.succ;
  81.             if (tmp.succ == before) {
  82.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  83.                 tmp.succ = ins;
  84.             } else {
  85.                 System.out.println("Elementot ne postoi vo listata");
  86.             }
  87.         } else {
  88.             System.out.println("Listata e prazna");
  89.         }
  90.     }
  91.  
  92.     public void insertLast(E o) {
  93.         if (first != null) {
  94.             SLLNode<E> tmp = first;
  95.             while (tmp.succ != null)
  96.                 tmp = tmp.succ;
  97.             SLLNode<E> ins = new SLLNode<E>(o, null);
  98.             tmp.succ = ins;
  99.         } else {
  100.             insertFirst(o);
  101.         }
  102.     }
  103.  
  104.     public E deleteFirst() {
  105.         if (first != null) {
  106.             SLLNode<E> tmp = first;
  107.             first = first.succ;
  108.             return tmp.element;
  109.         } else {
  110.             System.out.println("Listata e prazna");
  111.             return null;
  112.         }
  113.     }
  114.  
  115.     public E delete(SLLNode<E> node) {
  116.         if (first != null) {
  117.             SLLNode<E> tmp = first;
  118.             if(first ==node){
  119.                 return this.deleteFirst();
  120.             }
  121.             while (tmp.succ != node && tmp.succ.succ != null)
  122.                 tmp = tmp.succ;
  123.             if (tmp.succ == node) {
  124.                 tmp.succ = tmp.succ.succ;
  125.                 return node.element;
  126.             } else {
  127.                 System.out.println("Elementot ne postoi vo listata");
  128.                 return null;
  129.             }
  130.         } else {
  131.             System.out.println("Listata e prazna");
  132.             return null;
  133.         }
  134.  
  135.     }
  136.  
  137.     public SLLNode<E> getFirst() {
  138.         return first;
  139.     }
  140.    
  141.     public SLLNode<E> find(E o) {
  142.         if (first != null) {
  143.             SLLNode<E> tmp = first;
  144.             while (tmp.element != o && tmp.succ != null)
  145.                 tmp = tmp.succ;
  146.             if (tmp.element == o) {
  147.                 return tmp;
  148.             } else {
  149.                 System.out.println("Elementot ne postoi vo listata");
  150.             }
  151.         } else {
  152.             System.out.println("Listata e prazna");
  153.         }
  154.         return first;
  155.     }
  156.    
  157.     public Iterator<E> iterator () {
  158.     // Return an iterator that visits all elements of this list, in left-to-right order.
  159.         return new LRIterator<E>();
  160.     }
  161.  
  162.     // //////////Inner class ////////////
  163.  
  164.     private class LRIterator<E> implements Iterator<E> {
  165.  
  166.         private SLLNode<E> place, curr;
  167.  
  168.         private LRIterator() {
  169.             place = (SLLNode<E>) first;
  170.             curr = null;
  171.         }
  172.  
  173.         public boolean hasNext() {
  174.             return (place != null);
  175.         }
  176.  
  177.         public E next() {
  178.             if (place == null)
  179.                 throw new NoSuchElementException();
  180.             E nextElem = place.element;
  181.             curr = place;
  182.             place = place.succ;
  183.             return nextElem;
  184.         }
  185.  
  186.         public void remove() {
  187.             //Not implemented
  188.         }
  189.     }
  190.    
  191.     public void mirror(){
  192.         if (first != null) {
  193.             //m=nextsucc, p=tmp,q=next
  194.             SLLNode<E> tmp = first;
  195.             SLLNode<E> newsucc = null;
  196.             SLLNode<E> next;
  197.            
  198.             while(tmp != null){
  199.                 next = tmp.succ;
  200.                 tmp.succ = newsucc;
  201.                 newsucc = tmp;
  202.                 tmp = next;
  203.             }
  204.             first = newsucc;
  205.         }
  206.        
  207.     }
  208.    
  209.     public void merge (SLL<E> in){
  210.         if (first != null) {
  211.             SLLNode<E> tmp = first;
  212.             while(tmp.succ != null)
  213.                 tmp = tmp.succ;
  214.             tmp.succ = in.getFirst();
  215.         }
  216.         else{
  217.             first = in.getFirst();
  218.         }
  219.     }
  220. }
  221.  
  222.  
  223.  
  224. class SLLNode<E> {
  225.      E element;
  226.      SLLNode<E> succ;
  227.  
  228.     public SLLNode(E elem, SLLNode<E> succ) {
  229.         this.element = elem;
  230.         this.succ = succ;
  231.     }
  232.  
  233.     @Override
  234.     public String toString() {
  235.         return element.toString();
  236.     }
  237. }
  238.  
  239. public class Petar {
  240.    
  241.     public static String prvElement(SLL<Integer> lista)
  242.     {
  243.         SLLNode<Integer> current = lista.getFirst();
  244.         return "Prviot element e: " + current.element;
  245.        
  246.     }
  247.     public static void brisiNeparni(SLL<Integer> lista)
  248.     {
  249.         SLLNode<Integer> current = lista.getFirst();
  250.         while(current != null)
  251.         {
  252.             SLLNode<Integer> next = current.succ;
  253.             if(next.element % 2 != 0)
  254.             {
  255.                current.succ = next.succ;
  256.             }
  257.             current = current.succ;
  258.         }
  259.     }
  260.     public static void main(String[] args) throws IOException
  261.     {
  262.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  263.        
  264.         SLL<Integer> lista1 = new SLL();
  265.         int broj = Integer.parseInt(input.readLine()); //broj na JAZLI vo listata
  266.         System.out.println("Vnesi: ");
  267.        
  268.         String line = input.readLine();
  269.        
  270.         String[] pom = line.split(" ");
  271.         for(int i = 0; i < broj; i++)
  272.         {
  273.             lista1.insertLast(Integer.parseInt(pom[i]));
  274.         }
  275.                
  276.                
  277.                 System.out.println(prvElement(lista1));
  278.                 brisiNeparni(lista1);
  279.                 System.out.println(lista1);
  280.                
  281.     }
  282.    
  283. }
Add Comment
Please, Sign In to add comment