线性表:
public interface IListDS<T>
{
int GetLength();
void Clear();
bool IsEmpty();
void Add(T item);
void Insert(T item,int index);
T Delete(int index);
T this[int index]{get;}
T GetEle(int index);
int Locate(T value);
}
顺序实现方式:(List)
//顺序实现方式
public class SeqList<T> : IListDS<T>
{
private T[] data; //用来存储数据
private int count = 0; //表示存了多少数据
public SeqList(int size)
{
data = new T[size];
count = 0;
}
public SeqList() : this(10) //默认构造函数 容量是10
{
}
public void Add(T item)
{
if (count == data.Length)
{
Debug.Log("当前顺序已经存满,不允许在存入");
}
else
{
data[count++] = item;
}
}
public T GetEle(int index)
{
if (index >= 0 && index <= count - 1) //索引存在
{
return data[index];
}
else
{
Debug.Log( "索引不存在");
return default(T);
}
}
public T this[int index]
{
get { return GetEle(index); }
}
public void Clear()
{
count = 0;
}
public bool IsEmpty()
{
return count == 0;
}
public int GetLength()
{
return count;
}
public void Insert(T item, int index)
{
for (int i = count - 1; i >= index; i--)
{
data[i + 1] = data[i];
}
data[index] = item;
count++;
}
public T Delete(int index)
{
T temp = data[index];
for (int i = index + 1; i < count; i++)
{
data[i - 1] = data[i]; //把数据向前移动
}
count--;
return temp;
}
public int Locate(T value)
{
for (int i = 0; i < count; i++)
{
if (data[i].Equals(value))
{
return i;
}
}
return -1;
}
}
单链表:
//接口里头的方法
public interface IListDS<T>
{
int GetLength();
void Clear();
bool IsEmpty();
void Add(T item);
void Insert(T item, int index);
T Delete(int index);
T this[int index] { get; }
T GetEle(int index);
int Locate(T value);
}
//单链表结点
public class Node<T>
{
private T data; //存储数据
private Node<T> next; //指针 用来指向下一个元素
public Node()
{
data = default(T);
next = null;
}
public Node(T value)
{
this.data = value;
next = null;
}
public Node(T value, Node<T> next)
{
this.data = value;
this.next = next;
}
public Node(Node<T> next)
{
this.next = next;
}
public T Data
{
get { return this.data; }
set { this.data = value; }
}
public Node<T> Next
{
get { return this.next; }
set { this.next = value; }
}
}
链表插入的图(理解):
删除的图:
单链表的实现:
public class LinkList<T> : IListDS<T>
{
private Node<T> head; //存储一个头结点
public int GetLength()
{
Node<T> temp = head;
int count = 1;
if (head == null)
{
return 0;
}
else
{
while (true)
{
if (temp.Next != null)
{
temp = temp.Next;
count++;
}
else
{
break;
}
}
return count;
}
}
public void Clear()
{
head = null; //清空直接让头结点为空,其他都无法访问了
}
public bool IsEmpty()
{
return head == null;
}
public void Add(T item)
{
Node<T> newNode=new Node<T>(item); //根据新的数据创建一个新节点
if (head == null) //如果头结点为空,那么这个新节点就是头结点
{
head = newNode;
}
else
{//把新来的结点放到链表的尾部
//要访问链表的尾结点
Node<T> temp = head;
while (true)
{
if (temp.Next != null)
{
temp = temp.Next;
}
else
{
break;
}
}
temp.Next = newNode; //把新来的结点放到链表的尾部
}
}
public void Insert(T item, int index)
{
Node<T> newNode=new Node<T>(item);
if (index == 0) //插入到头结点
{
newNode.Next = head;
head = newNode;
}
else
{
Node<T> temp = head;
for (int i = 1; i <= index - 1; i++) //移动到当前插入节点的前一个
{
//让temp向后移动一个位置
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
preNode.Next = newNode;
newNode.Next = currentNode;
}
}
public T Delete(int index)
{
Node<T> newNode=new Node<T>();
T data;
if (index == 0) //删除头结点
{
data = head.Data;
head = head.Next;
}
else //删除的不是头结点
{
Node<T> temp = head;
for (int i = 1; i <=index - 1; i++) //移动到当前位置的上一个节点
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
Node<T> nextNode = temp.Next.Next;
data = currentNode.Data;
preNode.Next = nextNode;
}
return data;
}
public T this[int index]
{
get
{
Node<T> temp = head;
for (int i = 1; i <= index; i++)
{
temp = temp.Next;
}
return temp.Data;
}
}
public T GetEle(int index)
{
return this[index];
}
public int Locate(T value)
{
Node<T> temp = head;
int index = 0;
if (head == null)
{
return -1;
}
else
{
while (true)
{
if (temp.Data.Equals(value))
{
return index;
}
else
{
if (temp.Next != null)
{
temp = temp.Next;
index++;
}
else
{
break;
}
}
}
return -1;
}
}
}