# 单链表的实现

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DataStructure

{

class LinkList<T> : IListDS<T>

{

class Node<T>

{

private T data;

/// <summary>

/// 数据域

/// </summary>

public T Data

{

get { return data; }

set { data = value; }

}

private Node<T> next;

/// <summary>

/// 引用域

/// </summary>

public Node<T> Next

{

get { return next; }

set { next = value; }

}

//头结点构造函数

public Node(Node<T> node)

: this(default(T), node)

{

}

//普通结点构造函数

public Node(T data, Node<T> node)

{

this.Data = data;

this.Next = node;

}

/// <summary>

/// 空构造函数

/// </summary>

public Node()

: this(default(T), null)

{

}

//尾结点构造函数

public Node(T data)

: this(data, null)

{

}

}

{

}

public int Lenth

{

get { return this.GetLength(); }

}

{

this.Head = new Node<T>(t[0]);

Node<T> Last;

for (int i = 1; i < t.Length; i++)

{

//尾插入法

Last.Next = new Node<T>(t[i]);

Last = Last.Next;

}

}

public States Append(T item)

{

//(1)头结点没有

{

Head = new Node<T>(item);

}

//(2)正常的插入情况

Node<T> Last;

//遍历到最后一个结点，在最后一个结点附加上

while (null!=Last.Next)

{

Last = Last.Next;

}

Last.Next = new Node<T>(item);

return States.Success;

}

public void Clear()

{

}

public T Delete(int index, out States states)

{

bool isIndexTrue = index < 0 || index >= this.GetLength();

if (IsEmpty() == true || isIndexTrue)

{

states = States.Fail;

return default(T);

}

//找到指定的结点

Node<T> Current = Head;

Node<T> Previous = Head;

int Count = 0;

while (Count != index && Current.Next != null)

{

Previous = Current;

Current = Current.Next;

Count++;

}

T ValueToDel = Current.Data;

//是否是头结点

if (Count == 0)

{

}

//是否是普通的结点

if (Count != 0 && Current.Next != null)

{

Previous.Next = Current.Next;

}

//是否是最后一个结点

if (Count != 0 && Current.Next == null)

{

Previous.Next = null;

}

//删除结点

states = States.Success;

return ValueToDel;

}

public T GetElem(int i)

{

if (i < 0 || i >= this.GetLength())

{

return default(T);

}

if (IsEmpty() == true)

{

return default(T);

}

Node<T> Last = Head;

int Count = 0;

while (Count != i && Last.Next != null)

{

Last = Last.Next;

Count++;

}

return Last.Data;

}

public int GetLength()

{

{

return 0;

}

Node<T> Last;

int Count=0;

while (Last.Next!=null)

{

Last = Last.Next;

Count++;

}

return ++Count;

//throw new NotImplementedException();

}

public States Insert(T item, int index)

{

bool isIndexTrue = index < 0 || index > this.GetLength();

if (isIndexTrue)

{

return  States.Fail;

}

//如果链表为空

if (IsEmpty() == true )

{

}

//如果是第一个结点

if (index==0)

{

Node<T> node = new Node<T>(item);

}

//如果是普通的结点

Node<T> Previous = Head;

//  Node<T> Previous = Head;

int Count = 0;

while (Count != index1 && Previous.Next != null)

{

//    Previous = Current;

Previous = Previous.Next;

Count++;

}

if (Count == index1)

{

Node<T> node=new Node<T>(item);

node.Next = Previous.Next;

Previous.Next = node;

}

//如果是最后一个结点

if (this.GetLength()==index)

{

Previous.Next = new Node<T>(item);

}

return States.Success;

}

public bool IsEmpty()

{

return Head == null ? true : false;

}

public int Locate(T value, out States states)

{

if (IsEmpty() == true)

{

states = States.Fail;

return 0;

}

Node<T> Last = Head;

int Count = 0;

while (Last.Next != null &&( !value.Equals(Last.Data)))

{

Last = Last.Next;

Count++;

}

states = States.Success;

return Count;

}

}

}

原文作者：陈哈哈
原文地址: https://www.cnblogs.com/kissazi2/p/3193957.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。