Created
December 16, 2012 08:33
-
-
Save halfcoder/4305135 to your computer and use it in GitHub Desktop.
A CircularyLinkedList implement in C#, based on LinkedList(.NET Framework 2.0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created by SharpDevelop. | |
* User: halfcoder | |
* Date: 2012/12/16 | |
* Time: 15:03 | |
* | |
* To change this template use Tools | Options | Coding | Edit Standard Headers. | |
*/ | |
using System; | |
using System.Collections.Generic; | |
namespace halfcoder.DataStructure | |
{ | |
/// <summary> | |
/// Description of CircularyLinkedList. | |
/// </summary> | |
public class CircularyLinkedList<T> : LinkedList<T> | |
{ | |
private LinkedListNode<T> _currentNode; | |
private int _currentIndex; | |
public int CurrentIndex | |
{ | |
get | |
{ | |
return this._currentIndex; | |
} | |
} | |
public CircularyLinkedList() : base() | |
{ | |
this._currentNode = this.First; | |
this._currentIndex = 0; | |
} | |
public CircularyLinkedList(IEnumerable<T> collection) : base(collection) | |
{ | |
this._currentNode = this.First; | |
this._currentIndex = 0; | |
} | |
public T GetCurrentNode() | |
{ | |
if (this.Count == 0) { | |
throw new IndexOutOfRangeException(); | |
} | |
return this._currentNode.Value; | |
} | |
public T GetPreviousNode() | |
{ | |
if (this.Count == 0) { | |
throw new IndexOutOfRangeException(); | |
} | |
if (this._currentNode == null || this._currentNode == this.First) { | |
this._currentNode = this.Last; | |
this._currentIndex = this.Count - 1; | |
} | |
else { | |
this._currentNode = this._currentNode.Previous; | |
this._currentIndex--; | |
} | |
return this._currentNode.Value; | |
} | |
public T GetNextNode() | |
{ | |
if (this.Count == 0) { | |
throw new IndexOutOfRangeException(); | |
} | |
if(this._currentNode == null || this._currentNode == this.Last) { | |
this._currentNode = this.First; | |
this._currentIndex = 0; | |
} | |
else { | |
this._currentNode = this._currentNode.Next; | |
this._currentIndex++; | |
} | |
return this._currentNode.Value; | |
} | |
public T PopCurrentNode() | |
{ | |
LinkedListNode<T> savedNode = this._currentNode; | |
T returnValue = this.GetNextNode(); | |
this.Remove(savedNode); | |
return returnValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment