Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. public class List {
  2.  
  3.     Node head;
  4.  
  5.     public List() {
  6.         head = null;
  7.     }
  8.  
  9.     public boolean isEmpty() {
  10.         return head == null;
  11.     }
  12.  
  13.     public void insert(int data) {
  14.         Node node = new Node(data, head);
  15.         head = node;
  16.     }
  17.  
  18.     public void modify() {
  19.         Node current = head;
  20.         while(current.next != null) {
  21.             int min = Math.min(current.getData(), current.next.getData());
  22.             int max = Math.max(current.getData(), current.next.getData());
  23.             int newData = Math.abs(max - min);
  24.             Node newNode = new Node(newData, null);
  25.             newNode.next = current.next;
  26.             current.next = newNode;
  27.             current = newNode.next;
  28.             System.out.println(this);
  29.         }
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         String output = "{";
  35.         Node current = head;
  36.         while(current != null) {
  37.             output += " " + current.getData();
  38.             output += (current.next == null) ? "" : ", ";
  39.             current = current.next;
  40.         }
  41.         output += "}\n";
  42.         return output;
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         List list = new List();
  47.         list.insert(8);
  48.         list.insert(3);
  49.         list.insert(5);
  50.         list.insert(2);
  51.         list.insert(4);
  52.         System.out.println(list);
  53.         list.modify();
  54.         System.out.println(list);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement