import java.lang.String; public class CharList { private Node head; private int size; public CharList() { head = new Node('\u0000'); size = 0; } public int getSize() { return size; } public void push(char c) //adds node to front of list { Node newNode = new Node(c); newNode.next = head; head = newNode; size = size + 1; } /* Method is broken. Use insertAfter() method instead public void append(char c) //adds node to the end of the list { Node newNode = new Node(c); // if (head == null) // { // head = new Node(c); // return; // } newNode.next = null; Node last = head; while(last.next != null) { last = last.next; } last.next = newNode; size = size + 1; } */ public void insertAfterNode(int position, char c) // position refers to after (int position) number of characters in the list. Also happens to be add to position in an array index { Node temp = head; /* if (position == 0) { System.out.println("The previous node cannot be null. Please use pop method."); return; } */ for (int i = 0; temp != null && i < position - 1; i++) //finds node before position you want to add { temp = temp.next; } Node newNode = new Node(c); newNode.next = temp.next; temp.next = newNode; size = size + 1; } public void deleteAfterNode(int position) // counts the same way as insertAfter. Deletes node after (int position) characters in the list / at [int position] index of an array { Node temp = head; if (head == null) { return; } if (position == 0) { head = temp.next; return; } for (int i = 0; temp != null && i < position - 1; i++) //finds node before position you want to add { temp = temp.next; } Node next = temp.next.next; // point to the node after the node about to be deleted temp.next = next; // unlink the node from the list size = size - 1; } public Node findNode(int position) //counts the same way as delete and insert methods. Position is like index of an array { Node temp = head; // if (head == null) // { // return; // } if (position == 0) { return head; } for (int i = 0; i < position; i++) { temp = temp.next; } return temp; } public void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } } public String listToString() { char tempArray[] = new char [size]; Node temp = head; for (int i = 0; i < size; i++) { tempArray[i] = temp.data; temp = temp.next; } String listString = new String(tempArray); return listString; } public class Node { char data; Node next; Node(char c) { data = c; next = null; } char getData() { return data; } } }