Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class sumList {
- public static int sumOfList(Node<Integer> first){
- // The procedure receives a list of numbers and returns their sum
- int sum= 0;
- Node<Integer> current;
- current= first;
- while(current!=null){
- sum+= current.getValue();
- current= current.getNext();
- }
- return sum;
- }
- public static int findMin(Node<Integer> first){
- // The procedure receives a list of numbers and returns the smallest number
- Node<Integer> current= first;
- int min= current.getValue();
- while(current!=null){
- if(current.getValue()<min){
- min= current.getValue();
- }
- current= current.getNext();
- }
- return min;
- }
- public static int findMaxPos(Node<Integer> first){
- // The procedure receives a list of numbers and returns the position of the largest number
- Node<Integer> current= first;
- int position= 0;
- int times= 0;
- int max= current.getValue();
- while(current!=null){
- if(current.getValue()>max){
- max= current.getValue();
- position= times;
- }
- current= current.getNext();
- times+=1;
- }
- return position;
- }
- public static Node<Integer> addLast(Node<Integer> first, int val){
- // The procedure receives a list of numbers and a number and adds that number to the end of the list
- Node<Integer> current;
- current= first;
- if(first==null){
- return new Node<Integer>(val);
- }
- while(current.hasNext()){
- current= current.getNext();
- }
- current.setNext(new Node<Integer>(val));
- return first;
- }
- public static void printSize(Node<Integer> first){
- // The procedure receives a list of numbers and prints the number of numbers in the list
- Node<Integer> current;
- int times= 0;
- current= first;
- while(current!=null){
- times+= 1;
- current= current.getNext();
- }
- System.out.println(times);
- }
- public static void equalsValue(Node<Integer> first, int val){
- // The procedure receives a list of numbers and prints the number of numbers in the list
- Node<Integer> current;
- int times= 0;
- current= first;
- while(current!=null){
- if(current.getValue()==val){
- times+= 1;
- }
- current= current.getNext();
- }
- System.out.println(times);
- }
- public static boolean isInList(Node<Integer> first, int val){
- // The procedure receives a list of numbers and prints the number of numbers in the list
- while(first!=null){
- if(first.getValue()==val){
- return true;
- }
- first= first.getNext();
- }
- return false;
- }
- public static Node<Integer> listCombine(Node<Integer> first1, Node<Integer> first2){
- Node<Integer> first3= null, current1, current2, current3;
- int num;
- current1= first1;current2= first2;current3=first3;
- while(current1!=null){
- num= current1.getValue();
- while(current2!=null){
- if(num== current2.getValue() && !isInList(first3, num))
- first3= addLast(first3, num);
- current2= current2.getNext();
- }
- current1= current1.getNext();
- current2= first2;
- }
- return first3;
- }
- public static void addValue(Node<Integer> first, int val){
- // The procedure receives a list of numbers and a number and adds that number after each number in the list
- Node<Integer> current;
- current= first;
- while(current!=null && current.getNext()!=null){
- current.getNext().setValue(val);
- current= current.getNext().getNext();
- }
- }
- public static void printList(Node<Integer> first){
- Node<Integer> current;
- current= first;
- while(current!=null){
- current= current.getNext();
- }
- System.out.println(first);
- }
- public static Node<Integer> deleteNode(Node<Integer> first, int val) {
- Node<Integer> current= first;
- if (current.getValue() == val) {
- first = first.getNext();
- return first;
- }
- while (current != null) {
- if (current.getNext().getValue() == val){
- current.setNext(current.getNext().getNext());
- break;
- }
- current = current.getNext();
- }
- return first;
- }
- public static void deleteValue(Node<Integer> first, int val){
- Node<Integer> current;
- current= first;
- while(current!=null){
- Node<Integer> newVal = new Node<Integer>(val);
- newVal.setNext(current.getNext());
- current.setNext(newVal);
- current= current.getNext().getNext();
- }
- }
- public static Node<Integer> buildList10(){
- Node<Integer> first= new Node<Integer>((int)(Math.random()*10)+1);
- int num;
- Node<Integer> last= first, current;
- for(int i=1; i<10; i++){
- num= (int)(Math.random()*10)+1;
- current= new Node<Integer> (num);
- last.setNext(current);
- last=last.getNext();// You can also write last= current
- }
- return first;
- }
- public static char randomChar() {
- Random r = new Random();
- char random_Char = (char) (r.nextInt(26) + 'a');
- return random_Char;
- }
- public static Node<Character> buildListChar(){
- Node<Character> first= new Node<Character>(sumList.randomChar());
- char letter;
- Node<Character> last= first, current;
- for(int i=1; i<10; i++){
- letter= sumList.randomChar();
- current= new Node<Character> (letter);
- last.setNext(current);
- last=last.getNext();// You can also write last= current
- }
- return first;
- }
- public static Node<Integer> buildSortedList(){
- Node<Integer> node= null;
- for (int i = 0; i < 10; i++){
- node= addToSortedList(node, (int)(Math.random()*30)+1);
- }
- return node;
- }
- public static Node<Integer> addToSortedList(Node<Integer> first, int val){
- Node<Integer> current;
- current= first;
- if(first==null){
- Node<Integer> value= new Node<Integer>(val);
- return value;
- }
- if(val< first.getValue()){
- Node<Integer> value= new Node<Integer>(val);
- value.setNext(current);
- return first;
- }
- while(current.hasNext()){
- if(current.getValue()<val && val<current.getNext().getValue()){
- Node<Integer> value= new Node<Integer>(val);
- value.setNext(current.getNext());
- current.setNext(value);
- return first;
- }
- current= current.getNext();
- }
- Node<Integer> value= new Node<Integer>(val);
- current.setNext(value);
- return first;
- }
- public static void main(String[] args) {
- Node<Integer> first1= buildList10();
- Node<Integer> first2= buildList10();
- printList(first1);
- printList(first2);
- Node<Integer> first3= listCombine(first1, first2);
- printList(first3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement