Advertisement
Guest User

Untitled

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