Breaking

Be Strong Let's Try!

Tuesday, 21 September 2021

Circular Linked List Implementation | Data Structure

 

Objective:

To implement Circular Linked List ADT functions.

Description:

In single linked list, every node points to its next node in the sequence and the last node points NULL. However, a circular linked list is a sequence of elements in which every element has a link to its next element in the sequence and the last element has a link to the first element.

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list

Circular Linked List:

Circular Linked List Implementation | Data Structure

In a circular linked list, we perform the following operations...

1.      Insertion

2.      Deletion

3.      Display

Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of the circular linked list...

1.       Create a newNode with given value.

2.       Check whether list is Empty (head == NULL)

3.       If it is Empty then, set head = newNode and newNode→next = head .

4.       If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.

5.       Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp → next == head').

6.       Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.

Inserting At End of the list

We can use the following steps to insert a new node at end of the circular linked list...

1.       Create a newNode with given value.

1.       Check whether list is Empty (head == NULL).

2.       If it is Empty then, set head = newNode and newNode → next = head.

3.       If it is Not Empty then, define a node pointer temp and initialize with head.

4.       Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next == head).

5.       Set temp → next = newNode and newNode → next = head.

Inserting At Specific location in the list (After a Node)

We can use the following steps to insert a new node after a node in the circular linked list...

1.     Create a newNode with given value.

2.     Check whether list is Empty (head == NULL)

3.     If it is Empty then, set head = newNode and newNode → next = head.

4.     If it is Not Empty then, define a node pointer temp and initialize with head.

5.     Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode).

6.     Every time check whether temp is reached to the last node or not. If it is reached to last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp to next node.

7.     If temp is reached to the exact node after which we want to insert the newNode then check whether it is last node (temp → next == head).

8.     If temp is last node, then set temp → next = newNode and newNode → next = head.

9.     If temp is not last node, then set newNode → next = temp → next and temp → next = newNode.

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning of the circular linked list...

1.       Check whether list is Empty (head == NULL)

2.       If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.

3.       If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize both 'temp1' and 'temp2' with head.

4.       Check whether list is having only one node (temp1 → next == head)

5.       If it is TRUE then set head = NULL and delete temp1 (Setting Empty list conditions)

6.       If it is FALSE move the temp1 until it reaches to the last node. (until temp1 → next == head )

7.       Then set head = temp2 → nexttemp1 → next = head and delete temp2.

Deleting from End of the list

We can use the following steps to delete a node from end of the circular linked list...

1.       Check whether list is Empty (head == NULL)

2.       If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.

1.       If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.

2.       Check whether list has only one Node (temp1 → next == head)

3.       If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function. (Setting Empty list condition)

4.       If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same until temp1 reaches to the last node in the list. (until temp1 → next == head)

5.       Set temp2 → next head and delete temp1.

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from the circular linked list...

1.       Check whether list is Empty (head == NULL)

2.       If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.

3.       If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.

4.       Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.

5.       If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'. And terminate the function.

6.       If it is reached to the exact node which we want to delete, then check whether list is having only one node (temp1 → next == head)

7.       If list has only one node and that is the node to be deleted then set head = NULL and delete temp1 (free(temp1)).

8.       If list contains multiple nodes, then check whether temp1 is the first node in the list (temp1 == head).

9.       If temp1 is the first node then set temp2 = head and keep moving temp2 to its next node until temp2 reaches to the last node. Then set head = head → nexttemp2 → next = head and delete temp1.

10.    If temp1 is not first node then check whether it is last node in the list (temp1 → next == head).

11.    If temp1 is last node then set temp2 → next = head and delete temp1 (free(temp1)).

12.    If temp1 is not first node and not last node then set temp2 → next = temp1 → next and delete temp1 (free(temp1)).

Displaying a circular Linked List

We can use the following steps to display the elements of a circular linked list...

1.       Check whether list is Empty (head == NULL)

2.       If it is Empty, then display 'List is Empty!!!' and terminate the function.

3.       If it is Not Empty then, define a Node pointer 'temp' and initialize with head.

4.       Keep displaying temp → data with an arrow (--->) until temp reaches to the last node

Finally display temp → data with arrow pointing to head → data.

Example:

1.      Create templated Circular Linked List ADT and provide

a.       Constructor / copy constructor

b.      Destructor

c.       InsertAtStart(intval)

d.      InsertAtAnyposition(intposition,intval)

e.       insertAtEnd(intval)

f.        deleteAtstart()

g.      DeleteAtAnyposition(node)

h.      deleteAtEnd()

i.        Traverse()

j.        isEmpty()


CODE:

#include <iostream>

using namespace std;

 

template <class type>

struct node

{

    type data;

    node<type>* next;

};

 

template <class type>

class CircularList

{

public:

    node<type>* tail;

 

    CircularList()

    {

        tail = NULL;

    }

 

    bool isEmpty()

    {

        if (tail == NULL)

        {

            return true;

        }

        else

            return false;

    }

 

    void iAs(type val)

    {

        node<type>* s = new node<type>;

        s->data = val;

        s->next = NULL;

 

        if (isEmpty())

        {

            tail = s;

            s->next = tail;

        }

        else

        {

            s->next = tail->next;

            tail->next = s;

        }

    }

 

    void iAAp(int pos, type data)

    {

        if (pos == 1)

        {

            iAs(data);

        }

        else

        {

            node<type>* p = new node<type>;

            p->data = data;

            p->next = NULL;

            node<type>* temp = tail;

 

            for (int i = 1; i < pos - 1; i++)

            {

                temp = temp->next;

            }

            p->next = temp->next;

            temp->next = p;

            delete temp;

        }

    }

 

    void iAe(type data)

    {

        if (isEmpty())

        {

            iAs(data);

        }

        else

        {

            node<type>* s = new node<type>;

            s->data = data;

            s->next = tail->next;

            tail->next = s;

 

        }

    }

 

    void dAs()

    {

        if (isEmpty())

        {

            cout << "The list is Empty. Deletion is not possible!!\n";

        }

        else

        {

            node<type>* temp = tail->next;

 

            if (temp->next != tail)

            {

 

                tail->next = tail->next->next;

                delete temp;

            }

            else

            {

                delete temp;

                tail = NULL;

            }

        }

    }

 

    void dAAp(int pos)

    {

        if (isEmpty())

        {

            cout << "The list is Empty. Deletion is not possible!!\n";

        }

 

        if (pos == 1)

        {

            dAs();

        }

 

        else

        {

            node<type>* temp1 = tail;

            if (temp1->next = tail)

            {

                delete temp1;

                tail = NULL;

            }

            else

            {

                node<type>* temp2 = new node <type>;

 

 

                for (int i = 1; i < pos - 1; i++)

                {

                    temp1 = temp1->next;

                }

 

                temp2 = temp1->next;

                temp1->next = temp1->next->next;

                delete temp2;

            }

        }

 

 

    }

 

 

    void dAe()

    {

        if (isEmpty())

        {

            cout << "The list is Empty. Deletion is not possible!!\n";

        }

        else

        {

            node<type>* temp = tail->next;

            node<type>* temp2 = tail->next->next;

 

            if (temp->next == tail)

            {

                delete temp;

                tail = NULL;

            }

            else

            {

                node<type>* temp = tail->next;

                while (temp->next != tail)

                {

                    temp = temp->next;

                }

                temp->next = tail->next;

                delete tail;

                tail = temp;

            }

        }

 

    }

 

    void Traverse()

    {

        if (isEmpty())

        {

            cout << "The list is Empty....!!!!!";

        }

        else

        {

            node<type>* n = tail->next;

 

            do

            {

                cout << n->data << " ";

                n = n->next;

            } while (n != tail->next);

        }

    }

 

    CircularList(const CircularList& a)

    {

        tail = a.tail;

        if (isEmpty())

        {

            tail = NULL;

        }

        else

        {

            node<type>* temp = a.tail;

            node<type>* n = new node<type>;

            n->data = temp->data;

            tail = n;

            temp->next = tail->next;

            node<type>* temp2 = n;

 

            while (temp != NULL)

            {

                node<type>* b = new node<type>;

                temp2->next = b;

                temp2->data = temp->data;

                temp = temp->next;

                temp2 = temp2->next;

 

            }

            temp->next = NULL;

        }

    }

 

    ~CircularList()

    {

        if (isEmpty())

        {

            tail = NULL;

        }

        else

        {

            node<type>* temp1 = tail;

 

 

            do

            {

                node<type>* temp2 = temp1;

                temp1 = temp1->next;

                delete temp2;

 

            } while (temp1 != tail);

 

            tail = NULL;

 

        }

    }

 

 

};

 

int main()

{

    CircularList<int> obj1;

    CircularList<int> obj2(obj1);

 

    cout <<  obj1.isEmpty() << endl;

    for (int p = 1; p <= 5; p++)

    {

        obj1.iAs(p);

    }

    cout << obj1.isEmpty() << endl;

 

    cout << endl;

    obj1.Traverse();

    obj1.iAs();

 

    cout << endl;

    obj1.Traverse();

    obj1.iAs(6);

 

    cout << endl;

    obj1.Traverse();

    obj1.iAe(9);

 

    cout <<endl;

    obj1.Traverse();

    obj1.dAe();

 

    cout << endl;

    obj1.Traverse();

    obj1.dAAp(3);

 

    cout << "Displaying: ";

    obj1.Traverse();

    obj1.iAAp(4, 7);

 

    cout << endl;

    obj1.Traverse();

 

    CircularList<float> obj3;

    obj3.iAe(1.5);

    obj3.iAe(2.5);

    obj3.iAe(3.5);

    obj3.iAe(4.5);

    obj3.iAe(5.5);

    obj3.iAe(2);

 

    cout << endl;

    obj1.Traverse();

    obj3.iAs(5.5);

    obj3.iAe(6.5);

    obj3.iAAp(5, 7.7);

 

    cout << endl;

    obj1.Traverse();

 

}

CLICK HERE TO RUN THE CODE

Some Question Think And Answer:

1.    If you do not provide copy constructor in the above ADT, program will generate a run time error at the time of destructor calling. Why?

1.      Consider a small circular linked list. How to detect the presence of cycles in this list effectively?

1.      What will happen if you do not provide destructor to your Circular linked list ADT?





No comments:

Post a Comment

Pages