数据结构入门(二)固定容量数组

简介:

注:本文仅供菜鸟和初级入门者参考,欢迎高手指导,切勿拍砖,兄弟身子骨单薄,承受不起。博客园原创文章,转载请注明作者和出处!

下面的代码包装了一个静态数组,它实现了上一节中 IList<T> 接口的全部功能;为了具有排序功能,同时实现了 ISortable<T> 接口。关于 IList<T> 接口和 ISortable<T> 接口请参见上一部分 数据结构入门(一)基本接口的设计。类名起为 FixedList<T> 表明数组的大小在对象构造后就不可改变,可自动扩展大小的数组会在下一节实现。

FixedList<T> 只具有一些最基本的功能。代码都有注释,十分简单。

在客户端测试 FixedList<T> 的时候,为数组设置了一个冒泡排序算法 ,排序的具体实现将在后面给出。

Arrays.cs 文件:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
None.gif
namespace  My
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
namespace Collections
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 固定容量数组
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <typeparam name="T">实现了 Equals() 方法的数据类型</typeparam>

InBlock.gif        public class FixedList<T> : IList<T>, ISortable<T>
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 被包装的数组
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            private T[] elements;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 数组的最大容量
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            private int capacity;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 当前数组存储的数据个数
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            private int count;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 选择的排序类,运用策略模式,选择不同的排序类会调用不同的排序方法
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            private ISort<T> sort;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 验证容量是否有效,无效抛出异常
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="capacity">数组的最大容量,应该大于等于零</param>

InBlock.gif            private  void CapacityCheck(int capacity)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (capacity < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string str = String.Format("无效的数组容量: {0}", capacity);
InBlock.gif                    
throw new CollectionsException(str);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 检查数组是否已满,已满则抛出异常
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="capacity">数组的最大容量,应该大于等于零</param>
ExpandedSubBlockEnd.gif            
/// <param name="count">当前数组存储的数据个数,范围从 0 到 capacity - 1</param>

InBlock.gif            private void FullCheck(int capacity, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (count == capacity)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string str = String.Format("数组已满,无法继续添加数据。当前数组容量为 {0} !", capacity);
InBlock.gif                    
throw new CollectionsException(str);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 检查索引是否有效,无效则抛出异常
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="index">索引,范围从 0 到 count - 1</param>
ExpandedSubBlockEnd.gif            
/// <param name="count">当前数组存储的数据个数,范围从 0 到 capacity - 1</param>

InBlock.gif            private void IndexCheck(int index, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ((index > count - 1|| (index < 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string str = String.Format("访问越界:当前数组有 {0} 个元素,试图访问索引为号 {1} 的元素!", count, index);
InBlock.gif                    
throw new CollectionsException(str);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 构造函数,申请数组空间
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="capacity">设定数组的最大容量</param>

InBlock.gif            public FixedList(int capacity)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CapacityCheck(capacity);
InBlock.gif                
this.capacity = capacity;
InBlock.gif                elements 
= new T[capacity];
InBlock.gif                count 
= 0;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 返回数组容量
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public int Capacity
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn capacity; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
///返回当前数组内元素个数 
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public int Count
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn count; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 判断数组是否为空
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public bool IsEmpty
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn (count == 0); }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 判断数组是否已满
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public bool IsFull
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn (count == capacity); }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 设定一个排序类,数组将会用此类包装的排序方法进行排序
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public ISort<T> Sort
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis.sort = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 发出排序动作
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public void DoSort()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.sort.Do(this);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 遍历数组,将所有元素的值设为默认值
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public void Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    elements[i] 
= default(T);
ExpandedSubBlockEnd.gif                }

InBlock.gif                count 
= 0;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 将数据添加到数组的尾部,数组已满则抛出异常
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="data">需要添加的数据</param>

InBlock.gif            public void Add(T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FullCheck(capacity, count);
InBlock.gif                elements[count] 
= data;
InBlock.gif                count
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 在某一位置插入数据,并向后移动其他数据,数组已满则抛出异常
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="index">数据插入的位置,无效则抛出异常</param>
ExpandedSubBlockEnd.gif            
/// <param name="data">需要插入的数据</param>

InBlock.gif            public void Insert(int index, T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FullCheck(capacity, count);
InBlock.gif                IndexCheck(index, count);
InBlock.gif                System.Array.Copy(elements, index, elements, index 
+ 1, count - index);
InBlock.gif                elements[index] 
= data;
InBlock.gif                count
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 从位置 fromIndex 开始查找第一个符合的数据,并返回其的位置 
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="data">要查找的数据</param>
InBlock.gif            
/// <param name="fromIndex">开始查找的位置</param>
ExpandedSubBlockEnd.gif            
/// <returns>找到则返回数据所在位置,没找到则返回 -1</returns>

InBlock.gif            private int IndexOf(T data, int fromIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IndexCheck(fromIndex, count);
InBlock.gif                
for (int i = fromIndex; i < count; i++)
InBlock.gif                    
if (data.Equals(elements[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return i;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                
return -1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 从头开始查找,直到找到第一个符合的数据,并返回其的位置 
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="data">要查找的数据</param>
ExpandedSubBlockEnd.gif            
/// <returns>找到则返回数据所在位置,没找到则返回 -1</returns>

InBlock.gif            public int IndexOf(T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return IndexOf(data, 0);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 判断一个数据是否在数组中存在
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="data">要查找的数据</param>
ExpandedSubBlockEnd.gif            
/// <returns>找到则返回 true ,没找到则返回 false</returns>

InBlock.gif            public bool Find(T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return IndexOf(data) >= 0;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 返回和设置某一位置的数据
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="index">位置</param>
ExpandedSubBlockEnd.gif            
/// <returns>数据元素</returns>

InBlock.gif            public T this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IndexCheck(index, count);
InBlock.gif                    
return elements[index];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IndexCheck(index, count);
InBlock.gif                    elements[index] 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 删除某一位置的数据,并前移动其后数据,以填充留出的空位
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="index">要删除数据的位置</param>

InBlock.gif            public void RemoveAt(int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IndexCheck(index, count);
InBlock.gif                
if (index < count - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    System.Array.Copy(elements, index 
+ 1, elements, index, count - index - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// 如果要删除的数据是最后一项,则不需要移动,直接 count-- 丢弃他就行了
InBlock.gif
                count--;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 查找第一个符合的数据并删除,并向前移动其后数据,以填充留出的空位 
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="data">要删除的数据</param>

InBlock.gif            public void Remove(T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int index = IndexOf(data);
InBlock.gif                
if (index >= 0) RemoveAt(index);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 删除所有符合的数据,并前移动其后数据,以填充留出的空位
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="data">要删除的数据</param>

InBlock.gif            public void RemoveAll(T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int index = IndexOf(data, 0);
InBlock.gif                
while (index >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    RemoveAt(index);
InBlock.gif                    index 
= IndexOf(data, index);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 枚举器
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <returns></returns>

InBlock.gif            public IEnumerator<T> GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < count; i++)
InBlock.gif                    yield 
return elements[i];
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("未实现的方法!");
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

客户端测试文件:Client.cs

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  My.Collections;
None.gif
None.gif
namespace  Client
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FixedList
<int> arr = new FixedList<int>(18);
InBlock.gif
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
16);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
27);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
99);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
16);
InBlock.gif                arr.Add(
36);
InBlock.gif                arr.Add(
999);
InBlock.gif                arr.Add(
1);
InBlock.gif
InBlock.gif                Console.Write(
"显示当前数据:");
InBlock.gif                
foreach (int i in arr)
InBlock.gif                    Console.Write(i 
+ "  ");
InBlock.gif                Console.WriteLine(
"\n");
InBlock.gif
InBlock.gif                Console.WriteLine(
"数组容量:" + arr.Capacity);
InBlock.gif                Console.WriteLine(
"元素个数:" + arr.Count);
InBlock.gif                Console.WriteLine(
"查找数据27:" + arr.Find(27));
InBlock.gif                Console.WriteLine(
"查找数据72:" + arr.Find(72));
InBlock.gif                Console.WriteLine(
"定位数据99:" + arr.IndexOf(99));
InBlock.gif                Console.WriteLine(
"定位数据123:" + arr.IndexOf(123));
InBlock.gif
InBlock.gif                
InBlock.gif
InBlock.gif                Console.WriteLine(
"删除所有36dot.gif");
InBlock.gif                arr.RemoveAll(
36);
InBlock.gif
InBlock.gif                Console.Write(
"显示当前数据:");
InBlock.gif                
foreach (int i in arr)
InBlock.gif                    Console.Write(i 
+ "  ");
InBlock.gif                Console.WriteLine(
"\n");
InBlock.gif
InBlock.gif                Console.WriteLine(
"删除最后一个数据dot.gif");
InBlock.gif                arr.RemoveAt(arr.Count 
- 1);
InBlock.gif
InBlock.gif                Console.Write(
"显示当前数据:");
InBlock.gif                
foreach (int i in arr)
InBlock.gif                    Console.Write(i 
+ "  ");
InBlock.gif                Console.WriteLine(
"\n");
InBlock.gif
InBlock.gif                Console.WriteLine(
"数组容量:" + arr.Capacity);
InBlock.gif                Console.WriteLine(
"元素个数:" + arr.Count);
InBlock.gif
InBlock.gif
InBlock.gif                
//注意:这里用到了一个 BubleSort<T> 的排序类,
InBlock.gif                
//它仅仅是一个包装了冒泡排序算法的类,实现了 ISort<T> 接口,后面将会介绍它
InBlock.gif
                Console.WriteLine("用冒泡法排序dot.gif");
InBlock.gif                arr.Sort
=new BubbleSort<int>();
InBlock.gif                arr.DoSort();
InBlock.gif
InBlock.gif                Console.Write(
"显示当前数据:");
InBlock.gif                
foreach (int i in arr)
InBlock.gif                    Console.Write(i 
+ "  ");
InBlock.gif                Console.WriteLine(
"\n");
InBlock.gif
InBlock.gif                Console.WriteLine(
"当前数组是否为空::" + arr.IsEmpty);
InBlock.gif                Console.WriteLine(
"清空数组dot.gif");
InBlock.gif                arr.Clear();
InBlock.gif                Console.WriteLine(
"当前数组是否为空::" + arr.IsEmpty);
InBlock.gif                Console.Write(
"显示当前数据:");
InBlock.gif                
foreach (int i in arr)
InBlock.gif                    Console.Write(i 
+ "  ");
InBlock.gif                Console.WriteLine(
"\n");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                 Console.WriteLine(e.Message );
ExpandedSubBlockEnd.gif            }

InBlock.gif                    
InBlock.gif
InBlock.gif            Console.Read();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

运行效果如下:




本文转自左洸博客园博客,原文链接:http://www.cnblogs.com/myqiao/archive/2006/01/21/321141.html,如需转载请自行联系原作者

目录
相关文章
|
1月前
|
存储 算法 数据处理
数据结构从入门到精通——栈
栈,作为一种后进先出(LIFO)的数据结构,在计算机科学中扮演着重要的角色。它的特性使得它在处理函数调用、括号匹配、表达式求值等问题时具有得天独厚的优势。然而,如果我们跳出传统思维的束缚,会发现栈的用途远不止于此。
58 0
|
1月前
|
存储 缓存 算法
数据结构从入门到精通——链表
链表是一种常见的数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。链表的一个显著特点是,它不需要在内存中连续存储,因此可以高效地插入和删除节点。这种灵活性使得链表在许多应用中成为理想的选择,尤其是在需要动态调整数据结构大小的场景中。
72 0
|
1月前
|
存储 消息中间件 算法
数据结构从入门到精通——顺序表
顺序表是一种常见的线性数据结构,它使用一段连续的存储单元依次存储数据元素。这种数据结构的特点是逻辑上相邻的元素在物理存储位置上也相邻,因此可以快速地访问表中的任意元素。 顺序表的实现通常依赖于数组,数组是一种静态的数据结构,一旦创建,其大小就是固定的。这意味着在顺序表中插入或删除元素可能会导致空间的浪费或不足。例如,如果在一个已经满了的顺序表中插入一个新元素,就需要重新分配更大的数组空间,并将原有元素复制到新数组中,这是一个相对耗时的操作。
52 0
|
1月前
|
机器学习/深度学习 存储 缓存
数据结构从入门到精通——算法的时间复杂度和空间复杂度
算法的时间复杂度和空间复杂度是评估算法性能的两个重要指标。时间复杂度主要关注算法执行过程中所需的时间随输入规模的变化情况,而空间复杂度则关注算法执行过程中所需的最大存储空间或内存空间。
71 0
|
1月前
【数据结构】数组、双链表代码实现
【数据结构】数组、双链表代码实现
|
2月前
|
算法 测试技术 C++
【数据结构】【双堆】【滑动窗口】3013. 将数组分成最小总代价的子数组 II
【数据结构】【双堆】【滑动窗口】3013. 将数组分成最小总代价的子数组 II
|
28天前
|
存储 NoSQL Java
Redis 数据结构操作入门
Redis 数据结构操作入门
15 0
|
1月前
|
存储 算法 Serverless
【软件设计师备考 专题 】数据结构深度解析:从数组到图
【软件设计师备考 专题 】数据结构深度解析:从数组到图
56 0
|
1月前
|
机器学习/深度学习 存储 Java
揭秘数组:数据结构的基石与代码实践解析
揭秘数组:数据结构的基石与代码实践解析
9 0
|
1月前
|
机器学习/深度学习 存储 缓存
数据结构从入门到精通——队列
队列是一种特殊的线性数据结构,遵循先入先出(FIFO)的原则。它只允许在队列的末尾添加元素(称为入队操作),并从队列的开头移除元素(称为出队操作)。队列在多种应用中发挥着重要作用,如计算机系统的任务调度、打印机作业管理以及多线程编程中的线程同步等。
49 0