Data structure (C #): Circular link List

Source: Internet
Author: User

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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.