C#算法与数据结构之线性结构

时间:2023-03-09 10:00:05
C#算法与数据结构之线性结构

线性结构是什么?

  线性结构是一种数据结构,它有一对一的关系,就像一个长对,一个接一个,特点是,除第一个元素和最后一个元素外,其它元素前后只有一个元素。

  简单示例1:

static void Main(string[] args)
{
//使用BCL中的List线性表
List<string> strList = new List<string>();
strList.Add("123");
strList.Add("456");
strList.Add("789");
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count);
strList.Remove("123"); //去除某一元素
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count); }

  输出为:456

      3  

      789

      2

线性表实现方式

    顺序表:连续排放

        单链表:

      双向链表

      循环链表

自设一个List<>:

首先创建一个接口,然后实现接口得所有方法,完程自建List功能:

  class SeqList<T>:IListDS<T>
{
private T[] data; //存储数据
private int count = ; public SeqList(int size)
{
data = new T[size];
count = ;
}
public SeqList():this() //默认构造函数10
{ }
public void Add(T item) //添加物件
{
if(count == data.Length)
{
Console.WriteLine("空间已存满");
}
else
{
data[count] = item;
count++;
}
}
//取得数据个数 public int GetLength() //得到长度
{
return count;
} public T GetEle(int index) //返回索引
{
if(index>=&&index<=count-)
{
return data[index];
}
else
{
Console.WriteLine("索引不存在");
return default(T);
}
}
public void Clear()
{
count = ;
}
public bool IsEmpty()
{
return count == ;
}
public void Insert(T item,int index)
{ for(int i=count-;i>=count;i--)
{
data[i + ] = data[i];
}
data[index] = item;
count++;
}
public T Delete(int index)
{
T temp = data[index];
for(int i=index+;i<count;i++)
{
data[i - ] = data[i];
}
count--;
return temp;
}
public T this[int index]
{
get { return GetEle(index); }
}
public int Locate(T value)
{
for(int i=;i<count;i++)
{
if(data[i].Equals(value))
{
return i;
}
}
return -;
} }

测试:

  static void Main(string[] args)
{
SeqList<string> seqList = new SeqList<string>();
seqList.Add("");
seqList.Add("");
seqList.Add("");
Console.WriteLine(seqList.GetEle());
Console.WriteLine(seqList[]);
seqList.Insert("",);
for(int i=;i<seqList.GetLength();i++)
{
Console.WriteLine(seqList[i] + ".");
}
seqList.Clear();
Console.WriteLine(seqList.GetLength());
Console.ReadKey(); }

结果:

456
456
123.
666.
456.
789.
0              结果OK,功能实现

单链表:     data     next                      示图:        C#算法与数据结构之线性结构

 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)
{
this.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; }
} }
class LinkList<T>:IListDS<T>
{
private Node<T> head;
public LinkList()
{
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==)
{
newNode.Next = head;
}
else
{
Node<T> temp = head;
for(int i=;i<index-;i++)
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp;
preNode.Next = newNode;
newNode.Next = currentNode;
}
}
public T Delete(int index)
{
T data = default(T);
if(index == )
{
data = head.Data;
head = head.Next;
}
else
{
Node<T> temp = head;
for (int i = ; i < index - ; i++)
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
data = currentNode.Data;
Node<T> nextNode = temp.Next.Next;
preNode.Next = nextNode; }
return data;
}
public int GetLength()
{
if (head == null)
return ;
Node<T> temp = head;
int count = ;
while(true)
{
if(temp.Next!=null)
{
count++;
temp = temp.Next;
}
else
{
break;
}
}
return count;
}
public void Clear()
{
head = null;
}
public bool IsEmpty()
{
return head == null;
}
public T this[int index]
{
get
{
Node<T> temp = head;
for(int i=;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 -;
}
else
{
int index = ;
while(true)
{
if(temp.Data.Equals(value))
{
return index;
}
else
{
if(temp.Next!=null)
{
temp = temp.Next;
}
else
{
break;
}
}
}
return -;
}
}
}