Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CircularLinkedList {
- private ListNode last;
- public CircularLinkedList() {
- last = null;
- }
- public boolean isEmpty() {
- return last == null;
- }
- public String toString() {
- String val = "";
- if (isEmpty()) {
- return "Error: Empty List";
- }
- ListNode curr = last.getNext();
- while (curr != last) {
- val += curr.getValue();
- curr = curr.getNext();
- }
- val += last.getValue();
- return val;
- }
- public int size() {
- int val = 0;
- if (isEmpty()) {
- return 0;
- }
- ListNode curr = last.getNext();
- while (curr != last) {
- val++;
- curr = curr.getNext();
- }
- return val+1;
- }
- public void addFirst(Object arg) {
- if (isEmpty()) {
- ListNode test = new ListNode(arg, last);
- test.setNext(test);
- }
- ListNode next = last.getNext();
- last.setNext(new ListNode(arg, next));
- }
- public Object removeFirst(Object arg) {
- if (isEmpty()) {
- return null;
- }
- ListNode first = last.getNext();
- if (last == first) {
- return first.getValue();
- }
- last.setNext(first.getNext());
- return first.getValue();
- }
- public void addLast(Object arg) {
- if (isEmpty()) {
- ListNode test = new ListNode(arg, last);
- test.setNext(test);
- }
- ListNode first = last.getNext();
- ListNode curr = first;
- while (curr != last) {
- curr = curr.getNext();
- }
- curr.setNext(new ListNode(arg, first));
- }
- public Object removeLast(Object arg) {
- if (isEmpty()) {
- return null;
- }
- ListNode first = last.getNext();
- if (last == first) {
- return first.getValue();
- }
- ListNode curr = first;
- while (curr.getNext() != last) {
- curr = curr.getNext();
- }
- curr.setNext(new ListNode(arg, first));
- return last;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement