首页 > 编程语言 >C#中的线性表

C#中的线性表

时间:2024-07-21 19:27:37浏览次数:8  
标签:index 线性表 C# list System int using public

什么是线性表

线性表是最简单、最基本、最常用的数据结构。线性表是线性结构的抽象(Abstract),线性结构的特点是结构中的数据元素之间存在一对一的线性关系。这种一对一的关系指的是数据元素之间的位置关系,即:(1)除第一个位置的数据元素外,其它数据元素位置的前面都只有一个数据元素;(2)除最后一个位置的数据元素外,其它数据元素位置的后面都只有一个元素。也就是说,数据元素是一个接一个的排列。因此,可以把线性表想象为一种数据元素序列的数据结构。

线性表就是位置有先后关系,一个接着一个排列的数据结构。

C#提供了一个非泛型接口IList 接口中的项是object,实现了lList解扣子的类有ArrayList,ListDictionary,StringCollection,StringDictionary.

c#2.0提供了泛型的IList<T>接

c#1.1 提供了一个非泛型接lList接接,实现了List<T>接口的类有List<T>

使用List<>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //1.使用BCL中的List线性表
            List<string> list = new List<string>();
            list.Add("123");//索引为0
            list.Add("456");//索引为1
        }
    }
}
​

线性表的接口定义

public interface IListDS<T> {
int GetLength(;//求长度
void Clear(;//清空操作
​
bool IsEmpty0;//判断线性表是否为空
void Append(T item);//附加操作
void Insert(T item,int i);//插八操作
T Delete(int i);//刪除操作
T GetElem(int i);//取表元
​
int Locate(T value);//按值查找
​
}

顺序表

自定义自己实现List

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal interface IlistDS<T>
    {
        int GetLength();
        void Clear();
        bool IsEmpty();
        void Add(T item);
        void Insert(int index, T item);
        T Delete(int index);
        T this[int index] {  get; }
        T GetEle(int index);
        int Locate(T value);
    }
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal class SeqList<T> : IlistDS<T>
    {
        private T[] data;//用来存储数据
        private int count = 0;//表示存了多少个数据
​
​
        public SeqList(int size)//size就是最大容量
        {
            data = new T[size];
            count = 0;
        }
        public SeqList() : this(10)//默认构造函数的大小为10
        {
​
        }
        public T this[int index]
        {
            get { return GetEle(index); }
        }
​
        public void Add(T item)
        {
            if (count == data.Length)//说明当前数组已经存满
            {
                Console.WriteLine("当前顺序表已经存满,不允许再存");
            }
            else
            {
                data[count] = item;
                count++;
            }
        }
​
        public void Clear()
        {
            count = 0;
        }
​
        public T Delete(int index)
        {
            T temp = data[index];
            for (int i = index + 1; i < count; i++)
            {
                data[i - 1] = data[i];//把数据向前移动
            }
            count--;
            return temp;
        }
​
        public T GetEle(int index)
        {
            if (index >= 0 && index <= count - 1)//说明所以存在
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);
            }
        }
        /// <summary>
        /// 取得数据的个数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int GetLength()
        {
            return count;
        }
​
        public void Insert(int index, T item)
        {
            for (int i = count - 1; i >= index; i--)
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
            count++;
        }
​
        public bool IsEmpty()
        {
            return (count == 0);
        }
​
        public int Locate(T value)
        {
            for (int i = 0; i < count; ++i)
            {
                if (data[i].Equals(value))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SeqList<string> list = new SeqList<string>();
            list.Add("123");
            list.Add("456");
            list.Add("789");
            Console.WriteLine(list.GetEle(0));
            Console.WriteLine(list[0]);
            list.Insert(1,"777");
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(list[i]+" ");  
            }
            Console.WriteLine();
            list.Delete(0);
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(list[i] + " ");
            }
            list.Clear();
            Console.WriteLine();
            Console.WriteLine(list.GetLength());
        }
    }
}
​

链表

自定义代码实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    /// <summary>
    /// 单链表的结点
    /// </summary>
    /// <typeparam name="T"></typeparam>
    internal class Node<T>
    {
        private T data; //存储数据
        private Node<T> next;//指针 用来指向下一个元素
​
        public Node()
        {
            data = default(T);
            next = null;
        }
​
​
        public Node(T value)
        {
            data = value;
            next = null;
        }
        public Node(T value, Node<T> next)
        {
            this.data = value;
            this.next = next;
        }
​
        public Node(Node<T> node)
        {
            this.next = next;
​
​
        }
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
​
        public Node<T> Next
        {
            get { return next; }
            set {  next = value; }
        }
​
    }
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal interface IlistDS<T>
    {
        int GetLength();
        void Clear();
        bool IsEmpty();
        void Add(T item);
        void Insert(int index, T item);
        T Delete(int index);
        T this[int index] {  get; }
        T GetEle(int index);
        int Locate(T value);
    }
}
​
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal class LinkList<T> : IlistDS<T>
    {
        private Node<T> head;
        public LinkList()
        {
            head = null;
        }
​
​
        public T this[int index]
        {
            get
            {
                Node<T> temp = head;
                for (int i = 1; i <= index; i++)
                {
                    temp = temp.Next;
​
                }
                return temp.Data;
            }
        }
        public void Add(T item)
        {
            Node<T> newnode = new Node<T>(item);//根据新的数据创建一个新的节点
            //如果头节点为空,那么这个新的节点就是头节点
            if (head == null)
            {
                head = newnode;
            }
            else
            {//把新来的节点放在链表的尾部
                //要访问到链表的尾节点
                Node<T> temp = head;
                while (true)
                {
                    if (temp.Next != null)
                    {
                        temp = temp.Next;
                    }
                    else
                    {
                        break;
                    }
                }
                temp.Next = newnode;//把新来的节点放在链表的尾部
​
            }
​
        }
​
        public void Clear()
        {
            head = null;
        }
​
        public T Delete(int index)
        {
            T data = default(T);
            if (index == 0) //删除头节点 
            {
                data = head.Data;
                head = head.Next;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 1; i <= index - 1; i++)
                {
                    temp = temp.Next;
​
                }
                Node<T> perNode = temp;
                Node<T> currentNode = temp.Next;
                data = currentNode.Data;
                Node<T> nextNode = temp.Next.Next;
                perNode.Next = nextNode;
            }
            return data;
        }
​
        public T GetEle(int index)
        {
            return this[index];
        }
​
        public int GetLength()
        {
            if (head == null)
            {
​
                return 0;
            }
            Node<T> temp = head;
            int count = 1;
            while (true)
            {
                if (temp.Next != null)
                {
                    count++;
                    temp = temp.Next;
                }
                else { break; }
​
            }
            return count;
        }
​
        public void Insert(int index, T item)
        {
            Node<T> newNode = new Node<T>(item);
            if (index == 0)//插入头节点
            {
                newNode.Next = head;
                head = newNode;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 1; i <= index - 1; i++)
                {
                    //让temp向后移动一个位置
                    temp = temp.Next;
                }
​
                Node<T> perNode = temp;
                Node<T> currentNode = temp.Next;
                perNode.Next = newNode;
                newNode.Next = currentNode;
            }
        }
        public bool IsEmpty()
        {
            return head == null;
        }
​
        public int Locate(T value)
        {
            Node<T> temp = head;
            if (temp == null)
            {
                return - 1;
            }
            else
            {
                int index = 0;
                while (true)
                {
                    if (temp.Data.Equals(value))
                    {
                        return index;
                    }
                    else
                    {
                        if(temp.Next == null)
                        {
                            temp=temp.Next;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return -1;
            }
        }
    }
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 线性表
{
    internal class Program
    {
        static void Main(string[] args)
        {
            LinkList<string> list = new LinkList<string>();
            list.Add("123");
            list.Add("456");
            list.Add("789");
            Console.WriteLine(list.GetEle(0));
            Console.WriteLine(list[0]);
            list.Insert(1,"777");
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(list[i]+" ");  
            }
            Console.WriteLine();
            list.Delete(0);
            for (int i = 0; i < list.GetLength(); i++)
            {
                Console.Write(list[i] + " ");
            }
            list.Clear();
            Console.WriteLine();
            Console.WriteLine(list.GetLength());
        }
    }
}
​

双向链表

循环链表

课程:        201-线性表介绍List-T_哔哩哔哩_bilibili

标签:index,线性表,C#,list,System,int,using,public
From: https://blog.csdn.net/weixin_64532720/article/details/140571087

相关文章

  • TypeScript体操(一):从基础到进阶
    目录前言UtilityTypes是什么?常用UtilityTypes前置知识`typeof``keyof``typeof`和`keyof`的区别`never`关键字`extends`关键字结合条件判断`infer`类型推断(模式匹配)判断是与非判断两个类型是否相等或兼容循环递归嵌套字符串数组协变(Covariance)逆变(Contravarian......
  • javascript中常规操作节点的方法
    JavaScript常用操作DOM节点的方法包括获取节点、创建节点、添加节点、删除节点、替换节点等。1.获取节点(1)通过ID获取使用document.getElementById(“元素ID”)方法,通过元素的ID获取单个元素。这是最常用的方法之一,因为ID在页面中是唯一的,可以直接定位到具体元素。<d......
  • C++合作开发项目:美术馆1.0
    快乐星空MakerZINCFFO合作入口:CM工作室效果图:代码:(还有几个音乐!)main.cpp#include<bits/stdc++.h>#include<windows.h>#include<conio.h>#include<time.h>#include"music.h"usingnamespacestd;structCITYBLOCK{ stringi......
  • 4mic阵列基础讲解
    概述4麦克风线性阵列是指在一条直线上等间距排列的四个麦克风,用于拾取声源信号。它常用于语音识别、会议系统、远场语音交互等应用。通过信号处理技术,4麦克风线性阵列可以提高语音信号的质量,抑制噪声和混响,提高语音识别的准确率。组成结构麦克风阵列:由四个麦克风组成,通常是全向......
  • A Bit More Common (2024牛客多校1 B)
    进入博客阅读体验更佳:ABitMoreCommon(2024牛客多校1B)|付诺の小站(funuo0728.github.io)ABitMoreCommon题目大意给定两个整数n和m(1\len,m\le5000),在所有长度为n且元素取值范围为[0,2^m)的序列中,计算存在两个合法子序列的序列个数。其中,合法子序列是指......
  • C#中的Func
    1.Func委托的定义和使用步骤         Func委托在C#中用于表示一个具有指定参数和返回类型的方法。‌Func委托的定义允许你指定参数的类型和返回值的类型,‌它可以有多达16个参数。‌第一个泛型参数表示方法的参数类型,‌最后一个泛型参数表示方法的返回类型。‌   ......
  • Docker-harbor私有仓库部署与管理
    1、什么是HarborHarbor是VMware公司开源的企业级DockerRegistry项目,其目标是帮助用户迅速搭建一个企业级的DockerRegistry服务。Harbor以Docker公司开源的Registry为基础,提供了图形管理UI、基于角色的访问控制(RoleBasedAccessControl)、AD/LDAP集成、以......
  • C语言数据类型和变量
    1.数据类型介绍C语言提供了丰富的数据类型来描述生活中的各种数据。所谓“类型”,就是相似的数据所拥有的共同特征,编译器只有知道了数据的类型,才知道怎么操作数据。1.1数据类型下面盘点一下C语言提供的各种数据类型,本章节主要探讨内置数据类型:具体解释:1.2各种数据类型......
  • Array Craft(Round 960)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • LeetCode题(66,69,35,88)--《c++》
     66.加一////Createdbywxj05on2024/7/20.////法一classSolution{public:vector<int>plusOne(vector<int>&digits){boolcarry=true;//进位标志for(inti=digits.size()-1;i>=0&&carry;--i){......