C#算法与数据结构之线性结构

线性结构是什么?

  线性结构是一种数据结构,它有一对一的关系,就像一个长对,一个接一个,特点是,除第一个元素和最后一个元素外,其它元素前后只有一个元素。

  简单示例1:

       

static void Main(string[] args)
{
//使用BCL中的List线性表
List<string> strList = new List<string>();
strList.Add("123");
strList.Add("456");
strList.Add("789");
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count);
strList.Remove("123"); //去除某一元素
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count);

}

  输出为:456

      3  

      789

      2

线性表实现方式

        顺序表:连续排放

        单链表:

      双向链表

      循环链表

自设一个List<>:

首先创建一个接口,然后实现接口得所有方法,完程自建List功能:

  class SeqList<T>:IListDS<T>
    {
        private T[] data;     //存储数据
        private int count = 0;

        public SeqList(int size)         
        {
            data = new T[size];
            count = 0;
        }
        public SeqList():this(10)      //默认构造函数10
        {

        }
        public void Add(T item)        //添加物件
        {
            if(count == data.Length)
            {
                Console.WriteLine("空间已存满");
            }
            else
            {
                data[count] = item;
                count++;
            }
        }
        //取得数据个数

        public int GetLength()           //得到长度
        {
            return count;
        }

        public T GetEle(int index)         //返回索引
        {
            if(index>=0&&index<=count-1)
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);
            }
        }
        public void Clear()
        {
            count = 0;
        }
        public bool IsEmpty()
        {
            return count == 0;
        }
        public void Insert(T item,int index)
        {
            
            for(int i=count-1;i>=count;i--)
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
            count++;
        }
        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 this[int index]
        {
            get { return GetEle(index); }
        }
        public int Locate(T value)
        {
            for(int i=0;i<count;i++)
            {
                if(data[i].Equals(value))
                {
                    return i;
                }
            }
            return -1;
        }
           
    }

测试:

  static void Main(string[] args)
        {
            SeqList<string> seqList = new SeqList<string>();
            seqList.Add("123");
            seqList.Add("456");
            seqList.Add("789");
            Console.WriteLine(seqList.GetEle(1));
            Console.WriteLine(seqList[1]);
            seqList.Insert("666",1);
            for(int i=0;i<seqList.GetLength();i++)
            {
                Console.WriteLine(seqList[i] + ".");
            }
            seqList.Clear();
            Console.WriteLine(seqList.GetLength());
            Console.ReadKey();



        }

结果:

456
456
123.
666.
456.
789.
0              结果OK,功能实现

单链表:     data     next                      示图:        

 

 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> next)
        {
            this.next = next;
        }

        public T Data
        {
            get { return data; }
            set { data = value; }

        }
        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }
        
        
    }
class LinkList<T>:IListDS<T>
    {
        private Node<T> head;
        public LinkList()
        {
            head = null;
        }
        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 Insert(T item, int index)
        {
            Node<T> newNode = new Node<T>(item);
            if(index==0)
            {
                newNode.Next = head;
            }
            else
            {
                Node<T> temp = head;
                for(int i=1;i<index-1;i++)
                {
                    temp = temp.Next;
                }
                Node<T> preNode = temp;
                Node<T> currentNode = temp;
                preNode.Next = newNode;
                newNode.Next = currentNode;
            }
        }
        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> preNode = temp;
                Node<T> currentNode = temp.Next;
                data = currentNode.Data;
                Node<T> nextNode = temp.Next.Next;
                preNode.Next = nextNode;
                
            }
            return data;
        }
        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 Clear()
        {
            head = null;
        }
        public bool IsEmpty()
        {
            return head == null;
        }
        public T this[int index]
        {
            get
            {
                Node<T> temp = head;
                for(int i=1;i<=index-1;i++)
                {
                    temp = temp.Next;
                }
                return temp.Data;
            }
        }
        public T GetEle(int index)
        {
            return this[index];
        }
        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;
            }
        }
    }

 

转载于:https://www.cnblogs.com/redkop/p/7724985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值