Advertisement
Guest User

ZbirParni

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