SHARE
TWEET

Untitled

a guest Nov 14th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2.  
  3. public class sumList {
  4.  
  5.     public static int sumOfList(Node<Integer> first){
  6.         // The procedure receives a list of numbers and returns their sum
  7.         int sum= 0;
  8.         Node<Integer> current;
  9.         current= first;
  10.         while(current!=null){
  11.             sum+= current.getValue();
  12.             current= current.getNext();
  13.         }
  14.         return sum;
  15.     }
  16.     public static int findMin(Node<Integer> first){
  17.         // The procedure receives a list of numbers and returns the smallest number
  18.         Node<Integer> current= first;
  19.         int min= current.getValue();
  20.         while(current!=null){
  21.             if(current.getValue()<min){
  22.                 min= current.getValue();
  23.             }
  24.             current= current.getNext();
  25.         }
  26.         return min;
  27.     }
  28.     public static int findMaxPos(Node<Integer> first){
  29.         // The procedure receives a list of numbers and returns the position of the largest number
  30.         Node<Integer> current= first;
  31.         int position= 0;
  32.         int times= 0;
  33.         int max= current.getValue();
  34.         while(current!=null){
  35.             if(current.getValue()>max){
  36.                 max= current.getValue();
  37.                 position= times;
  38.             }
  39.             current= current.getNext();
  40.             times+=1;
  41.         }
  42.         return position;
  43.     }
  44.     public static Node<Integer> addLast(Node<Integer> first, int val){
  45.         // The procedure receives a list of numbers and a number and adds that number to the end of the list
  46.         Node<Integer> current;
  47.         current= first;
  48.         if(first==null){
  49.             return new Node<Integer>(val);
  50.         }
  51.         while(current.hasNext()){
  52.             current= current.getNext();
  53.         }
  54.         current.setNext(new Node<Integer>(val));
  55.         return first;
  56.     }
  57.     public static void printSize(Node<Integer> first){
  58.         // The procedure receives a list of numbers and prints the number of numbers in the list
  59.         Node<Integer> current;
  60.         int times= 0;
  61.         current= first;
  62.         while(current!=null){
  63.             times+= 1;
  64.             current= current.getNext();
  65.         }
  66.         System.out.println(times);
  67.     }
  68.     public static void equalsValue(Node<Integer> first, int val){
  69.         // The procedure receives a list of numbers and prints the number of numbers in the list
  70.         Node<Integer> current;
  71.         int times= 0;
  72.         current= first;
  73.         while(current!=null){
  74.             if(current.getValue()==val){
  75.                 times+= 1;
  76.             }
  77.             current= current.getNext();
  78.         }
  79.         System.out.println(times);
  80.     }
  81.     public static boolean isInList(Node<Integer> first, int val){
  82.         // The procedure receives a list of numbers and prints the number of numbers in the list
  83.         while(first!=null){
  84.             if(first.getValue()==val){
  85.                 return true;
  86.             }
  87.             first= first.getNext();
  88.         }
  89.         return false;
  90.     }
  91.     public static Node<Integer> listCombine(Node<Integer> first1, Node<Integer> first2){
  92.         Node<Integer> first3= null, current1, current2, current3;
  93.         int num;
  94.         current1= first1;current2= first2;current3=first3;
  95.         while(current1!=null){
  96.             num= current1.getValue();
  97.             while(current2!=null){
  98.                 if(num== current2.getValue() && !isInList(first3, num))
  99.                     first3= addLast(first3, num);
  100.                 current2= current2.getNext();
  101.             }
  102.             current1= current1.getNext();
  103.             current2= first2;
  104.         }
  105.         return first3;
  106.     }
  107.     public static void addValue(Node<Integer> first, int val){
  108.         // The procedure receives a list of numbers and a number and adds that number after each number in the list
  109.         Node<Integer> current;
  110.         current= first;
  111.         while(current!=null && current.getNext()!=null){
  112.             current.getNext().setValue(val);
  113.             current= current.getNext().getNext();
  114.         }
  115.     }
  116.     public static void printList(Node<Integer> first){
  117.         Node<Integer> current;
  118.         current= first;
  119.         while(current!=null){
  120.             current= current.getNext();
  121.         }
  122.         System.out.println(first);
  123.  
  124.     }
  125.     public static Node<Integer> deleteNode(Node<Integer> first, int val) {
  126.         Node<Integer> current= first;
  127.         if (current.getValue() == val) {
  128.             first = first.getNext();
  129.             return first;
  130.         }
  131.         while (current != null) {
  132.             if (current.getNext().getValue() == val){
  133.                 current.setNext(current.getNext().getNext());
  134.                 break;
  135.             }
  136.             current = current.getNext();
  137.         }
  138.         return first;
  139.     }
  140.     public static void deleteValue(Node<Integer> first, int val){
  141.         Node<Integer> current;
  142.         current= first;
  143.         while(current!=null){
  144.             Node<Integer> newVal = new Node<Integer>(val);
  145.             newVal.setNext(current.getNext());
  146.             current.setNext(newVal);
  147.             current= current.getNext().getNext();
  148.         }
  149.     }
  150.     public static Node<Integer> buildList10(){
  151.         Node<Integer> first= new Node<Integer>((int)(Math.random()*10)+1);
  152.         int num;
  153.         Node<Integer> last= first, current;
  154.         for(int i=1; i<10; i++){
  155.             num= (int)(Math.random()*10)+1;
  156.             current= new Node<Integer> (num);
  157.             last.setNext(current);
  158.             last=last.getNext();// You can also write last= current
  159.         }
  160.         return first;
  161.     }
  162.     public static char randomChar() {
  163.         Random r = new Random();
  164.         char random_Char = (char) (r.nextInt(26) + 'a');
  165.         return random_Char;
  166.     }
  167.     public static Node<Character> buildListChar(){
  168.         Node<Character> first= new Node<Character>(sumList.randomChar());
  169.         char letter;
  170.         Node<Character> last= first, current;
  171.         for(int i=1; i<10; i++){
  172.             letter= sumList.randomChar();
  173.             current= new Node<Character> (letter);
  174.             last.setNext(current);
  175.             last=last.getNext();// You can also write last= current
  176.         }
  177.         return first;
  178.     }
  179.     public static Node<Integer> buildSortedList(){
  180.         Node<Integer> node= null;
  181.         for (int i = 0; i < 10; i++){
  182.             node= addToSortedList(node, (int)(Math.random()*30)+1);
  183.         }
  184.         return node;
  185.     }
  186.     public static Node<Integer> addToSortedList(Node<Integer> first, int val){
  187.         Node<Integer> current;
  188.         current= first;
  189.         if(first==null){
  190.             Node<Integer> value= new Node<Integer>(val);
  191.             return value;
  192.         }
  193.         if(val< first.getValue()){
  194.             Node<Integer> value= new Node<Integer>(val);
  195.             value.setNext(current);
  196.             return first;
  197.         }
  198.         while(current.hasNext()){
  199.             if(current.getValue()<val && val<current.getNext().getValue()){
  200.                 Node<Integer> value= new Node<Integer>(val);
  201.                 value.setNext(current.getNext());
  202.                 current.setNext(value);
  203.                 return first;
  204.             }
  205.             current= current.getNext();
  206.         }
  207.  
  208.         Node<Integer> value= new Node<Integer>(val);
  209.         current.setNext(value);
  210.         return first;
  211.  
  212.     }
  213.     public static void main(String[] args) {
  214.         Node<Integer> first1= buildList10();
  215.         Node<Integer> first2= buildList10();
  216.         printList(first1);
  217.         printList(first2);
  218.         Node<Integer> first3= listCombine(first1, first2);
  219.         printList(first3);
  220.     }
  221. }
RAW Paste Data
Top