Advertisement
Unit2

Untitled

Mar 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.71 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.util.Scanner;
  5. import java.util.Random;
  6.  
  7.  
  8. public class Hw02 {
  9.    
  10.     public static void complexityIndicator()
  11.     {
  12.         System.err.println("ma483382;1.75;6");
  13.     }
  14.    
  15.     public static void main(String[] args)
  16.     {
  17.         File file = new File(args[0]);
  18.         long seed;
  19.         Random random;
  20.        
  21.         // Scans through the input file and performs functions
  22.         try
  23.         {
  24.            
  25.             List list = new List();
  26.            
  27.             BufferedReader br = new BufferedReader(new FileReader(file));
  28.                      
  29.             Scanner scanner = new Scanner(file);
  30.            
  31.             if(args[0].equals(new String("R")))
  32.             {
  33.                 seed = System.currentTimeMillis();
  34.                 System.out.println("For the input file named " + args[0]);
  35.                 System.out.println("With the RNG seeded" + seed + ",");
  36.             }
  37.             else
  38.             {
  39.                 seed = 42;
  40.                 System.out.println("For the input file named " + args[0]);
  41.                 System.out.println("With the RNG unseeded,");
  42.             }
  43.            
  44.             random = new Random(seed);
  45.                
  46.             // Loops through file until EOF
  47.             while(scanner.hasNext())
  48.             {              
  49.                 // Scans the line
  50.                 String data = scanner.nextLine();
  51.                
  52.                 // Splits the function and the number into two Strings
  53.                 String[] line = data.split(" ");
  54.                
  55.                 // Insert number call
  56.                 if(line[0].equals("i"))
  57.                 {                  
  58.                     int num = Integer.parseInt(line[1]);
  59.                    
  60.                     if(list.search(num) != true)
  61.                     {
  62.                         list.insert(num);
  63.                        
  64.                         while(random.nextInt() % 2 == 1)
  65.                         {
  66.                             list.promote(num);
  67.                         }
  68.                     }
  69.  
  70.                    
  71.                     // IF(promote success)
  72.                     // insert
  73.                    
  74.                     list.sort();
  75.                 }
  76.                
  77.                 // Delete number call
  78.                 if(line[0].equals("d"))
  79.                 {
  80.                     int delNum = Integer.parseInt(line[1]);
  81.                    
  82.                     if(list.search(delNum) == true)
  83.                     {
  84.                         list.delete(delNum);
  85.                         System.out.println(delNum + " deleted");
  86.                     }
  87.                    
  88.                     else
  89.                     {
  90.                         System.out.println(delNum + " integer not found - delete not successful");
  91.                     }
  92.  
  93.                 }
  94.                
  95.                 // Quit function and calls the Tree's info
  96.                 if(line[0].equals("q"))
  97.                 {                                        
  98.                     // Quit Function
  99.                 }
  100.                
  101.                 // Print function call
  102.                 if(line[0].contentEquals("p"))
  103.                 {
  104.                     list.print();
  105.                 }
  106.                
  107.                 // Search function call
  108.                 if(line[0].contentEquals("s"))
  109.                 {
  110.                     int searchNum = Integer.parseInt(line[1]);
  111.                    
  112.                     if(list.search(searchNum) == true)
  113.                     {
  114.                         System.out.println(searchNum + " found");
  115.                     }
  116.                    
  117.                     else
  118.                     {
  119.                         System.out.println(searchNum + " NOT FOUND");
  120.                     }
  121.                 }
  122.  
  123.             }
  124.            
  125.             // Closes text file
  126.             br.close();
  127.            
  128.             // Closes the scanner
  129.             scanner.close();
  130.         }
  131.         catch (Exception e)
  132.         {
  133.             e.printStackTrace();
  134.         }
  135.        
  136.         // Prints UCF info to STD.err
  137. //        complexityIndicator();
  138.     }
  139. }
  140.  
  141. class List
  142. {
  143.     Node head;
  144.     Node tail;
  145.    
  146.     public List()
  147.     {
  148.         head = null;
  149.         tail = null;
  150.     }
  151.    
  152.     class Node
  153.     {
  154.         int data;
  155.         int level = 1;
  156.         Node next, back;
  157.        
  158.         public Node(int d)
  159.         {
  160.             this.data = d;
  161.         }
  162.     }
  163.    
  164.     public void promote(int key)
  165.     {
  166.         Node temp = head;
  167.  
  168.         while(temp.data != key)
  169.         {
  170.             temp = temp.next;
  171.         }
  172.        
  173.         temp.level += 1;
  174.     }
  175.    
  176.     public void insert(int data)
  177.     {
  178.         Node node = new Node(data);
  179.        
  180.         if(head == null)
  181.         {
  182.             head = tail = node;
  183.            
  184.             head.back = null;
  185.            
  186.             tail.next= null;
  187.         }
  188.        
  189.         else
  190.         {
  191.             tail.next = node;
  192.            
  193.             node.back = tail;
  194.            
  195.             tail = node;
  196.            
  197.             tail.next = null;
  198.         }
  199.     }
  200.    
  201.     public void sort()
  202.     {
  203.         Node current = null;
  204.         Node index = null;
  205.        
  206.         int temp;
  207.         int temp2;
  208.        
  209.         if(head == null)
  210.         {
  211.             return;
  212.         }
  213.        
  214.         else
  215.         {
  216.             for(current = head; current.next != null; current = current.next)
  217.             {
  218.                 for(index = current.next; index != null; index = index.next)
  219.                 {
  220.                     if(current.data > index.data)
  221.                     {
  222.                         temp = current.data;
  223.                         temp2 = current.level;
  224.                         current.data = index.data;
  225.                         current.level = index.level;
  226.                         index.data = temp;
  227.                         index.level = temp2;
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.     }
  233.    
  234.     public void delete(int key)
  235.     {
  236.         deleteNode(head, key);
  237.     }
  238.    
  239.     public Node deleteNode(Node head, int key)
  240.     {
  241.        
  242.         Node temp = head;
  243.         Node temp2 = null;
  244.         Node temp3 = null;
  245.        
  246.         while(temp.data != key)
  247.         {
  248.             temp = temp.next;
  249.         }
  250.        
  251.         if(temp.back == null)
  252.         {
  253.             temp3 = temp.next;
  254.             temp3.back = temp2;
  255.         }
  256.        
  257.         else if(temp.next == null)
  258.         {
  259.             temp2 = temp.back;
  260.             temp2.next = temp3;
  261.         }
  262.        
  263.         else
  264.         {
  265.             temp2 = temp.back;
  266.             temp3 = temp.next;
  267.             temp2.next = temp3;
  268.             temp3.back = temp2;
  269.         }
  270.  
  271.         return head;
  272.     }
  273.    
  274.     public void print()
  275.     {
  276.         Node current = head;
  277.        
  278.         if(head == null)
  279.         {
  280.             System.out.println("Empty list");
  281.             return;
  282.         }
  283.        
  284.         System.out.println("the current Skip List is shown below: ");
  285.         System.out.println("---infinity");
  286.        
  287.         while(current != null)
  288.         {
  289.             for(int i = 0; i < current.level; i++)
  290.             {
  291.                 System.out.print(" " + current.data + "; ");
  292.             }
  293.            
  294.             System.out.println();
  295.             current = current.next;
  296.         }
  297.        
  298.         System.out.println("+++infinity");
  299.         System.out.println("---End of Skip List---");
  300.     }
  301.    
  302.     public boolean search(int key)
  303.     {
  304.         return searchKey(head, key);
  305.     }
  306.    
  307.     public boolean searchKey(Node head, int key)
  308.     {
  309.         Node here = head;
  310.        
  311.         while(here != null)
  312.         {
  313.             if(here.data == key)
  314.             {
  315.                 return true;
  316.             }
  317.            
  318.             here = here.next;
  319.         }
  320.        
  321.         return false;
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement