Software & Finance





Turbo C++ - Singly Linked Circular List





I have given here Turbo C++ sample code for singly linked circular list, filling elements and traversal in forward direction and counting the number of elements in the list. 

 

Singly Linked List, Doubly Linked List, Singly Linked Circular List and Doubly Linked Circular List are an important data structure used in C++ applications.


The main difference between singly linked list and singly linked circular list are the next node pointer of last node is NULL in case of singly linked list where as next node pointer of last node is first node in case of singly linked circular list.

If there is only one node in singly linked circular list, then this condition will be true (this == this->next)

Related Links:



Source Code


#include <iostream.h>

 

 

struct SLinkCircularList

{

       int data;

       SLinkCircularList *next;

 

       SLinkCircularList();

       SLinkCircularList(int value);

 

       SLinkCircularList* InsertNext(int data);

       int DeleteNext();

       void Traverse(SLinkCircularList *node = NULL);

       int GetNumberOfNodes(SLinkCircularList *node = NULL);

};

 

 

SLinkCircularList::SLinkCircularList() : data(0), next(this) { }

 

SLinkCircularList::SLinkCircularList(int value) : data(value), next(this) { }

 

SLinkCircularList* SLinkCircularList::InsertNext(int value)

{

       SLinkCircularList *node = new SLinkCircularList(value);

       if(this->next == this) // only one node in the circular list

       {

              // Easy to handle, after the two lines of executions,

              // there will be two nodes in the circular list

 

              node->next = this;

              this->next = node;

       }

       else

       {

              // Insert in the middle

 

              SLinkCircularList *temp = this->next;

              node->next = temp;

              this->next = node;

       }

       return node;

 

}

 

int SLinkCircularList::DeleteNext()

{

       if(this->next == this)

       {

              cout << "\nThe node can not be deleted as there is only one node in the circular list\n";

              return 0;

       }

 

       SLinkCircularList *pNode = this->next;

       this->next = this->next->next;

       delete pNode;

       return 1;

}

 

 

void SLinkCircularList::Traverse(SLinkCircularList *node)

{

       if(node == NULL)

              node = this;

       cout << "\n\nTraversing in Forward Direction\n\n";

       SLinkCircularList *startnode = node;

 

       do

       {

              cout << node->data;

              cout << "\n";

              node = node->next;

       }

       while(node != startnode);

}

 

int SLinkCircularList::GetNumberOfNodes(SLinkCircularList *node)

{

       if(node == NULL)

              node = this;

 

       int count = 0;

       SLinkCircularList *startnode = node;

       do

       {

              count++;

              node = node->next;

       }

       while(node != startnode);

 

       cout << "\nCurrent Node Value: " << node->data;

       cout << "\nTotal nodes in Circular List: " << count;

 

       cout << "\n";

 

       return count;

}

 

 

 

int main()

{

       SLinkCircularList *node1 = new SLinkCircularList(1);

       node1->DeleteNext(); // Delete will fail in this case.

 

       SLinkCircularList *node2 = node1->InsertNext(2);

       node1->DeleteNext(); // It will delete the node2.

 

       node2 = node1->InsertNext(2); // Insert it again

 

       SLinkCircularList *node3 = node2->InsertNext(3);

       SLinkCircularList *node4 = node3->InsertNext(4);

       SLinkCircularList *node5 = node4->InsertNext(5);

 

       node1->GetNumberOfNodes();

       node3->GetNumberOfNodes();

       node5->GetNumberOfNodes();

 

       node1->Traverse();

       node3->DeleteNext(); // delete the node "FOUR"

       node2->Traverse();

       cout << "\n";

 

       node1->GetNumberOfNodes();

       node3->GetNumberOfNodes();

       node5->GetNumberOfNodes();

 

       cout << "\n";

       return 0;

}

 

Output


The node can not be deleted as there is only one node in the circular list

 

Current Node Value: 1

Total nodes in Circular List: 5

 

Current Node Value: 3

Total nodes in Circular List: 5

 

Current Node Value: 5

Total nodes in Circular List: 5

 

 

Traversing in Forward Direction

 

1

2

3

4

5

 

 

Traversing in Forward Direction

 

2

3

5

1

 

 

Current Node Value: 1

Total nodes in Circular List: 4

 

Current Node Value: 3

Total nodes in Circular List: 4

 

Current Node Value: 5

Total nodes in Circular List: 4