본문 바로가기
[ Program ]/C#

[C#자료구조]Linked-List

by 관이119 2012. 9. 18.
출처 카페 > C# private C# | esct33
원문 http://cafe.naver.com/privatecs/9

 

자료구조의 기본이라고 할수 있는 링크리스트를 c#으로 구현했습니다.

비교적 간단한 자료구조이지만

정적 자료구조인 배열과 비교한다면 링크리스트는 동적으로 크기를 늘리거나 줄일수 있는 자료구조 입니다.

 

public class ListElement
{
private string data;
private ListElement nextData;

public ListElement(string str)
{
data=str;
nextData=null;
}

public ListElement next()
{
return nextData;
}

public bool equals(string str)
{
return str.Equals(str);
}

public void insert(ListElement elem)
{
elem.nextData=this.nextData;
this.nextData=elem;
return;
}

public void removeNext()
{
ListElement elem=next();
if(elem==null)
return;
this.nextData=null;
elem.nextData=null;
return;
}

public override string ToString()
{
return data.ToString();
}

}

public class LinkList
{
private ListElement head;

public LinkList()
{
head=new ListElement("head");
return;
}

public void add(string str)
{
ListElement elem=new ListElement(str);
head.insert(elem);
return;
}

public void remove(string str)
{
ListElement elem;
for(elem=head; elem.next()!=null; elem=elem.next())
{
ListElement next=elem.next();
if(next.equals(str))
{
elem.removeNext();
break;
}
}
return;
}

댓글