Click here to Skip to main content
Click here to Skip to main content

Demonstration of Sorted Cyclic Linked List

, 18 Sep 2012
Rate this:
Please Sign up or sign in to vote.
A simple UI based solution to demonstrate how to implement Cyclic Linked List.

Introduction 

This article shows the key logic points which must be taken care while implementing a Sorted Cyclic Linked List. 

It is useful specially for students and beginners in development. 

Background 

Today a fresher engineering graduate asked me can I help her solving the Sorted Cyclic Linked List problem, I thought a working code will be best way to solve her problem. Uploading here to help other freshers and students in the similar need. 

Using the code 

The code it self is very simple, it contains a Visual Studio solution. Just open that in Visual Studio 2010 and run (F5). On the form you can start giving integer value to let that added in a cyclic linked list, the output (flattened) will keep appearing on the form itself. 

Full source code is attached with this article, you will need Visual Studio 2010 to open it. If you have some previous version of Visual Studio then you can add the Form1.cs, Form1.Designer.cs, Form1.resx, and Node.cs to add in a new project.

User Interface

Points of Interest 

The key method AddNode() is explained below in wording and then in code. 

  • AddNode(currentNode, valueToAdd)
    • First it checks if the current passed node is null if yes (in case of first node getting added), it creates the node and put this newly created node in next of itself. 
    • Then it check if the valueToAdd is >= to currentNode's value and < currentNode.Next's value, if yes, add the node in between currentNode and currentNode.Next. It also check if the valueToAdd is less than even the header's value then add it prior to that header (or say after the last node which also contains maximum value)
    • If nothing above is true, then call AddNode(currentNode.Next, valueToAdd) so that it keep searching for correct location via recursion until it find its currect location. 
private void AddNode(ref Node currentNode, int valueToAdd)
{
	//To create the first node (header) if the list is completly blank
	if (currentNode == null)
    {
        currentNode = new Node(valueToAdd, null);
        currentNode.Next = currentNode;
    }
	//To find the correct location for new value to add
    else if( ((currentNode.Data <= valueToAdd) && 
           (currentNode.Next.Data > valueToAdd) ) || (currentNode.Next == this.header ))
    {
        Node newNode = new Node(valueToAdd, currentNode.Next);
        
        if (currentNode.Next == header && valueToAdd < currentNode.Next.Data)
            this.header = newNode;

        currentNode.Next = newNode;
	}
	//If not found, then proceed to next node via recursion
    else
        AddNode(ref currentNode.Next, valueToAdd);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anuj Chauhan from India
Software Developer (Senior) Royal Bank of Scotland
India India
If the going is real easy, beware, you may be headed down hill.

Comments and Discussions

 
GeneralMy vote of 3 PinmemberJohn Brett20-Sep-12 0:36 
QuestionNice but.. PinmemberFatCatProgrammer18-Sep-12 7:16 
AnswerRe: Nice but.. PinmemberAnuj Chauhan from India18-Sep-12 20:42 
GeneralRe: Nice but.. PinmemberFatCatProgrammer19-Sep-12 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by Anuj Chauhan from India
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid