A circular list can be a single linked list, or it can be a double linked list. The subsequent nodes of the tail node of the linked list point to the head node and become the cyclic list.
Here we inherit the double linked list to realize the circular list, and when it arrives at the end of the double linked list, let the cursor point to the NO. 0 node, and when the beginning of the double linked list is reached, let the cursor point to the end node, so that the circular double linked list is realized. At the end, a classic Joseph question is used as an example of the circular list.
1. Cyclic link List code:
/*
* File:CircularlyLinkedList.cs
* Author:zhenxing Zhou
* date:2008-12-07
* blog:http://www.xianfen.net/
*/
Using System;
Namespace Xianfen.Net.DataStructure
{
public class Circularlylinkedlist<t>: doublelinkedlist<t>
{
Private doublelinkedlistnode<t> M_currentnode;
private int m_currentindex;
public int Currentindex
{
get {return m_currentindex;}
}
Public Circularlylinkedlist ()
: Base ()
{
M_currentnode = M_head.next;
M_currentindex = 0;
}
Public Circularlylinkedlist (t)
: Base (t)
{
M_currentnode = M_head.next;
M_currentindex = 0;
}
Public T GetCurrent ()
{
if (m_count = 0)
{
throw new IndexOutOfRangeException ();
}
return m_currentnode.value;
}
Public T GetNext ()
{
if (m_count = 0)
{
throw new IndexOutOfRangeException ();
}
if (M_currentnode!= null)
{
M_currentnode = M_currentnode.next;
m_currentindex++;
}
if (M_currentnode = null)
{
M_currentnode = M_head.next;
M_currentindex = 0;
}
return m_currentnode.value;
}
Public T getprevious ()
{
if (m_count = 0)
{
throw new IndexOutOfRangeException ();
}
if (M_currentnode!= null)
{
M_currentnode = M_currentnode.prior;
m_currentindex--;
}
if (M_currentnode = null | | m_currentnode = = m_head)
{
M_currentnode = M_tail;
M_currentindex = m_count-1;
}
return m_currentnode.value;
}
}
}