| Article Index |
|---|
| Circularly Singly Linked List |
| Source Code |
| Documentation |
| Test Cases |
| Compatibility & References |
| All Pages |
CIRCULARLY SINGLY LINKED LIST
Introduction :
A singly linked circular list is a linked list where the last node in the list points to the first node in the list. A circular list does not contain NULL pointers. A good example of an application where circular linked list should be used is a timesharing problem solved by the operating system. In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small slice of CPU time, one user at a time. The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user, etc. For this application, there should be no NULL pointers unless there is absolutely no one requesting CPU time.
The insert, delete and search operations are similar to the singly linked list. In circular linked list, we should always maintain that the next node of the tail is linked to the head after the insertion and deletion operations.
Advantages:
-
Given any node, we can traverser the entire list.
-
Certain operations, such as concatenation and splitting of string, is more efficient with circular linked list.
Disadvantage:
-
Danger of an infinite loop ! (The header node is used to prevent infinite loop)
Circularly singly linked list operations :
* Inserting a node (new) in the beginning when the list is empty

* Inserting a node (new) in the beginning when the list is not empty

* Inserting a node (new) in the end of the list

Let end1 and end2 be the position of the end pointer before and after insertion respectively.
* Inserting a node (new) in the middle of the list

To insert a node in the middle, the list is traversed till the position where the node to be inserted is reached using a temp node. Here, the above code inserts the new node after the temp node.
* Deleting a node from the list
To delete a node in the end, traverse the list till the last node is reached using temp pointer. The pointer prev is made to point the node before the node which is to be deleted. The deletion of 4 is shown below.

Code :
Goal of the code :
The aim of this code is to perform basic operations of circularly linked list such as
-
Inserting element at the beginning of the list
-
Inserting element at the middle of the list at a position specified by the user
-
Inserting element at the end of the list
-
Deleting an element from the list
-
Searching for an element in the list
Variables Used :
§ header-This is a dummy node which points to the first node of the list.
§ new1 -This points to the new node to be inserted
§ temp -This is the temporary node used for traversing the list
§ prev -This pointer is used to track the node(i.e. to find the node previous to the node to be deleted)
§ end -This pointer points to the last node of the list
§ info -This variable stores the element in the information part of the node
§ next -This points the next node in the list
§ x -This variable stores the element to be inserted (used in functions)
§ j -This variable stores the element to be inserted or deleted (used in main() )
§ pos -This variable stores the position where the element is to be inserted (used in insertmid() function)
§ k -This variable stores the position where the element is to be inserted (used in main() )
§ n -This variable stores the user's choice and is used in switch-case to transfer the control to corresponding function
Functions Used :
|
Functions
|
Use
|
|
makeempty()
|
To empty the list
|
|
isempty()
|
To check whether the list is empty or not
|
|
create()
|
It allocates memory and thus creates a new list
|
|
insertbeg()
|
To insert a node at the beginning of the list
|
|
insertmid()
|
To insert a node at the middle
|
|
insertend()
|
To insert a node into the list at the end
|
|
display()
|
To display the contents of the list
|
|
deletee()
|
To delete a specific node
|
|
search()
|
To search for a specific node
|
|
deletelist()
|
To delete the entire list
|
|
main()
|
To input values from the user and to call other functions
|
Implementation:
§ In the main(), a list is created using create() function.
§ In case of 'Insert begin' and 'Insert end', the element to be inserted is got from the user and the insertion operation is performed using insertbeg() and insertend() functions respectively.
§ In case of 'Insert middle', the element to be inserted and the position of the new node are got from the user and the insertion operation is performed using insertmid() function.
§ In case of 'Delete', the element to be deleted is got from the user and then deletion operation is performed using deletee() function.
§ In case of 'Search', the element to be searched for is got from the user and its availability is printed using search() function.
Test cases :
Compatibility :
Compatible with all computers which is capable of running gcc-4.4.0. The program has been tested with the above version of gcc as of June 15.
References :
http://www.algorithmist.com/index.php/Linked_List#Circular_Lists
http://www.cs.usask.ca/content/resources/tutorials/csconcepts/2000_1/Tutorial/clist.html
**********
| < Prev | Next > |
|---|




