using System;
using System.Collections.Generic;
using System.Text;
namespace Linear
{
interface IDsLinear<T>
{
void Clear();
bool IsEmpty();
void Append(T element);
void Insert(T element,int position);
void Delete(int i);
T GetElement(int i);
int FindValue(T element);
int PreElement(T element,ref T preEle);
int NextElement(T element,ref T nextEle);
}
}
//顺序表
using System;
using System.Collections.Generic;
using System.Text;
namespace Linear
{
/// <summary>
/// 顺序表
/// </summary>
/// <typeparam name="T">设置顺序表存储的类型</typeparam>
public class SeqList<T> : IDsLinear<T>
{
private int maxSize;
private T[] data;
private int lastPoint;
/// <summary>
/// 初始化顺序表
/// </summary>
/// <param name="maxSize">指定顺序表中最大的存储空间,当maxSize=0时,默认为100</param>
public SeqList(int maxSize)
{
if (maxSize < 0)
{
maxSize = 100;
}
this.maxSize = maxSize;
lastPoint = -1;
data = new T[maxSize];
}
public SeqList() : this(100)
{
}
public T this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
/// <summary>
/// 返回线性表的最大长度
/// </summary>
public int MaxLenth
{
get
{
return maxSize;
}
}
/// <summary>
/// 判断线性表是否为满
/// </summary>
/// <returns></returns>
public bool IsFull()
{
return (lastPoint == (maxSize - 1));
}
/// <summary>
/// 判断线性表是否为空
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
return (lastPoint==-1);
}
/// <summary>
/// 返回线性表的长度
/// </summary>
/// <returns></returns>
public int GetLength()
{
if (IsEmpty())
return 0;
else
return (lastPoint + 1);
}
/// <summary>
/// 清除线性表的元素
/// </summary>
public void Clear()
{
lastPoint = -1;
}
/// <summary>
/// 将指定元素追回到线性表的尾部
/// </summary>
/// <param name="element">要追回的元素</param>
public void Append(T element)
{
if(IsFull())
throw new InvalidOperationException("SeqList is full");
data[++lastPoint] = element;
}
/// <summary>
/// 在指定下标处插入新元素
/// </summary>
/// <param name="element">要插入的元素</param>
/// <param name="position">要插入的位置</param>
/// <exception cref="">ArgumentOutOfRangeException</exception>
/// <exception cref="">InvalidOperationException</exception>
public void Insert(T element, int position)
{
if (position < 0 || position > lastPoint + 2)
throw new ArgumentOutOfRangeException("position", "the insert position out of rang");
if(IsFull())
throw new InvalidOperationException("SeqList is full");
for (int i = lastPoint; i >= position; i--)
{
data[i + 1] = data[i];
}
data[position] = element;
++lastPoint;
}
/// <summary>
/// 删除指定下标的元素
/// </summary>
/// <param name="position">要删除的元素下标</param>
/// <exception>ArgumentOutOfRangeException</exception>
public void Delete(int position)
{
if (position < 0 || position > lastPoint)
throw new ArgumentOutOfRangeException("position", "the delete position out of rang");
for (int i = position; i < lastPoint+1; i++)
{
data[i] = data[i + 1];
}
--lastPoint;
}
/// <summary>
/// 返回指定下标的元素
/// </summary>
/// <param name="position">要返回的元素下标</param>
/// <returns>返回元素的值</returns>
/// <exception cref="">ArgumentOutOfRangException</exception>
public T GetElement(int position)
{
if (position < 0 || position > lastPoint)
throw new ArgumentOutOfRangeException("position", "the get position out of rang");
return data[position];
}
/// <summary>
/// 返回指定元素的下标
/// </summary>
/// <param name="element">要返回其下标的元素</param>
/// <returns> -1表示没找到</returns>
public int FindValue(T element)
{
if (!IsEmpty())
{
for (int i = 0; i <= lastPoint; i++)
{
if (data[i].Equals( element))
return i;
}
}
return -1;
}
/// <summary>
/// 返回指定元素的前驱的下标
/// </summary>
/// <param name="element">要返回其前驱的下标的元素</param>
/// <returns>-1表示没找到</returns>
public int PreElement(T element,ref T preEle)
{
if (IsEmpty())
return -1;
for (int i = 0; i <= lastPoint; i++)
{
if (data[i].Equals(element))
{
preEle = data[i - 1];
return (i - 1);
}
}
return -1;
}
/// <summary>
/// 返回指定元素的后继的下标
/// </summary>
/// <param name="element">要返回其后继的下标的元素</param>
/// <returns>-1表示没找到</returns>
public int NextElement(T element,ref T nextEle)
{
if (IsEmpty())
return -1;
for (int i = 0; i <= lastPoint; i++)
{
if (data[i].Equals( element))
{
nextEle = data[i + 1];
return (i + 1);
}
}
return -1;
}
}
}
//单链表
using System;
using System.Collections.Generic;
using System.Text;
namespace Linear
{
/// <summary>
/// 链表结点类
/// </summary>
/// <typeparam name="T">链表结点的类型</typeparam>
public class LinkNode<T>
{
public T data;
public LinkNode<T> next;
}
/// <summary>
/// 没有头结点的单链表
/// </summary>
/// <typeparam name="T">设置单链表存储的类型</typeparam>
public class LinkList<T> : IDsLinear<T>
{
public LinkNode<T> H; //开始结点
private LinkNode<T> p; //尾结点
private int len;
/// <summary>
/// 数据域
/// </summary>
public T Data
{
get { return H.data; }
set { H.data = value; }
}
/// <summary>
/// 指针域
/// </summary>
public LinkNode<T> Next
{
get { return H.next; }
set { H.next = value; }
}
/// <summary>
/// 链表的长度
/// </summary>
public int LENGTH
{
get { return len; }
}
#region 构造函数
public LinkList()
{
H = null;
p = null;
len = 0;
}
public LinkList(T data)
{
this.H = new LinkNode<T>();
this.H.data = data;
++len;
p = H;
}
#endregion
#region IDsList<T> 成员
public void Clear()
{
H = null;
p = null;
len = 0;
}
public bool IsEmpty()
{
return len == 0;
}
/// <summary>
/// 在尾部追加结点
/// </summary>
/// <param name="element">要追加的元素</param>
public void Append(T element)
{
LinkNode<T> s = new LinkNode<T>();
s.data = element;
++len;
if (H == null)
{
H = s;
p = H;
return;
}
p.next = s;
p = s;
}
/// <summary>
/// 插入结点
/// </summary>
/// <param name="element">要插入的数据</param>
/// <param name="position">数据的位置,从1开始</param>
public void Insert(T element, int position)
{
if (H == null)
throw new Exception("The LinkList is not exist");
if (position <= 0)
throw new ArgumentException("The LinkList argument less than zero");
if (position > len + 1) // position指定在几个元素前插入数据
throw new ArgumentException("The LinkList argument out of range");
LinkNode<T> ln = H;
LinkNode<T> temp = new LinkNode<T>();
temp.data = element;
if (position == 1)
{
temp.next = H;
H = temp;
}
else
{
for (int k = 0; k < position - 2; k++)
ln = ln.next;
LinkNode<T> next = ln.next;
ln.next = temp;
temp.next = next;
}
if (position == len + 1)
p = temp;
++len;
}
/// <summary>
/// 删除指定位置的数据
/// </summary>
/// <param name="i">数据的位置,从1开始</param>
public void Delete(int i)
{
if (H == null)
throw new Exception("The LinkList is not exist");
if (i <= 0)
throw new ArgumentException("The LinkList argument less than zero");
if (i > len)
throw new ArgumentException("The position in LinkList out of range");
LinkNode<T> ln = H;
if (i == 1)
{
ln = ln.next;
H = ln;
}
else
{
int j = 0;
while (j < i - 2)
{
ln = ln.next;
j++;
}
if (ln.next.next != null)
ln.next = ln.next.next;
else
ln.next = null;
if (i == len)
p = ln;
}
--len;
}
/// <summary>
/// 取指定位置的数据
/// </summary>
/// <param name="i">数据的位置</param>
/// <returns>返回取到的数据</returns>
public T GetElement(int i)
{
if (H == null)
throw new Exception("The LinkList is not exist");
if (IsEmpty())
throw new Exception("The LinkList is empty");
if (i < 0)
throw new ArgumentException("The LinkList argument less than zero");
if (i > len)
throw new ArgumentException("The position in LinkList out of range");
LinkNode<T> ln = H;
if (i == 0)
return ln.data;
for (int k = 0; k < i - 1; k++)
ln = ln.next;
return ln.data;
}
/// <summary>
/// 查找指定数据的位置
/// </summary>
/// <param name="element">要查找的数据</param>
/// <returns>返回指定数据的位置,失败返回-1</returns>
public int FindValue(T element)
{
if (H == null)
throw new Exception("The LinkList is not exist");
if (IsEmpty())
throw new Exception("The LinkList is empty");
LinkNode<T> ln = H;
int result = -1;
for (int k = 0; k < len; k++)
{
if (ln.data.Equals(element))
{
result = k;
break;
}
ln = ln.next;
}
return result;
}
/// <summary>
/// 查找指定数据的后继
/// </summary>
/// <param name="element">要查找的数据</param>
/// <param name="nextEle">要查找数据的后继</param>
/// <returns>返回查找数据后缀的索引,不存在则返回-1</returns>
public int NextElement(T element, ref T nextEle)
{
if (H == null)
throw new Exception("The LinkList is not exist");
if (IsEmpty())
throw new Exception("The LinkList is empty");
LinkNode<T> ln = H;
int result = -1;
for (int k = 0; k < len; k++)
{
if (ln.data.Equals(element))
{
if (ln.next != null)
{
nextEle = ln.next.data;
result = k + 1;
}
break;
}
if (ln.next != null)
ln = ln.next;
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="element"></param>
/// <param name="preEle"></param>
/// <returns></returns>
public int PreElement(T element, ref T preEle)
{
if (H == null)
throw new Exception("The LinkList is not exist");
if (IsEmpty())
throw new Exception("The LinkList is empty");
LinkNode<T> ln = H;
int result = -1;
for (int k = 0; k < len; k++)
{
if (k == 0 && ln.data.Equals(element))
return result;
if (ln.next != null)
{
if (ln.next.data.Equals(element))
{
preEle = ln.data;
result = k;
break;
}
ln = ln.next;
}
}
return result;
}
#endregion
public void Display(LinkNode<T> p)
{
do
{
if (p.next != null)
Console.Write(p.data.ToString() + "->");
else
Console.WriteLine(p.data.ToString());
p = p.next;
} while (p != null);
}
public void ResvLinklist(LinkNode<T> head)
{
LinkNode<T> d = H;
if (d == null || d.next == null)
return ;
//前指针 尾指针 当前元素
LinkNode<T> pre = null, rear = null, temp = null;
while (d.next != null)
{
rear = d.next;
pre = d;
pre.next = temp;
d = rear.next;
rear.next = pre;
if (rear != null)
temp = rear;
}
d.next = temp;
H = d;
}
}
}