Insertion into circular singly linked list at beginningThere are two scenario in which a node can be inserted in circular singly linked list at beginning. Either the node will be inserted in an empty list or the node is to be inserted in an already filled list. Show
Firstly, allocate the memory space for the new node by using the malloc method of C language. In the first scenario, the condition head == NULL will be true. Since, the list in which, we are inserting the node is a circular singly linked list, therefore the only node of the list (which is just inserted into the list) will point to itself only. We also need to make the head pointer point to this node. This will be done by using the following statements. In the second scenario, the condition head == NULL will become false which means that the list contains at least one node. In this case, we need to traverse the list in order to reach the last node of the list. This will be done by using the following statement. At the end of the loop, the pointer temp would point to the last node of the list. Since, in a circular singly linked list, the last node of the list contains a pointer to the first node of the list. Therefore, we need to make the next pointer of the last node point to the head node of the list and the new node which is being inserted into the list will be the new head node of the list therefore the next pointer of temp will point to the new node ptr. This will be done by using the following statements. the next pointer of temp will point to the existing head node of the list. Now, make the new node ptr, the new head node of the circular singly linked list. in this way, the node ptr has been inserted into the circular singly linked list at beginning. Circular Singly Linked List | InsertionWe have discussed Singly and Circular Linked List in the following post: Why Circular? In a singly linked list, for accessing any node of the linked list, we start traversing from the first node. If we are at any node in the middle of the list, then it is not possible to access nodes that precede the given node. This problem can be solved by slightly altering the structure of a singly linked list. In a singly linked list, the next part (pointer to next node) is NULL. If we utilize this link to point to the first node, then we can reach the preceding nodes. Refer to this for more advantages of circular linked lists. In this post, the implementation and insertion of a node in a Circular Linked List using a singly linked list are explained. Implementation The pointer last points to node Z and last -> next points to node P. Why have we taken a pointer that points to the last node instead of the first node? Insertion
Insertion in an empty List After inserting node T, After insertion, T is the last node, so the pointer last points to node T. And Node T is the first and the last node, so T points to itself. C++
Java
Python3
C#
Javascript
After insertion, Function to insert nodes at the beginning of the list, C++
Java
Python3
C#
Javascript
After insertion, Function to insert a node at the end of the List C++
Java
Python3
C#
Javascript
After searching and insertion, Function to insert a node at the end of the List, C++
Java
Python3
C#
Javascript
The following is a complete program that uses all of the above methods to create a circular singly linked list. C++
Java
Python3
C#
Javascript
Output: 2 4 6 8 10 12This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Article Tags :
Linked List
circular linked list Practice Tags :
Linked List circular linked list Circular Linked List | Set 1 (Introduction and Applications)We have discussed singly and doubly linked lists in the following posts. Introduction to Linked List & Insertion Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list. Advantages of Circular Linked Lists: 2) Useful for implementation of queue. Unlike this implementation, we don’t need to maintain two pointers for front and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can always be obtained as next of last. 3) Circular lists are useful in applications to repeatedly go around the list. For example, when multiple applications are running on a PC, it is common for the operating system to put the running applications on a list and then to cycle through them, giving each of them a slice of time to execute, and then making them wait while the CPU is given to another application. It is convenient for the operating system to use a circular list so that when it reaches the end of the list it can cycle around to the front of the list. 4) Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap. Next Posts : Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem
Article Tags :
Linked List
circular linked list Practice Tags :
Linked List circular linked list Insertion In Circular Linked ListThere are three situation for inserting element in Circular linked list. Circular Linked ListIn this article, you will learn what circular linked list is and its types with implementation. A circular linked list is a type of linked list in which the first and the last nodes are also connected to each other to form a circle. There are basically two types of circular linked list: 1. Circular Singly Linked List Here, the address of the last node consists of the address of the first node. Circular Linked List Representation2. Circular Doubly Linked List Here, in addition to the last node storing the address of the first node, the first node will also store the address of the last node. Circular Doubly Linked List RepresentationNote: We will be using the singly circular linked list to represent the working of circular linked list. Circular Linked ListCircular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required. Application of Circular Linked List
Implementing Circular Linked ListImplementing a circular linked list is very easy and almost similar to linear linked list implementation, with the only difference being that, in circular linked list the last Node will have it's next point to the Head of the List. In Linear linked list the last Node simply holds NULL in it's next pointer. So this will be oue Node class, as we have already studied in the lesson, it will be used to form the List. class Node { public: int data; //pointer to the next node node* next; node() { data = 0; next = NULL; } node(int x) { data = x; next = NULL; } }Circular Linked ListCircular Linked List class will be almost same as the Linked List class that we studied in the previous lesson, with a few difference in the implementation of class methods. class CircularLinkedList { public: node *head; //declaring the functions //function to add Node at front int addAtFront(node *n); //function to check whether Linked list is empty int isEmpty(); //function to add Node at the End of list int addAtEnd(node *n); //function to search a value node* search(int k); //function to delete any Node node* deleteNode(int x); CircularLinkedList() { head = NULL; } }Insertion at the BeginningSteps to insert a Node at beginning :
Insertion at the EndSteps to insert a Node at the end :
Searching for an Element in the ListIn searhing we do not have to do much, we just need to traverse like we did while getting the last node, in this case we will also compare the data of the Node. If we get the Node with the same data, we will return it, otherwise we will make our pointer point the next Node, and so on. node* CircularLinkedList :: search(int x) { node *ptr = head; while(ptr != NULL && ptr->data != x) { //until we reach the end or we find a Node with data x, we keep moving ptr = ptr->next; } return ptr; }Deleting a Node from the ListDeleting a node can be done in many ways, like we first search the Node with data which we want to delete and then we delete it. In our approach, we will define a method which will take the data to be deleted as argument, will use the search method to locate it and will then remove the Node from the List. To remove any Node from the list, we need to do the following :
Inserting a node at the beginning of a linked listThe new node will be added at the beginning of a linked list. ExampleAssume that the linked list has elements: 20 30 40 NULL If we insert 100, it will be added at the beginning of a linked list. After insertion, the new linked list will be 100 20 30 40 NULL |