线性链表

线性链表
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace ConsoleApp7
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             LinearLIst line = new LinearLIst(10);
 14 
 15             line.Add(0,12);
 16             line.Add(1,20);
 17             line.Add(2, 4);
 18             line.Add(3, 9);
 19             Console.WriteLine("数组大小size: {0}",line.Size());
 20             Console.WriteLine("数组长度length: {0}", line.sList.Length);
 21             Console.WriteLine(line.Get(0));
 22             Console.WriteLine(line.Get(1));
 23             Console.WriteLine(line.Get(2));
 24             Console.WriteLine(line.Get(3));
 25             Console.Read();
 26         }
 27     }
 28     interface ILinearLIst
 29     {
 30         /// <summary>
 31         /// 是否为空
 32         /// </summary>
 33         /// <returns></returns>
 34         bool IsEmpty();
 35         /// <summary>
 36         /// 大小
 37         /// </summary>
 38         /// <returns></returns>
 39         int Size();
 40         /// <summary>
 41         /// 获取元素
 42         /// </summary>
 43         /// <param name="index">待获取的索引</param>
 44         /// <returns></returns>
 45         object Get(int index);
 46         /// <summary>
 47         /// 设置元素
 48         /// </summary>
 49         /// <param name="index">待获取的索引</param>
 50         /// <param name="elem">待设置的元素</param>
 51         /// <returns></returns>
 52         object Set(int index, object elem);
 53         /// <summary>
 54         /// 添加元素
 55         /// </summary>
 56         /// <param name="index">待添加的位置</param>
 57         /// <param name="elem">待添加的元素</param>
 58         /// <returns></returns>
 59         bool Add(int index, object elem);
 60         /// <summary>
 61         /// 移除元素
 62         /// </summary>
 63         /// <param name="index">待移除的索引</param>
 64         /// <returns></returns>
 65         Object Remove(int index);
 66         /// <summary>
 67         /// 清除所有元素
 68         /// </summary>
 69         void Clear();
 70     }
 71 
 72     internal class LinearLIst : ILinearLIst
 73     {
 74         public object[] sList = null;
 75         private const int defaultCapacity = 10;       // 数组容量
 76         private int size=0;         //数组实际存储大小
 77         public LinearLIst(int capacity)
 78         {
 79             if(capacity <= 0)
 80             {
 81                 sList = new object[defaultCapacity];
 82             }
 83             else
 84             {
 85                 sList = new object[capacity];
 86             }
 87             
 88             this.size = 0;
 89         }
 90         public bool Add(int index, object elem)
 91         {
 92 
 93             //索引大于数组的长度,还是数组的容量
 94             //元素后移,插入
 95             if(index < 0 || index > size)
 96             {
 97                 throw new Exception("插入位置有误");
 98             }
 99             //从新分配空间
100             if(size == sList.Length)
101             {
102                 object[] temp = sList;
103                 sList = new object[size * 2];
104                 for (int i = 0; i < temp.Length; i++)
105                 {
106                     sList[i] = temp[i];
107                 }
108             }
109             //分析:1,2,3,4,5    长度为5的数组 , size = 5,  待插入数值为10 插入索引index = 2  插入后的值为   1,2,10,3,4,5     
110             //首先移动index = size - 1 最后一位,以此从最后一位到index这个位置 
111             for (int j = size - 1; j >= index; j--)
112             {
113                 sList[j + 1] = sList[j];
114             }
115             sList[index] = elem;
116             size++;
117             return true;
118 
119         }
120 
121         public void Clear()
122         {
123             for (int i = 0; i < sList.Length; i++)
124             {
125                 sList[i] = null;
126             }
127             size = 0;
128         }
129 
130         public object Get(int index)
131         {
132             ThrowExe(index);
133             return sList[index];
134         }
135 
136         
137         public bool IsEmpty()
138         {
139             return size == 0;
140         }
141 
142         public object Remove(int index)
143         {
144             //1,2,3,4,5    index = 2   移除值为3的元素,后面的元素 4,5前移
145             ThrowExe(index);
146             object removeNumber = sList[index];
147 
148             for (int i = index; i < sList.Length - 1; i++)
149             {
150                 sList[i] = sList[i + 1];
151             }
152             sList[size - 1] = null;
153             size--;
154             return removeNumber;
155         }
156 
157         public object Set(int index, object elem)
158         {
159             ThrowExe(index);
160             
161             var old = sList[index];
162             sList[index] = elem;
163             return old;
164         }
165 
166         public int Size()
167         {
168             return size;
169         }
170 
171         public bool RangeCheck(int index)
172         {
173             if(index < 0 || index >= size)
174             {
175                 return false;
176             }
177             return true;
178         }
179 
180         private void ThrowExe(int index)
181         {
182             if (!RangeCheck(index))
183             {
184                 throw new Exception("参数不合法");
185             }
186         }
187 
188     }
189 }
View Code

 

上一篇:windows10从零开始安装ensp,启动设备失败,错误代码:40


下一篇:MacBook VirtualBox 安装centos7