Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a method rotate() to CircularLinkedList class that shift the list one node, below is the output before and after calling the method. Node 1:

  1. Add a method rotate() to CircularLinkedList class that shift the list one node, below is the output before and after calling the method.

Node 1: 5

Node 2: 4

Node 3: 3

Node 4: 2

Node 5: 1

Node 1: 4

Node 2: 3

Node 3: 2

Node 4: 1

Node 5: 5

------------------------------------------

public class SinglLinkedList { Node head=null; Node tail=null; int size=0; class Node { Node next; E Element; Node(E value,Node next) { this.Element = value; this.next = next; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public E getElement() { return Element; } } public E getHead() { return head.getElement(); }

public E getTail() { return tail.getElement(); } public int getSize() { return size; }

boolean isEmpty() { if (size == 0) { return true; } else { return false; } } void Display() { if(isEmpty()) { System.out.println("Empty List.. "); return; } System.out.println(" Display List: "); Node current = head; int count=1; while (current != null) { //System.out.print("\tNode "+count+" :"+current.getElement()); System.out.print("\t\tNode "+count+" :"+current.getElement().toString()); current = current.getNext(); count++; } } void addFirst(E Element) { Node newNode = new Node(Element, head); head = newNode; if (size == 0) { tail = head; } size++; } void addLast(E Element) { Node newNode = new Node(Element, null); tail.next = newNode; tail = newNode; if (size == 0) { head = tail; } size++; }

public void addAtPos(E e, int position) { //check if the position is correct (from 1 to size) if (position < 1 || position > size) { System.out.println("Invalid Position"); return; } //Check if the position 1 (first node) if (position == 1) { addFirst(e); return; } //any other location Node newNode = new Node(e, null); Node current = head; int count = 1; while (count < position - 1) { current = current.getNext(); count++; } //found.. // Put this node before the current newNode.setNext(current.getNext()); current.setNext(newNode); size++; } int FindNode(E key) { Node current = head; int counter = 1; while (current != null) { if (current.getElement()== key) } } System.out.println("Found in position# " + counter); return counter; } counter++; current = current.getNext(); } return -1; } void removeFirst() { if (size == 0) { System.out.println("Empty list"); return; } head = head.getNext(); size--; //if this was the last and only node.. fix tail if (size == 0) { tail = null; } } void RemoveNode(E key) { Node current = head; Node prev = head; while (current.getElement()!= key) { if (current.next == null) { System.out.println("Not found"); return; } prev = current; current = current.next; } if (current == head) { removeFirst(); } else { prev.next = current.next; size--; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions