Advertisement
jovanovski

НЛ Лаб5 Зад2

Nov 18th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3.  
  4. class ArrayIndexOutOfBoundsException extends Exception{
  5.     public ArrayIndexOutOfBoundsException(){
  6.         super();
  7.     }
  8. }
  9.  
  10. class IntegerList{
  11.    
  12.     LinkedList<Integer> list;
  13.    
  14.     public IntegerList(){
  15.         list = new LinkedList<Integer>();
  16.     }
  17.    
  18.     public IntegerList(Integer[] niza){
  19.         list = new LinkedList<Integer>();
  20.         for(int i=0;i<niza.length;i++){
  21.             list.add(niza[i]);
  22.         }
  23.     }
  24.    
  25.     public void add(int broj, int index){
  26.         list.add(index, broj);
  27.     }
  28.    
  29.     public void remove(int index) throws ArrayIndexOutOfBoundsException{
  30.         if (index>list.size()-1 || index<0) throw new ArrayIndexOutOfBoundsException();
  31.         list.remove(index);
  32.     }
  33.    
  34.     public void set(int broj, int index) throws ArrayIndexOutOfBoundsException{
  35.         if (index>list.size()-1 || index<0) throw new ArrayIndexOutOfBoundsException();
  36.         list.set(index, broj);
  37.     }
  38.    
  39.     public int get(int index) throws ArrayIndexOutOfBoundsException{
  40.         if (index>list.size()-1 || index<0) throw new ArrayIndexOutOfBoundsException();
  41.         return list.get(index);
  42.     }
  43.    
  44.     public int count(int broj){
  45.         int brojac = 0;
  46.         for(int i=0;i<list.size();i++){
  47.             if(list.get(i)==broj) brojac++;
  48.         }      
  49.         return brojac;
  50.     }
  51.    
  52.     public void removeDuplicates(){
  53.         for(int i=list.size()-1;i>0;i--){
  54.             for(int j=i-1;j>=0;j--){
  55.                 if(list.get(j)==list.get(i)) list.remove(j);
  56.             }
  57.         }
  58.     }
  59.    
  60.     public int size(){
  61.         return list.size();
  62.     }
  63.    
  64.     public int sumFirst(int k) throws ArrayIndexOutOfBoundsException{
  65.         if (k>list.size()-1 || k<0) throw new ArrayIndexOutOfBoundsException();
  66.         int suma = 0;
  67.         for(int i=0;i<k;i++){
  68.             suma+=list.get(i);
  69.         }
  70.         return suma;
  71.     }
  72.    
  73.     public int sumLast(int k) throws ArrayIndexOutOfBoundsException{
  74.         if (k>list.size()-1 || k<0) throw new ArrayIndexOutOfBoundsException();
  75.         int suma = 0;
  76.         for(int i=list.size()-1;i>list.size()-1-k;i--){
  77.             suma+=list.get(i);
  78.         }
  79.         return suma;
  80.     }
  81.    
  82.     public IntegerList addValue(int n){
  83.         IntegerList nov = new IntegerList();
  84.         for(int i=0;i<list.size();i++){
  85.             nov.add(i, list.get(i)+n);
  86.         }
  87.         return nov;
  88.     }
  89.    
  90.     public void shiftRight(int index, int k){
  91.         if(index+k<=list.size()-1){
  92.             list.add(index+k, list.get(index));
  93.             list.remove(index);
  94.         }
  95.         else{
  96.             int novapozicija = (index+k)%list.size();
  97.             list.add(novapozicija, list.get(index));
  98.             if(novapozicija<=index) list.remove(index+1);
  99.             else list.remove(index);
  100.         }
  101.     }
  102.    
  103.     public void shiftLeft(int index, int k){
  104.         if(k<=list.size()){
  105.             shiftRight(index, k-list.size());
  106.         }
  107.         else{
  108.             shiftRight(index, k%list.size());
  109.         }
  110.     }
  111.    
  112.    
  113. }
  114.  
  115.  
  116. public class IntegerListTest {
  117.    
  118.     public static void main(String[] args) {
  119.         Scanner jin = new Scanner(System.in);
  120.         int k = jin.nextInt();
  121.         if ( k == 0 ) { //test standard methods
  122.             int subtest = jin.nextInt();
  123.             if ( subtest == 0 ) {
  124.                 IntegerList list = new IntegerList();
  125.                 while ( true ) {
  126.                     int num = jin.nextInt();
  127.                     if ( num == 0 ) {
  128.                         list.add(jin.nextInt(), jin.nextInt());
  129.                     }
  130.                     if ( num == 1 ) {
  131.                         try {
  132.                             list.remove(jin.nextInt());
  133.                         } catch (ArrayIndexOutOfBoundsException e) {
  134.                             // TODO Auto-generated catch block
  135.                             e.printStackTrace();
  136.                         }
  137.                     }
  138.                     if ( num == 2 ) {
  139.                         print(list);
  140.                     }
  141.                     if ( num == 3 ) {
  142.                         break;
  143.                     }
  144.                 }
  145.             }
  146.             if ( subtest == 1 ) {
  147.                 int n = jin.nextInt();
  148.                 Integer a[] = new Integer[n];
  149.                 for ( int i = 0 ; i < n ; ++i ) {
  150.                     a[i] = jin.nextInt();
  151.                 }
  152.                 IntegerList list = new IntegerList(a);
  153.                 print(list);
  154.             }
  155.         }
  156.         if ( k == 1 ) { //test count,remove duplicates, addValue
  157.             int n = jin.nextInt();
  158.             Integer a[] = new Integer[n];
  159.             for ( int i = 0 ; i < n ; ++i ) {
  160.                 a[i] = jin.nextInt();
  161.             }
  162.             IntegerList list = new IntegerList(a);
  163.             while ( true ) {
  164.                 int num = jin.nextInt();
  165.                 if ( num == 0 ) { //count
  166.                     System.out.println(list.count(jin.nextInt()));
  167.                 }
  168.                 if ( num == 1 ) {
  169.                     list.removeDuplicates();
  170.                 }
  171.                 if ( num == 2 ) {
  172.                     print(list.addValue(jin.nextInt()));
  173.                 }
  174.                 if ( num == 3 ) {
  175.                     list.add(jin.nextInt(), jin.nextInt());
  176.                 }
  177.                 if ( num == 4 ) {
  178.                     print(list);
  179.                 }
  180.                 if ( num == 5 ) {
  181.                     break;
  182.                 }
  183.             }
  184.         }
  185.         if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
  186.             int n = jin.nextInt();
  187.             Integer a[] = new Integer[n];
  188.             for ( int i = 0 ; i < n ; ++i ) {
  189.                 a[i] = jin.nextInt();
  190.             }
  191.             IntegerList list = new IntegerList(a);
  192.             while ( true ) {
  193.                 int num = jin.nextInt();
  194.                 if ( num == 0 ) { //count
  195.                     list.shiftLeft(jin.nextInt(), jin.nextInt());
  196.                 }
  197.                 if ( num == 1 ) {
  198.                     list.shiftRight(jin.nextInt(), jin.nextInt());
  199.                 }
  200.                 if ( num == 2 ) {
  201.                     try {
  202.                         System.out.println(list.sumFirst(jin.nextInt()));
  203.                     } catch (ArrayIndexOutOfBoundsException e) {
  204.                         // TODO Auto-generated catch block
  205.                         e.printStackTrace();
  206.                     }
  207.                 }
  208.                 if ( num == 3 ) {
  209.                     try {
  210.                         System.out.println(list.sumLast(jin.nextInt()));
  211.                     } catch (ArrayIndexOutOfBoundsException e) {
  212.                         // TODO Auto-generated catch block
  213.                         e.printStackTrace();
  214.                     }
  215.                 }
  216.                 if ( num == 4 ) {
  217.                     print(list);
  218.                 }
  219.                 if ( num == 5 ) {
  220.                     break;
  221.                 }
  222.             }
  223.         }
  224.     }
  225.    
  226.     public static void print(IntegerList il) {
  227.         if ( il.size() == 0 ) System.out.print("EMPTY");
  228.         for ( int i = 0 ; i < il.size() ; ++i ) {
  229.             if ( i > 0 ) System.out.print(" ");
  230.             try {
  231.                 System.out.print(il.get(i));
  232.             } catch (ArrayIndexOutOfBoundsException e) {
  233.                 System.out.println("ArrayIndexOutOfBoundsException");
  234.             }
  235.         }
  236.         System.out.println();
  237.     }
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement