1. 顺序表
定义了一个接口类IListDS,在类SeqList中实现。
1.1 IListDS.cs
namespace _401_线性表
{
interface IListDS<T>
{
int GetLength();
void Claer();
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);
}
}
1.2 SeqList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _401_线性表
{
class SeqList<T> : IListDS<T>
{
/// <summary>
/// 顺序表实现方式
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int count=0;//计数器,表示存了多少个数据
public T[] data;//用来存储数据
public SeqList(int size)//size就是最大容量
{
data = new T[size];
count = 0;
}
public SeqList() : this(10)//默认构造函数容量是10
{
}
public T this[int index]
{
get { return GetEle(index); }
}
public void Claer()
{
count = 0;
}
public T Delete(int index)
{
T temp = data[index];
if (index >= 0 && index <= count - 1)
{
for(int i=index+1;i<count;i++)
{
data[i - 1] = data[i];//将数据向前移动
}
count--;
}
else
{
Console.WriteLine("删除位置有误");
}
return temp;
}
public T GetEle(int index)
{
if(index>=0&&index<=count-1)//索引存在
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);//取得类型的默认值
}
}
/// <summary>
/// 取得数据的个数
/// </summary>
/// <returns></returns>
public int GetLength()
{
return count;
}
public void Insert(T item, int index)
{
if(index>=0&&index<=count-1)
{
for(int i=count-1;i>=index;i--)
{
data[i + 1] = data[i];
}
data[index] = item;
count++;
}
else
{
Console.WriteLine("插入位置有误");
}
}
public bool IsEmpty()
{
return count == 0;
}
public int Locate(T value)
{
for(int i=0;i<count;i++)
{
if(data[i].Equals(value))
{
return i;
}
}
return -1;
}
public void Add(T item)
{
if (count == data.Length)
{
Console.WriteLine("当前顺序表已满");
}
else
{
data[count] = item;
count++;
}
}
}
}
1.3 Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _401_线性表
{
class Program
{
static void Main(string[] args)
{
//1.使用BCL中的List线性表
//List<string> strList = new List<string>();
//strList.Add("34");
//strList.Add("3435");
//strList.Add("6767");
//strList.Remove("3435");
//foreach(var temp in strList)
//{
// Console.WriteLine(temp);
//}
//Console.WriteLine(strList.IndexOf("34"));
//Console.WriteLine(strList.Count);
//Console.ReadKey();
//2.使用自己定义的顺序表
SeqList<string> seqList = new SeqList<string>();
seqList.Add("45");
seqList.Add("657");
seqList.Add("243");
Console.WriteLine(seqList.GetEle(1));
Console.WriteLine("长度:" + seqList.GetLength());
seqList.Insert("888", 2);
seqList.Delete(3);
Console.WriteLine(seqList.IsEmpty());
Console.WriteLine(seqList.Locate("45"));
Console.WriteLine("遍历结果:");
for (int i = 0; i < seqList.count; i++)
{
Console.WriteLine(seqList[i]);
}
Console.WriteLine("清空后");
seqList.Claer();
Console.WriteLine("长度:" + seqList.GetLength());
Console.ReadKey();
}
}}
2. 单链表
定义了一个j节点类Node,接口类IListDS,在类LinkList中实现。
2.1 Node.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _401_线性表
{
/// <summary>
/// 单链表的节点
/// </summary>
class Node<T>
{
private T data;//数据域
private Node<T> next;//指针,用来指向下一个节点
public Node()
{
data = default(T);
next = null;
}
public Node(T value)
{
data = value;
next = null;
}
public Node(T value,Node<T> next)
{
data = value;
this.next = next;
}
public Node(Node<T> next)
{
this.next = next;
}
public T Data { get { return data; } set { data = value; } }
public Node<T> Next { get { return next; } set { next = value; } }
}
}
2.2 IListDS.cs
namespace _401_线性表
{
interface IListDS<T>
{
int GetLength();
void Claer();
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);
}
}
2.3 LinkList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _401_线性表
{
class LinkList<T> : IListDS<T>
{
private Node<T> head;//储存一个头结点
public LinkList()
{
head = null;
}
public T this[int index]
{
get
{
Node<T> temp = head;
for (int i = 1; i <= index; i++)
{
temp = temp.Next;
}
return temp.Data;
}
}
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 Claer()
{
head = null;
}
public T Delete(int index)
{
T data = default(T);
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;
preNode.Next = nextNode;
}
return data;
}
public T GetEle(int index)
{
return this[index];
}
public int GetLength()
{
if (head == null) return 0;
Node<T> temp = head;
int count = 1;
while(true)
{
if(temp.Next!=null)
{
count++;
temp = temp.Next;
}
else
{
break;
}
}
return count;
}
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 bool IsEmpty()
{
return head == null;
}
public int Locate(T value)
{
Node<T> temp = head;
if(temp==null)
{
return -1;
}
else
{
int index = 0;
while(true)
{
if(temp.Data.Equals(value))
{
return index;
}
else
{
if(temp.Next!=null)
{
temp = temp.Next;
}
else
{
break;
}
}
}
return -1;
}
}
}
}
2.4 Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _401_线性表
{
class Program
{
static void Main(string[] args)
{
//3.使用链表
LinkList<string> seqList = new LinkList<string>();
seqList.Add("45");
seqList.Add("657");
seqList.Add("243");
Console.WriteLine(seqList.GetEle(1));
Console.WriteLine("长度:" + seqList.GetLength());
seqList.Insert("888", 2);
seqList.Delete(3);
Console.WriteLine(seqList.IsEmpty());
Console.WriteLine(seqList.Locate("45"));
Console.WriteLine("遍历结果:");
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.WriteLine(seqList[i]);
}
Console.WriteLine("清空后");
seqList.Claer();
Console.WriteLine("长度:" + seqList.GetLength());
Console.ReadKey();
}
}
}