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

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

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

顺序存储结构

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. 双向链表

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Springboot启动报错[ main] o.s.boot.SpringApplication: Application run failed(佷有可能是版本问题)

    Springboot启动报错[ main] o.s.boot.SpringApplication: Application run failed(佷有可能是版本问题)前言:本人小白一枚,最近在自学JAVA时遇到了一个小问题,在网上求解无果后,自己找到了原因,这里跟大家分享一下。开发环境:Win10;IntelliJIDEA2021.3.2版本信息:Java\jdk-17.0.2;apache-maven-3.8.4-bin;springboot2.3.4编程目的:本人之前对JAVA一窍不通,最近在自学JAVA时想要用JAVA,Springboot和maven搭建一个最基础的helloworld程序。报错信息:ERROR后面显示“o.s.boot.

    2022年9月8日
    0
  • aic准则和bic准则_用户故事准则

    aic准则和bic准则_用户故事准则aic准则和bic准则免责声明:这篇文章摘自内部Codurance文档,该文档用于帮助我们的学徒学习我们的工作方式。我们都知道每个项目都是不同的,而且我们绝不能在任何地方应用完全相同的技术和实践。但是,以下文字不仅作为基础,而且还是我们所有人涉及用户故事时的指南。有很多关于用户故事的好书和帖子。这篇文章绝不是该领域所有良好实践的总结。用户故事是收集需求,就需要完成的事情达成共识…

    2022年5月24日
    42
  • 最低公共祖先java_洛谷是啥

    最低公共祖先java_洛谷是啥原题链接题目描述如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先。输入格式第一行包含三个正整数 N,M,SN,M,S,分别表示树的结点个数、询问的个数和树根结点的序号。接下来 N-1N−1 行每行包含两个正整数 x, yx,y,表示 xx 结点和 yy 结点之间有一条直接连接的边(数据保证可以构成树)。接下来 MM 行每行包含两个正整数 a, ba,b,表示询问 aa 结点和 bb 结点的最近公共祖先。输出格式输出包含 MM 行,每行包含一个正整数,依次为每一个询问的结果。输入

    2022年8月9日
    3
  • 飞猪双11挑战_天猫双十一晚会极限挑战

    飞猪双11挑战_天猫双十一晚会极限挑战摘要: EagleEye作为阿里集团老牌的链路跟踪系统,其自身业务虽不在交易链路上,但却监控着全集团的链路状态,特别是在中间件的远程调用上,覆盖了集团绝大部分的场景,在问题排查和定位上发挥着巨大的作用,保障了各个系统的稳定性,为整个技术团队打赢这场战役保驾护航。作者:王华锋(水彧)背景双十一一直是阿里巴巴集团每年要打的一场大战役。要打赢这场战役,技术上,不仅仅是几个应用

    2022年8月16日
    3
  • 深度学习检测小目标常用方法

    深度学习检测小目标常用方法

    2020年11月14日
    179
  • 微信每日早安推送「建议收藏」

    微信每日早安推送「建议收藏」七夕到啦,做一个程序员给女朋友的浪漫礼物吧。微信公众号推送。每日早安推送

    2022年9月29日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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