1:顺序表的优缺点:List
优点:顺序表示用地址连续的存储单元顺序存储线性表中的各个元素,逻辑上相领的数据元素在物理位置上也相领,因此,在顺序表中查找任何一个位置上的数据元素非常方便;
缺点:在顺序表中插入及删除时需要通过移动数据元素来实现,影响了运行的效率;
2:线性表的另外一种存储结构---链式存储LinkList
优点:对链表进行插入和删除时不需要去移动数据元素,效率比较高
缺点:失去了对线性表可随机存储的优点
顺序表实现接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
interface IList<T>
{
int GetLength();
void Clear();
bool IsEmpty();
void Add(T item);
void Insert(T item, int index);
void Delete(int index);
T this[int index] { get; }
T GetEle(int index);
int Locate(T value);
}
}
顺序表实现方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
class MyList<T>:IList<T>
{
public T[] data;
private int count;
public MyList(int size) {
data = new T[size];
count = 0;
}
public MyList():this(10)
{
}
public int GetLength()
{
return count;
}
public void Clear()
{
count = 0;
}
public bool IsEmpty()
{
return count == 0;
}
public void Add(T item)
{
if (count == data.Length)
{
Console.WriteLine("当前顺序表已存满,不允许在存入");
}
else {
data[count] = item;
count++;
}
}
public void Insert(T item, int index)
{
if (index >= 0 && index <= count - 1)
{
for (int i = count - 1; i >= index; i--)
{
if (i + 1 >= data.Length)
{
}
else
{
data[i + 1] = data[i];
}
}
data[index] = item;
if (count == data.Length)
{
Console.WriteLine("存储满了 替换元素");
}
else {
count++;
}
}
else {
Console.WriteLine("下标越界了------------>");
}
}
public void Delete(int index)
{
if (index >= 0 && index <= count - 1)
{
for (int i = index; i < count; i++)
{
if (i + 1 >= count)
{
}
else
{
data[i] = data[i + 1];
}
}
count--;
}
else {
Console.WriteLine("下标越界了------------>");
}
}
public T this[int index]
{
get
{
if (index >= 0 && index <= count)
{
return data[index];
}
else
{
return default(T);
}
}
}
public T GetEle(int index)
{
if (index >= 0 && index <= count -1 )
{
return data[index];
}
else {
return default(T);
}
}
public int Locate(T value)
{
for (int i = 0; i < count; i++)
{
if (data[i].Equals(value))
{
return i;
}
}
return -1;
}
}
}
单链表实现接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 线性表
{
/// <summary>
/// 单链表的节点
/// </summary>
/// <typeparam name="T"></typeparam>
class Node<T>
{
private T data;//存储数据
private Node<T> next;//指针 用来指向下一个元素
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 Node() {
data = default(T);
next = null;
}
public T Data {
get { return data; }
set { data = value; }
}
public Node<T> Next {
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>:IList<T>
{
private Node<T> head;//存储一个头节点
public LinkList()
{
head = null;
}
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 Clear()
{
head = null;//GC 会自动回收其他指针的内存
}
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.Next;
head.Next = 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 void Delete(int index)
{
if (index == 0)
{
Node<T> temp = head.Next;
head = temp;
head.Next = temp.Next;
}
else
{
Node<T> temp = head;
for (int i = 0; i <= index - 1; i++)
{
temp = temp.Next;
}
Node<T> preTemp = temp;
Node<T> curTemp = preTemp.Next;
Node<T> nextTemp = curTemp.Next;
preTemp.Next = nextTemp;
}
}
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;
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;
index++;
}
else {
break;
}
}
}
}
return -1;
}
}
}