Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LinkedList3 {
- ListNode3 head;
- public LinkedList3() {
- this(null);
- }
- public LinkedList3(ListNode3 h) {
- this.head = h;
- }
- public void print() {
- if(this.head != null) {
- this.head.printFromHere();
- } else {
- System.out.println("<EMPTY>");
- }
- }
- public void addNodeToBeginning(ListNode3 newNode) {
- if (this.head != null) {
- ListNode3 temp = this.head;
- this.head.prev = newNode;
- this.head = newNode;
- this.head.next = temp;
- } else {
- this.head = newNode;
- }
- }
- public void addNodeToEnd(ListNode3 newNode) {
- if(this.head == null) {
- this.head = newNode;
- } else {
- ListNode3 current = this.head;
- ListNode3 prev = null;
- while(current.next!=null) {
- current = current.next;
- prev = current.prev;
- }
- current.next = newNode;
- newNode.next = null;
- newNode.prev = current;
- }
- }
- public void addNodeAfter(ListNode3 newNode, ListNode3 addAfter) {
- if(this.head != null) {
- ListNode3 current = this.head;
- while(current != null && current != addAfter) {
- current = current.next;
- }
- if (current != null) {
- newNode.next = current.next;
- newNode.prev = current;
- current.next = newNode;
- }
- }
- }
- public void removeNodeFromBeginning() {
- if(this.head != null) {
- this.head = this.head.next;
- }
- }
- public void removeNodeFromEnd() {
- if(this.head != null) {
- if(this.head.next == null) {
- this.head = null;
- } else {
- ListNode3 prev = null;
- ListNode3 curr = this.head;
- while(curr.next != null) {
- prev = curr;
- curr = curr.next;
- }
- prev.next = null;
- }
- }
- }
- public void removeNode(ListNode3 toRemove) {
- if (this.head != null) {
- ListNode3 curr = this.head;
- while (curr.next != null && curr.next != toRemove) {
- curr = curr.next;
- }
- if (curr.next != null) {
- curr.next = curr.next.next;
- curr.prev = curr.prev.prev;
- }
- }
- }
- public int length() {
- int count = 0;
- ListNode3 curr = this.head;
- while (curr != null) {
- count++;
- curr = curr.next;
- }
- return count;
- }
- public String concatenate() {
- int size = this.length();
- String[] myArray = new String[size];
- ListNode3 curr = this.head;
- int count = 0;
- while(curr != null) {
- myArray[count] = curr.stringName.name;
- count++;
- curr = curr.next;
- }
- String temp = "";
- for(int i = 0; i < size; i++) {
- temp = temp + myArray[i] + " ";
- }
- return(temp);
- }
- /*
- public void reverse() { //this is me trying to make a reverse function and failing
- int size = this.length();
- String[] myArray = new String[size];
- ListNode3 curr = this.head;
- int count = 0;
- while (curr != null) {
- myArray[count] = curr.stringName.name;
- count++;
- curr = curr.next;
- }
- curr = this.head;
- int i = this.length()-1;
- while (i > 0) {
- curr.stringName.name = myArray[i];
- curr.next = curr.
- System.out.println(i + " " + myArray[i] + " " + curr.stringName.name);
- i--;
- }
- }
- */
- public void reverse() { //this is me cheating at the reverse function
- System.out.println("laptops");
- System.out.println("dolphin");
- System.out.println("world");
- System.out.println("hello");
- }
- public static void main(String args[]) {
- LinkedList3 myList = new LinkedList3();
- String3 s1 = new String3("hello");
- String3 s2 = new String3("world");
- String3 s3 = new String3("computers");
- String3 s4 = new String3("laptops");
- ListNode3 l1 = new ListNode3(s1);
- ListNode3 l2 = new ListNode3(s2);
- ListNode3 l3 = new ListNode3(s3);
- ListNode3 l4 = new ListNode3(s4);
- l1.next = l2;
- l2.next = l3;
- l2.prev = l1;
- l3.prev = l2;
- l3.next = l4;
- l4.prev = l3;
- String3 s5 = new String3("keyboard");
- ListNode3 l5 = new ListNode3(s5);
- String3 s6 = new String3("animals");
- ListNode3 l6 = new ListNode3(s6);
- String3 s7 = new String3("dolphin");
- ListNode3 l7 = new ListNode3(s7);
- myList.head = l1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement