using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
interface IlistDS<T> //接口
{
int GetLength();
T Delete(int index);
T GetEle(int index);
int Locate(T value);
bool IsEmpty();
void Insert(T item, int index);
void Add(T item);
void Clear();
T this[int index] { get; } //索引器 取值
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
class SeqList<T>:IlistDS<T> //顺序表(线性表)
{
int Count;//表示储存了多少个数据
private T[] data;//用来存储数据
public SeqList(int size) //size为最大容量,构造方法初始化数组
{
data = new T[size];
Count = 0;
}
public SeqList():this(10) //默认构造函数 容量为10
{
}
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 T GetEle(int index)
{
if (index >= 0 && index < Count)
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);
}
}
public int Locate(T value)
{
for (int i = 0; i < Count; i++)
{
if (data[i].Equals(value))
return i;
}
return -1;
}
public bool IsEmpty()
{
return Count == 0;
}
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 void Add(T item)
{
if (Count == data.Length)
{
Console.WriteLine("数组已满无法添加");
}
else
{
data[Count] = item;
Count++;
}
}
public void Clear()
{
Count = 0;
}
public T this[int index]
{
get { return GetEle(index); }
}
public int GetLength()
{
return Count;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
class Node<T> //实现单链表节点类
{
private T data;//用于存储数据
private Node<T> next;//指针 用来指向下一个数据
public Node(T value)
{
this.data = value;
next = null;
}
public Node(Node<T> next)
{
this.next = next;
}
public Node(Node<T> next,T value)
{
this.data = value;
this.next = next;
}
public Node()
{
data = default(T);
next = null;
}
public T GetData
{
get{return data;}
set{data = value;}
}
public Node<T> GetNext
{
get { return next; }
set { next = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
class LinkList<T>:IlistDS<T> // 实现链表
{
public Node<T> head;//头节点
public LinkList()
{
head = null;
}
public int GetLength()
{
int Count = 1;
if (head == null)
{
return 0;
}
else
{
Node<T> temp = head;
while (true)
{
if(temp.GetNext!=null)
{
Count++;
temp = temp.GetNext;
}
else
{
break;
}
}
return Count;
}
}
public T Delete(int index)
{
T data = default(T);
if (index == 0)
{
data = head.GetData;
head = head.GetNext;
}
else
{
Node<T> temp = head;
for (int i = 1; i <= index - 1; i++)
{
temp = temp.GetNext;
}
Node<T> preNode = temp;
Node<T> newNode = temp.GetNext;
data = newNode.GetData;
Node<T> curNode = temp.GetNext.GetNext;
preNode.GetNext = curNode;
}
return data;
}
public T GetEle(int index)
{
return this[index];
}
public int Locate(T value)
{
Node<T> temp = head;
int count = 0;
if(temp == null)
{
return -1;
}
else
{
while (true)
{
if (temp.GetData.Equals(value))
{
return count;
}
else
{
if (temp.GetNext != null)
{
count++;
temp = temp.GetNext;
}
else
{
break;
}
}
}
return -1;
}
}
public bool IsEmpty()
{
return head == null;
}
public void Insert(T item, int index)
{
Node<T> newNode = new Node<T>(item);
if (index == 0)
{
newNode.GetNext = head;
head = newNode;
}
else
{
Node<T> temp = head;
for (int i = 1; i <= index-1; i++)
{
temp = temp.GetNext;
}
Node<T> preNode = temp;
Node<T> curNode = temp.GetNext;
preNode.GetNext = newNode;
newNode.GetNext = curNode;
}
}
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.GetNext != null)
{
temp = temp.GetNext;
}
else
{
break;
}
}
temp.GetNext = newNode;
}
}
public void Clear()
{
head = null;
}
public T this[int index]
{
get
{
Node<T> temp = head;
for (int i = 0; i <index ; i++)
{
temp = temp.GetNext;
}
return temp.GetData;
}
}
}
}