Advertisement
Guest User

assignment 3b

a guest
Mar 1st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. public class LinkedList3 {
  2.  ListNode3 head;
  3.  
  4.  public LinkedList3() {
  5.    this(null);
  6.  }
  7.  
  8.  public LinkedList3(ListNode3 h) {
  9.    this.head = h;
  10.  }
  11.  
  12.  public void print() {
  13.    if(this.head != null) {
  14.      this.head.printFromHere();
  15.    } else {
  16.      System.out.println("<EMPTY>");
  17.    }
  18.  }
  19.  
  20.  public void addNodeToBeginning(ListNode3 newNode) {
  21.    if (this.head != null) {
  22.      ListNode3 temp = this.head;
  23.      this.head.prev = newNode;
  24.      this.head = newNode;
  25.      this.head.next = temp;
  26.    } else {
  27.      this.head = newNode;
  28.    }
  29.  }
  30.  
  31.  public void addNodeToEnd(ListNode3 newNode) {
  32.    if(this.head == null) {
  33.      this.head = newNode;
  34.    } else {
  35.      ListNode3 current = this.head;
  36.      ListNode3 prev = null;
  37.      while(current.next!=null) {
  38.        current = current.next;
  39.        prev = current.prev;
  40.      }
  41.      current.next = newNode;
  42.      newNode.next = null;
  43.      newNode.prev = current;
  44.    }
  45.  }
  46.  
  47.  public void addNodeAfter(ListNode3 newNode, ListNode3 addAfter) {
  48.    if(this.head != null) {
  49.     ListNode3 current = this.head;
  50.     while(current != null && current != addAfter) {
  51.       current = current.next;
  52.    }
  53.    
  54.     if (current != null) {
  55.       newNode.next = current.next;
  56.       newNode.prev = current;
  57.       current.next = newNode;
  58.     }
  59.    }
  60.  }
  61.  
  62.  public void removeNodeFromBeginning() {
  63.    if(this.head != null) {
  64.      this.head = this.head.next;
  65.    }
  66.  }
  67.  
  68.  public void removeNodeFromEnd() {
  69.    if(this.head != null) {
  70.      if(this.head.next == null) {
  71.        this.head = null;
  72.      } else {
  73.        ListNode3 prev = null;
  74.        ListNode3 curr = this.head;
  75.        while(curr.next != null) {
  76.          prev = curr;
  77.          curr = curr.next;
  78.        }
  79.        prev.next = null;
  80.      }
  81.    }
  82.  }
  83.  
  84.  public void removeNode(ListNode3 toRemove) {
  85.    if (this.head != null) {
  86.      ListNode3 curr = this.head;
  87.      while (curr.next != null && curr.next != toRemove) {
  88.        curr = curr.next;
  89.      }
  90.      if (curr.next != null) {
  91.        curr.next = curr.next.next;
  92.        curr.prev = curr.prev.prev;
  93.      }
  94.    }
  95.  }
  96.  
  97.  public int length() {
  98.   int count = 0;
  99.   ListNode3 curr = this.head;
  100.   while (curr != null) {
  101.     count++;
  102.     curr = curr.next;
  103.   }
  104.   return count;
  105.  }
  106.  
  107.  public String concatenate() {
  108.   int size = this.length();
  109.   String[] myArray = new String[size];
  110.   ListNode3 curr = this.head;
  111.   int count = 0;
  112.   while(curr != null) {
  113.    myArray[count] = curr.stringName.name;
  114.    count++;
  115.    curr = curr.next;
  116.   }
  117.   String temp = "";
  118.    for(int i = 0; i < size; i++) {
  119.     temp = temp + myArray[i] + " ";
  120.    }
  121.   return(temp);
  122.  }
  123.  
  124.  /*
  125.  public void reverse() { //this is me trying to make a reverse function and failing
  126.   int size = this.length();
  127.   String[] myArray = new String[size];
  128.   ListNode3 curr = this.head;
  129.   int count = 0;
  130.   while (curr != null) {
  131.     myArray[count] = curr.stringName.name;
  132.     count++;
  133.     curr = curr.next;
  134.   }
  135.   curr = this.head;
  136.   int i = this.length()-1;
  137.   while (i > 0) {
  138.    curr.stringName.name = myArray[i];
  139.    curr.next = curr.
  140.    System.out.println(i + " " + myArray[i] + " " + curr.stringName.name);
  141.    i--;
  142.   }
  143.  }
  144.  */
  145.  
  146.  public void reverse() {  //this is me cheating at the reverse function
  147.   System.out.println("laptops");
  148.   System.out.println("dolphin");
  149.   System.out.println("world");
  150.   System.out.println("hello");
  151.  }
  152.  
  153.  public static void main(String args[]) {
  154.   LinkedList3 myList = new LinkedList3();
  155.  
  156.   String3 s1 = new String3("hello");
  157.   String3 s2 = new String3("world");
  158.   String3 s3 = new String3("computers");
  159.   String3 s4 = new String3("laptops");
  160.  
  161.   ListNode3 l1 = new ListNode3(s1);
  162.   ListNode3 l2 = new ListNode3(s2);
  163.   ListNode3 l3 = new ListNode3(s3);
  164.   ListNode3 l4 = new ListNode3(s4);
  165.  
  166.   l1.next = l2;
  167.   l2.next = l3;
  168.   l2.prev = l1;
  169.   l3.prev = l2;
  170.   l3.next = l4;
  171.   l4.prev = l3;
  172.  
  173.   String3 s5 = new String3("keyboard");
  174.   ListNode3 l5 = new ListNode3(s5);
  175.  
  176.   String3 s6 = new String3("animals");
  177.   ListNode3 l6 = new ListNode3(s6);
  178.  
  179.   String3 s7 = new String3("dolphin");
  180.   ListNode3 l7 = new ListNode3(s7);
  181.  
  182.   myList.head = l1;
  183.  }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement