Wawan

Friday, July 20, 2012

Contoh Program Single Linked List Circular


// Program for singly linked Circular list


#include <iostream>
#include <string>


struct SLinkCircularList
{
    std::string data;
    SLinkCircularList *next;


    SLinkCircularList() : data(""), next(this) { }
    SLinkCircularList(const char *value) : data(value), next(this) { }


    SLinkCircularList* InsertNext(const char *data)
    {
        SLinkCircularList *node = new SLinkCircularList(data);
        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;
    }


    bool DeleteNext()
    {
        if(this->next == this)
        {  
            std::cout << "\nThe node can not be deleted as there is only one node in the circular list\n";
            return false;
        }
        SLinkCircularList *pNode = this->next;
        this->next = this->next->next; 
        delete pNode;
    }


    void Traverse(SLinkCircularList *node = NULL)
    {
        if(node == NULL)
            node = this;
        std::cout << "\n\nTraversing in Forward Direction\n\n";


        SLinkCircularList *startnode = node;
        do
        {
            std::cout << node->data;
            std::cout << "\n";
            node = node->next;
        }
        while(node != startnode);
    }


    int GetNumberOfNodes(SLinkCircularList *node = NULL)
    {
        if(node == NULL)
            node = this;
        int count = 0;
        SLinkCircularList *startnode = node;
        do
        {
            count++;
            node = node->next;
        }
        while(node != startnode);


        std::cout << "\nCurrent Node Value: " << node->data.c_str();
        std::cout << "\nTotal nodes in Circular List: " << count;
        std::cout << "\n";
        return count;
    }
   
};


int main()
{
    SLinkCircularList *node1 = new SLinkCircularList("ONE");
    node1->DeleteNext(); // Delete will fail in this case.
    SLinkCircularList *node2 = node1->InsertNext("TWO");
    node1->DeleteNext(); // It will delete the node2.
    node2 = node1->InsertNext("TWO"); // Insert it again
    SLinkCircularList *node3 = node2->InsertNext("THREE");
    SLinkCircularList *node4 = node3->InsertNext("FOUR");
    SLinkCircularList *node5 = node4->InsertNext("FIVE");


    node1->GetNumberOfNodes();
    node3->GetNumberOfNodes();
    node5->GetNumberOfNodes();


    node1->Traverse();
    node3->DeleteNext(); // delete the node "FOUR"
    node2->Traverse();
    std::cout << "\n";  


    node1->GetNumberOfNodes();
    node3->GetNumberOfNodes();
    node5->GetNumberOfNodes();


    std::cout << "\n";
   
    return 0;
}

No comments:

Post a Comment