Circular Link List in Java
Circular Link List in Java
Circular Link List in Java
A linked list in which the last node points to the first node is called a circular linked list. Circular linked lists avoid the use of null references in their nodes. Can be useful for certain algorithms like playing video/audio files repeatedly and ALT + TAB in Windows. Successor of tail node is the head node. In doubly-linked list, predecessor of head node is the tail node. Insertions and deletions into a circular linked list follow the same logic patterns used in a singly linked list except that the last node must always point to the first node. Therefore, when inserting or deleting the last node, in addition to updating the tail pointer, we must also point the link field of the new last node to the first node.
Advantage is that we can start searching from any node of the linked list and get to any other node. No logical head and tail pointers. However, we follow the conventions i.e., first element inserted into the list is given status of head and last element entered is called as a tail.
Some characteristics of Circular Linked list are that the Last node references the first node, every node has a successor, and no node in a circular linked list contains NULL. Defining a Circular Linked List:
#include <iostream> using namespace std; struct Node{ int data; Node* next; };
Node* NodePtr;
Algorithm First let the user input two long integers Store the two long integers in the lists Both lists are traversed in parallel, and five digits are added at a time If the sum of two 5 digits number is x, the lower order 5 digits of x can be extracted by using
x % 100000
o x / 100000 largeInt* addint (largeint *int1, largeint *int2) { Node *p, q; int hunthou = 100000; int carry, number, total; largeint * sum; //defined to contain sum of p and q sum = new largeint; p = int1->Tail->Next; //p and q point to heads of list q = int2->Tail->Next; carry = 0; do { //add info of two nodes and previous carry total = p->data + q->data + carry; //Determine lower order five digits number = total % hunthou; insertRear(sum, number); //determine the carry carry = total / hunthou; p = p ->next; q = q->next } while ( p != int1->Tail->Next && q != int2->Tail->Next) while (p != int1->Tail->Next) { total = p->data + carry; number = total % hunthou; insertRear(sum, number); carry = total / hunthou; p = p->next; } while (q != int2->Tail->Next) { total = q->data + carry; number = total % hunthou; insertRear(sum, number); carry = total / hunthou; q = q->next; } if (carry == 1) { insertRear (sum, carry); } return sum; }