Digression : In the past two days, I feel that my sex has changed. I just want to study and don’t want to play. Is it possible to become a master? Ha ha ha ha
PS: The focus of this article is code code code code ! Rather than some basic concepts and classifications of data structures;
students who have never been in contact with it can watch the video of Professor Wang Zhuo at station B.
1. What is a sequence table?
-
A storage structure that stores logically adjacent data elements in physically adjacent storage units.
-
That is, the sequential storage structure of the linear table must occupy a continuous storage space .
-
What does this have to do with linked lists?
- congratulations! Blind monk, you discovered Huadian! !
- Sequential lists and linked lists are essentially linear lists , but! ! !
- Isn't the sequence table continuous in memory space ?
- The memory space of the linked list is not necessarily continuous , it can be one piece east and one piece west, this is the difference between them.
2. When to use sequence table?
- The amount of data access is relatively large, and data with infrequent addition and deletion operations .
3. The relationship between sequence table and array
- It can be understood that an array is an implementation of a sequence table , and an array is a sequence table;
- Because the high-level language helps you realize the underlying design of the array, developers can use it directly as a sequence table .
4. Summary: What is a sequence table?
- Data structures that are logically adjacent and have adjacent storage structures are called sequential tables.
5. Code implementation
namespace YoyoCode
{
//顺序表
internal class SqList<T> where T : class
{
/// <summary>
/// 数组默认最大容量
/// </summary>
public int MaxSize = 8;
/// <summary>
/// 存放元素的数组
/// </summary>
private T[] _array;
/// <summary>
/// 数组中元素的数量
/// </summary>
public int Count = 0;
public SqList()
{
_array = new T[MaxSize];
}
public SqList(int maxSize)
{
MaxSize = maxSize;
_array = new T[MaxSize];
}
/// <summary>
/// 在顺序表尾端添加元素
/// </summary>
/// <param name="value">添加的值,类型为T</param>
/// <returns>bool,是否添加成功</returns>
public bool Add(T value)
{
if (Count == MaxSize)
{
Console.WriteLine("数组已满,无法添加元素!");
return false;
}
_array[Count++] = value;
return true;
}
/// <summary>
/// 获取对应索引的元素
/// </summary>
/// <param name="idx">索引值</param>
/// <returns>T</returns>
public T? Get(int idx)
{
if (idx >= Count || idx < 0)
{
return null;
}
return _array[idx];
}
/// <summary>
/// 获取顺序表最后一个元素
/// </summary>
/// <returns>T</returns>
public T? GetLast() {
if (IsEmpty())
{
return null;
}
return _array[Count- 1];
}
/// <summary>
/// 顺序表是否为空
/// </summary>
/// <returns>bool,表是否为空</returns>
public bool IsEmpty()
{
return Count == 0;
}
/// <summary>
/// 清空顺序表(逻辑清空)
/// </summary>
public void Clear()
{
//逻辑清空(遍历_array时不用foreach,而是用Count)
Count= 0;
}
/// <summary>
/// 在对应的索引插入元素,
/// 不支持不连续的插入,例如数组总共3个元素,索引从0-2,你要在索引4插入,非法
/// </summary>
/// <param name="value"></param>
/// <param name="idx"></param>
/// <returns>bool,是否插入成功</returns>
public bool Insert(T value, int idx) {
if(Count == MaxSize)
{
Console.WriteLine("数组已满,无法添加元素!");
return false;
}
if (idx < 0 || idx > Count)
{
Console.WriteLine("插入位置有误!");
return false;
}
//从后向前插入,省一个空间
for (int i = Count; i > idx; i--)
{
_array[i] = _array[i - 1];
}
_array[idx] = value;
Count++;
return true;
}
/// <summary>
/// 删除某个索引的元素
/// </summary>
/// <param name="idx">被删除元素的索引</param>
/// <returns>bool,是否删除成功</returns>
public bool RemoveAt(int idx)
{
if (idx < 0 || idx >= Count)
{
Console.WriteLine("插入位置有误!");
return false;
}
//从前向后遍历,省一个空间
for (int i = idx; i < Count; i++)
{
_array[i] = _array[i + 1];
}
Count--;
return true;
}
/// <summary>
/// 遍历顺序表中所有的元素,并执行你希望的操作。
/// 例如:你可以通过该函数实现Contains的功能,判断表中是否存在某个元素。
/// </summary>
/// <param name="func">返回值为bool,参数为T的委托,bool为true时会打断循环</param>
public void Traverse(Func<T, bool> func)
{
for (int i = 0; i < Count; i++)
{
if (func(_array[i])) {
break;
}
}
}
}
}
Description :
- This class uses generics and must be a class type. .
- This class provides a traversal function Traverse, which can implement various traversal operations you need by passing in delegates.
6. There are already arrays, why do you need to implement them yourself?
- First of all, I am learning data structure, and I can only understand the advantages and disadvantages of the structure by typing it by hand .
- Secondly, the array provided by C# is actually not very convenient . It lacks many operations on the array, such as insertion, deletion, traversal , etc., so I implemented it myself, and I can use my own code in the future.
- Finally,,, ah. . . Um. . . It seems that hhh is gone.
7. Advantages and disadvantages of sequence table
- Advantages :
- The storage density is high , that is, in the storage structure (_array), 100% of the space is used by elements.
- Any element in the list can be accessed randomly, and the time complexity is O(1) .
- Disadvantages :
- The time complexity of insertion, deletion and search is O(n) ;
- Waste storage space and store statically (the array size is fixed and cannot be expanded)
Conclusion: This pigeon has been for several months hahaha, and a few articles related to data structures will be added later. I will write whatever the university's "Data Structure and Algorithm" textbook teaches.
You must brace yourself! ! ! Let people who look down on you look down on you! ! !