Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class List {
- Node head;
- public List() {
- head = null;
- }
- public boolean isEmpty() {
- return head == null;
- }
- public void insert(int data) {
- Node node = new Node(data, head);
- head = node;
- }
- public void modify() {
- Node current = head;
- while(current.next != null) {
- int min = Math.min(current.getData(), current.next.getData());
- int max = Math.max(current.getData(), current.next.getData());
- int newData = Math.abs(max - min);
- Node newNode = new Node(newData, null);
- newNode.next = current.next;
- current.next = newNode;
- current = newNode.next;
- System.out.println(this);
- }
- }
- @Override
- public String toString() {
- String output = "{";
- Node current = head;
- while(current != null) {
- output += " " + current.getData();
- output += (current.next == null) ? "" : ", ";
- current = current.next;
- }
- output += "}\n";
- return output;
- }
- public static void main(String[] args) {
- List list = new List();
- list.insert(8);
- list.insert(3);
- list.insert(5);
- list.insert(2);
- list.insert(4);
- System.out.println(list);
- list.modify();
- System.out.println(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement