13,298,083 members (54,576 online)
Tip/Trick
alternative version

#### Stats

7.8K views
2 bookmarked
Posted 18 Sep 2012

# Demonstration of Sorted Cyclic Linked List

 Rate this:
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.

## Points of Interest

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

• 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.Next = currentNode;
}
//To find the correct location for new value to add
else if( ((currentNode.Data <= valueToAdd) &&
{
Node newNode = new Node(valueToAdd, currentNode.Next);

currentNode.Next = newNode;
}
else
}```

## Share

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

## You may also be interested in...

 View All Threads First Prev Next
 Nice but.. FatCatProgrammer18-Sep-12 8:16 FatCatProgrammer 18-Sep-12 8:16
 Re: Nice but.. Anuj Chauhan from India18-Sep-12 21:42 Anuj Chauhan from India 18-Sep-12 21:42
 Re: Nice but.. FatCatProgrammer19-Sep-12 1:48 FatCatProgrammer 19-Sep-12 1:48
 Last Visit: 31-Dec-99 19:00     Last Update: 16-Dec-17 6:00 Refresh 1