本文章纯属个人练习笔记,如有错误欢迎纠正。
顺序表和单链表实现
1、通用接口实现如下:
namespace 手撕数据结构之线性表
{
/// <summary>
/// 通用接口
/// </summary>
/// <typeparam name="T"></typeparam>
interface IList<T>
{
void Add(T item); //添加
void Insert(T item, int index); //插入
T Remove(int index); //删除
void Clear(); //清空
int GetLength(); //获取表长
bool IsEmpty(); //判空
T FindEle(int index); //查找元素
int Local(T item); //获取数据位置
}
}
2、顺序表实现类如下:
namespace 手撕数据结构之线性表
{
/// <summary>
/// 顺序表
/// </summary>
class SeqList<T> : IList<T>
{
T[] datas; //数据存储
int length; //记录表长
public SeqList(int size) //构造函数初始化
{
this.datas = new T[size];
length = 0;
}
public SeqList() : this(10) //默认初始化表容量为10
{
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
if (length == datas.Length)
{
Console.WriteLine("当前顺序表容量已满!");
return;
}
datas[length] = item; //将数据存入表中
length++; //表长加1
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="item"></param>
/// <param name="index"></param>
public void Insert(T item, int index)
{
if (index < 0 || index > datas.Length - 1)
{
Console.WriteLine("当前插入索引溢出!");
return;
}
for (int i = length - 1; i >= index; i--)
{
datas[i + 1] = datas[i]; //把当前索引值后的数据往后移
}
datas[index] = item;
length++;
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T Remove(int index)
{
if (IsEmpty())
{
Console.WriteLine("该顺序表为空!");
return default(T);
}
else
{
T temp = datas[index];
for (int i = index + 1; i < length; i++)
{
datas[i - 1] = datas[i]; //数据前移
}
length--;
return temp;
}
}
/// <summary>
/// 清空
/// </summary>
public void Clear()
{
length = 0;
}
/// <summary>
/// 查找
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T FindEle(int index)
{
if (IsEmpty())
{
Console.WriteLine("该顺序表为空!");
return default(T);
}
else
{
if (index < 0 || index > datas.Length - 1)
{
Console.WriteLine("当前查询索引溢出!");
return default(T);
}
return datas[index];
}
}
/// <summary>
/// 获取表长
/// </summary>
/// <returns></returns>
public int GetLength()
{
return length;
}
/// <summary>
/// 判空
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
return length == 0;
}
/// <summary>
/// 获取数据所在索引
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int Local(T item)
{
if (IsEmpty())
{
Console.WriteLine("该顺序表为空!");
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
if (item.Equals(datas[i]))
return i;
}
return -1;
}
}
}
}
测试代码:
namespace 手撕数据结构之线性表
{
class Program
{
static void Main(string[] args)
{
SeqList<string> seqList = new SeqList<string>();
seqList.Add("张三");
seqList.Add("李四");
seqList.Add("王五");
Console.WriteLine(seqList.GetLength());
seqList.Remove(0);
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.WriteLine(seqList.FindEle(i));
}
seqList.Insert("老六", 0);
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.WriteLine(seqList.FindEle(i));
}
seqList.Clear();
Console.WriteLine(seqList.GetLength());
}
}
}
3、单链表实现如下:
单链表节点类:
namespace 手撕数据结构之线性表
{
/// <summary>
/// 链表的节点类
/// </summary>
/// <typeparam name="T"></typeparam>
class Node<T>
{
private T data; //数据存储
private Node<T> next; //指向下一个节点指针
public Node()
{
data = default(T);
next = null;
}
public Node(T data)
{
this.data = data;
next = null;
}
public Node(Node<T> next)
{
this.next = next;
}
public Node(T data, Node<T> next)
{
this.data = data;
this.next = next;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
单链表实现类:
namespace 手撕数据结构之线性表
{
/// <summary>
/// 单链表
/// </summary>
class LinkList<T> : IList<T>
{
private Node<T> head; //头节点
public LinkList()
{
head = null;
}
/// <summary>
/// 添加节点
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
Node<T> newNode = new Node<T>(item);
if (head == null) //如果头节点为空,即新节点就为头节点
{
head = newNode;
}
else
{
Node<T> temp = head;
while (temp.Next != null)
{
temp = temp.Next;
}
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.Next;
}
Node<T> preNode = temp;
Node<T> nextNode = temp.Next;
preNode.Next = newNode;
newNode.Next = nextNode;
}
}
/// <summary>
/// 删除节点
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T Remove(int index)
{
T data = default(T);
if (IsEmpty())
{
Console.WriteLine("链表为空!");
return data;
}
if (index == 0) //如果删除的是头节点
{
data = head.Data;
head = head.Next;
return data;
}
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;
data = currentNode.Data;
preNode.Next = currentNode.Next;
return data;
}
}
/// <summary>
/// 清空链表
/// </summary>
public void Clear()
{
head = null;
}
/// <summary>
/// 获取表长
/// </summary>
/// <returns></returns>
public int GetLength()
{
if (IsEmpty())
return 0;
int length = 1;
Node<T> temp = head;
while (temp.Next != null)
{
length++;
temp = temp.Next;
}
return length;
}
/// <summary>
/// 查询链表元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T FindEle(int index)
{
T data = default(T);
if (IsEmpty())
{
Console.WriteLine("链表为空!");
return data;
}
Node<T> temp = head;
for (int i = 0; i <= index - 1; i++)
{
temp = temp.Next;
}
return temp.Data;
}
/// <summary>
/// 查找当前元素的索引位置
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int Local(T item)
{
int index = 0;
if (IsEmpty())
{
Console.WriteLine("链表为空!");
return -1;
}
Node<T> temp = head;
while (temp.Next != null)
{
if (!item.Equals(temp.Data))
{
temp = temp.Next;
index++;
}
else
{
return index;
}
}
return -1;
}
public bool IsEmpty()
{
return head == null;
}
}
}
测试代码:
namespace 手撕数据结构之线性表
{
class Program
{
static void Main(string[] args)
{
LinkList<string> seqList = new LinkList<string>();
seqList.Add("张三");
seqList.Add("李四");
seqList.Add("王五");
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.WriteLine(seqList.FindEle(i));
}
Console.WriteLine(seqList.GetLength());
seqList.Remove(0);
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.WriteLine(seqList.FindEle(i));
}
seqList.Insert("老六", 0);
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.WriteLine(seqList.FindEle(i));
}
Console.WriteLine(seqList.Local("老六"));
seqList.Clear();
Console.WriteLine(seqList.GetLength());
}
}
}