数据结构和算法——线性表

数据结构和算法——线性表

大家好,又见面了,我是全栈君。

顺序存储结构

Array

  • 引用类型(托管堆)
  • 初始化时需要设置默认值
  • 实现自己的ArrayList
     1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5  6 namespace ConsoleAppMyArrayList  7 {  8 public class MyArrayList  9  {  10 //容量  11 private const int _defaultCapacity = 4;  12 //存放数组元素  13 private object[] _items;  14 //数组大小  15 private int _size;  16 //元素个数为0的数组状态  17 private static readonly object[] emptyArray = new object[0];  18  19 public MyArrayList()  20  {  21 this._items = emptyArray;  22  }  23  24 public MyArrayList( int capacity)  25  {  26 if (capacity<0)  27  {  28 throw new ArgumentOutOfRangeException(nameof(capacity),"ArrayList的容量不可为负数!");  29  }  30 this._items = new object[capacity];  31  }  32  33 //索引器  34 public virtual object this[int index]  35  {  36 get  37  {  38 if (index<0||index>=this._size)  39  {  40 throw new ArgumentOutOfRangeException(nameof(index),"索引超出范围!");  41  }  42 return this._items[index];  43  }  44  45 set  46  {  47 if (index < 0 || index >= this._size)  48  {  49 throw new ArgumentOutOfRangeException(nameof(index), "索引超出范围!");  50  }  51 this._items[index] = value;  52  }  53  54  }  55  56 //当前数组元素个数  57 public virtual int Count  58  {  59 get { return this._size ;}  60  }  61  62 //数组的容量  63 public virtual int Capacity  64  {  65 get { return this._items.Length; }  66 set  67  {  68 if (value!=this._items.Length)  69  {  70 if (value<this._size)  71  {  72 throw new ArgumentOutOfRangeException(nameof(value),"容量太小");  73  }  74 if (value > 0)  75 { //开辟新内存空间存储元素  76 object[] dest = new object[value];  77 if (this._size > 0)  78 { //搬动元素  79 Array.Copy(this._items, 0, dest, 0, this._size);  80  }  81 this._items = dest;  82  }  83 else//数组最小的空间为4  84  {  85 this._items=new object[_defaultCapacity];  86  }  87  }  88  }  89  }  90  91 //元素的添加  92 public virtual int Add(object value)  93 { //当空间已满  94 if (this._size==this._items.Length)  95  {  96 this.EnsureCapacity(this._size+1);  97  }  98 this._items[this._size] = value;  99 return this._size++; 100  } 101 102 //扩容 103 private void EnsureCapacity(int p) 104  { 105 if (this._items.Length<p) 106 { //空间加倍 107 int num = (this._items.Length == 0) ? _defaultCapacity : (this._items.Length * 2); 108 if (num < p) 109  { 110 num = p; 111  } 112 this.Capacity = num; 113  } 114  } 115 116 //指定位置插入元素 117 public virtual void Insert( int index,object value) 118  { 119 if (index<0||index>this._size) 120  { 121 throw new ArgumentOutOfRangeException(nameof(index),"索引超出范围!"); 122  } 123 if (this._size==this._items.Length) 124  { 125 this.EnsureCapacity(this._size + 1); 126  } 127 if (index<this._size) 128  { 129 Array.Copy(this._items, index, this._items, index + 1, this._size - index); 130  } 131 this._items[index] = value; 132 this._size++; 133  } 134 135 //移除指定索引的元素 136 public virtual void Remove(int index) 137  { 138 if (index < 0 || index > this._size) 139  { 140 throw new ArgumentOutOfRangeException(nameof(index), "索引超出范围!"); 141  } 142 this._size--; 143 if (index<this._size) 144  { 145 Array.Copy(this._items,index+1,this._items,index,this._size-index); 146  } 147 this._items[this._size]=null; 148  } 149 150 //裁剪空间 151 public virtual void TrimToSize() 152  { 153 this.Capacity = this._size; 154  } 155  } 156 }

     

链式存储结构

链表

  1. 单向链表

  2. 循环链表

    • 实现自己的循环链表
       1 using System;  2  3 namespace ConsoleAppLinked  4 {  5 class CirculLinkedList  6  {  7 //元素个数  8 private int count;  9  10 //尾指针  11 private Node tail;  12  13 //当前节点的前节点  14 private Node CurrPrev;  15  16  17 //增加元素  18 public void Add(object value)  19  {  20 Node newNode = new Node(value);  21 if (tail==null)  22 { //链表为空  23 tail = newNode;  24 tail.next = newNode;  25 CurrPrev = newNode;  26  }  27 else  28 { //链表不为空,把元素增加到链表结尾  29 newNode.next = tail.next;  30 tail.next = newNode;  31  32 if (CurrPrev==tail)  33  {  34 CurrPrev = newNode;  35  }  36 tail = newNode;  37  }  38 count++;  39  }  40  41 //删除当前元素  42 public void RemoveCurrNode()  43  {  44 //为null代表为空表  45 if (tail == null)  46  {  47 throw new NullReferenceException("集合中没有任何元素!");  48  }  49 else if (count==1)  50  {  51 tail = null;  52 CurrPrev = null;  53  }  54 else  55  {  56 if (CurrPrev.next==tail)  57  {  58 tail = CurrPrev;  59  }  60 CurrPrev.next = CurrPrev.next.next;  61  }  62 count--;  63  }  64  65 //当前节点移动步数  66 public void Move(int step)  67  {  68 if (step<0)  69  {  70 throw new ArgumentOutOfRangeException("step", "移动步数不可为负数!");  71  }  72 if (tail==null)  73  {  74 throw new NullReferenceException("链表为空!");  75  }  76 for (int i = 0; i < step; i++)  77  {  78 CurrPrev = CurrPrev.next;  79  }  80  }  81  82 //获得当前节点  83 public object Current  84  {  85 get { return CurrPrev.next.item ;}  86  }  87  88 public override string ToString()  89  {  90 if (tail==null)  91  {  92 return string.Empty;  93  }  94 string s = "";  95 Node temp = tail.next;  96 for (int i = 0; i < count; i++)  97  {  98 s += temp.ToString() + " ";  99 temp = temp.next; 100  } 101 return s; 102  } 103 104 105 public int Count 106  { 107 get { return count ;} 108  } 109 110 private class Node 111  { 112 public Node(object value) 113  { 114 item = value; 115  } 116 //放置数据 117 public object item; 118 public CirculLinkedList.Node next; 119 public override string ToString() 120  { 121 return item.ToString(); 122  } 123  } 124  } 125 }

       

  3. 双向链表

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108580.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • vue父组件调用子组件方法返回值_vue子组件修改父组件值

    vue父组件调用子组件方法返回值_vue子组件修改父组件值子组件调用父组件方法,父组件执行完后,进行回调,代码如下:子组件this.$emit(‘change’,this.dataList,(loading)=>{this.loading=loading})父组件@change=”onChange”………………………………..

  • GSLB调度服务原理

    GSLB调度服务原理GSLB,全局负载均衡(GlobalServerLoadBalancing),主要的目的是在整个网络范围内将用户的请求定向到最近的节点(或者区域)。是对物理集群的负载均衡,不止是简单的流量均匀分配,还会根据应用场景的不同来制定不同的策略。本文将讨论GSLB的几种实现,并介绍调度服务实现的大体情况。

  • 优化MySQL前缀索引[通俗易懂]

    优化MySQL前缀索引[通俗易懂]文章介绍如何如何创建MySQL前缀索引,以及计算索引的选择性,明确使用前置索引的场景。

  • react中添加debounce 实现[通俗易懂]

    react中添加debounce 实现[通俗易懂]react中添加debounce实现handelChange(e){//输入框修改的时候执行的方法 e.persist()//react默认会清楚所有的默认属性,所以需要添加这段,保留参数的属性 debounce(()=>{ console.log(e) },500)() }<inputref={ev=>this.moneyInp…

  • 压缩文件解压密码破解之fcrackzip

    压缩文件解压密码破解之fcrackzip写在前面:网上对fcrackzip相关知识很多,我就不多哔哔了,我比较喜欢直接掏出重点少废话,写的花留呼哨一坨官方术语各种夸、没必要大家都挺忙的。工具简介:fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具“短小精悍”。使用范围:Linux、Macosx关于安装:1、MacOSbrewinstallfcrackzip2、Ubuntuapt-getinstallfcrackzip3、CentOS这个比较特殊,yum找不到这个包,那就下.

  • 详解scheduleAtFixedRate与scheduleWithFixedDelay原理

    详解scheduleAtFixedRate与scheduleWithFixedDelay原理前言前几天,肥佬分享了一篇关于定时器的文章你真的会使用定时器吗?,从使用角度为我们详细地说明了定时器的用法,包括fixedDelay、fixedRate,为什么会有这样的区别呢?下面我们从源码角度分析下二者的区别与底层原理。jdk定时器这里不再哆嗦延迟队列、线程池的知识了,请移步下面的链接延迟队列原理,http://cmsblogs.com/?p=2448线程池原理,http://…

    2022年10月24日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号