public class CLinkedList {
private Node head = null; //new Node();
public int listLength() {
int cnt = 0;
Node t = this.head;
do{
cnt++;
t=t.next;
}while(t != this.head);
return cnt;
}
public void insertNode(int dat, int pos){
Node t = this.head;
Node prev = null;
int i = 0;
do{
prev = t;
t = t.next;
i++;
}while(i<pos-1 && t != this.head);
Node p = new Node(dat);
p.next = prev.next;
prev.next = p;
}
public void printList() {
Node t = this.head; //t.next != this.head; t = t.next)
do {
System.out.print(t.data + " ");
t = t.next;
}while(t != this.head);
System.out.println();
}
public void reverseList() {
Node nxt = this.head;
Node prev = null;
Node htemp = this.head;
do{
Node tmp = nxt.next;
nxt.next = prev;
prev = nxt;
nxt = tmp;
}while (nxt != this.head);
htemp.next = prev;
this.head = prev;
}
public void addNode(int dat) {
Node p = new Node(dat);
Node t = this.head;
if (this.head == null) {
this.head = p;
this.head.next = this.head;
} else {
while (t.next != this.head) {
t = t.next;
}
p.next = t.next;
t.next = p;
}
}
public void deleteNode(int dat) {
Node t = this.head;
Node prev = t;
while (t.next != this.head) {
if (t.data == dat) {
prev.next = t.next;
//break;
}
prev = t;
t = t.next;
}
}
public void sortList() {
Node t = this.head;
//for (; t.next != this.head; t = t.next) {
//for (Node r = t.next; r.next != this.head; r = r.next) {
do{
Node r = t;
do{
r = r.next;
if (t.data > r.data) {
int temp = t.data;
t.data = r.data;
r.data = temp;
}
//r = r.next;
}while(r.next != this.head);
t = t.next;
}while(t.next != this.head);
}
public static void main(String args[]) {
CLinkedList ll = new CLinkedList();
ll.addNode(10);
ll.addNode(15);
ll.addNode(18);
ll.addNode(1);
ll.addNode(20);
ll.addNode(3);
ll.addNode(7);
ll.printList();
System.out.println("Length: " + ll.listLength());
ll.reverseList();
ll.printList();
ll.sortList();
ll.printList();
ll.deleteNode(18);
ll.deleteNode(3);
ll.printList();
ll.insertNode(3, 3);
ll.insertNode(21, 5);
ll.insertNode(18, 7);
ll.printList();
ll.sortList();
ll.printList();
System.out.println("Length: " + ll.listLength());
}
}
private Node head = null; //new Node();
public int listLength() {
int cnt = 0;
Node t = this.head;
do{
cnt++;
t=t.next;
}while(t != this.head);
return cnt;
}
public void insertNode(int dat, int pos){
Node t = this.head;
Node prev = null;
int i = 0;
do{
prev = t;
t = t.next;
i++;
}while(i<pos-1 && t != this.head);
Node p = new Node(dat);
p.next = prev.next;
prev.next = p;
}
public void printList() {
Node t = this.head; //t.next != this.head; t = t.next)
do {
System.out.print(t.data + " ");
t = t.next;
}while(t != this.head);
System.out.println();
}
public void reverseList() {
Node nxt = this.head;
Node prev = null;
Node htemp = this.head;
do{
Node tmp = nxt.next;
nxt.next = prev;
prev = nxt;
nxt = tmp;
}while (nxt != this.head);
htemp.next = prev;
this.head = prev;
}
public void addNode(int dat) {
Node p = new Node(dat);
Node t = this.head;
if (this.head == null) {
this.head = p;
this.head.next = this.head;
} else {
while (t.next != this.head) {
t = t.next;
}
p.next = t.next;
t.next = p;
}
}
public void deleteNode(int dat) {
Node t = this.head;
Node prev = t;
while (t.next != this.head) {
if (t.data == dat) {
prev.next = t.next;
//break;
}
prev = t;
t = t.next;
}
}
public void sortList() {
Node t = this.head;
//for (; t.next != this.head; t = t.next) {
//for (Node r = t.next; r.next != this.head; r = r.next) {
do{
Node r = t;
do{
r = r.next;
if (t.data > r.data) {
int temp = t.data;
t.data = r.data;
r.data = temp;
}
//r = r.next;
}while(r.next != this.head);
t = t.next;
}while(t.next != this.head);
}
public static void main(String args[]) {
CLinkedList ll = new CLinkedList();
ll.addNode(10);
ll.addNode(15);
ll.addNode(18);
ll.addNode(1);
ll.addNode(20);
ll.addNode(3);
ll.addNode(7);
ll.printList();
System.out.println("Length: " + ll.listLength());
ll.reverseList();
ll.printList();
ll.sortList();
ll.printList();
ll.deleteNode(18);
ll.deleteNode(3);
ll.printList();
ll.insertNode(3, 3);
ll.insertNode(21, 5);
ll.insertNode(18, 7);
ll.printList();
ll.sortList();
ll.printList();
System.out.println("Length: " + ll.listLength());
}
}