Optimization C#链表,磁头#x2B;使用API InsertAfter+;去除是否看到任何缺陷或优化?

Optimization C#链表,磁头#x2B;使用API InsertAfter+;去除是否看到任何缺陷或优化?,optimization,linked-list,Optimization,Linked List,我在面试条件下写的另一个数据结构。它本质上是一个通用的链表,跟踪列表的开头和结尾(可能只是为了学术练习,在RL生活中,您只需要使用列表)。有人看到任何可能的缺陷或优化吗 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SingleLinkedHeadAndTail { class SingleListEntry<T> {

我在面试条件下写的另一个数据结构。它本质上是一个通用的链表,跟踪列表的开头和结尾(可能只是为了学术练习,在RL生活中,您只需要使用列表)。有人看到任何可能的缺陷或优化吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SingleLinkedHeadAndTail
{

    class SingleListEntry<T>
    {
        public T Data { get; set; }
        public SingleListEntry<T> Next {get; set;}
        public SingleListEntry(T data)
        {
            Data = data;
        }
    }

    public class SingleListExample<T>
    {
        private SingleListEntry<T> head;
        private SingleListEntry<T> tail;
        public int length { get; set; }
        public T Head
        {
            get
            {
                if (head != null)
                {
                    return head.Data;
                }
                return default(T);               
            }
        }

        public T Tail
        {
            get
            {
                if (tail != null)
                {
                    return tail.Data;
                }
                return default(T);
            }
        }

        public void Insert(T data)
        {
            if (head==null)
            {
                head = new SingleListEntry<T>(data);
                tail = head;
                length++;
            }
            else
            {
                tail.Next = new SingleListEntry<T>(data);
                tail = tail.Next;
                length++;
            }
        }

        public void InsertAfter(T data, int position)
        {
            if (position < 0)
            {
                throw new Exception("no soup for you - position must be 0 or higher");
            }

            if (head == null)
            {
                throw new Exception("sorry, you cannot insert after nothing, the list is empty.");
            }

            if (position >= length) //we could allow this stuff and padd out the list etc, but for now...no soup.
            {
                throw new Exception("Position requested is > then the length of the list.");
            }

            if (position == length - 1) //just an inswer
            {
                Insert(data);
                return;
            }


            SingleListEntry<T> newElement = new SingleListEntry<T>(data);
            SingleListEntry<T> temp = GetElementAt(position);             
            newElement.Next = temp.Next;
            temp.Next = newElement;            
            length++;
        }

        public T GetElement(int position)
        {
            return GetElementAt(position).Data;
        }

        private SingleListEntry<T> GetElementAt(int position)
        {
            SingleListEntry<T> temp = head;

            //pop down N levels until position
            int counter = 0;
            while (counter < position)
            {
                temp = temp.Next;
                counter++;
                if (temp == null && counter < position) //should have been caught
                {
                    throw new Exception(String.Format("{0} elements do not exist", position));
                }
            }

            return temp;
        }

        public void Remove(int position)
        {
            if (position < 0)
            {
                throw new Exception("no soup for you - position must be 0 or higher");
            }

            if (head == null)
            {
                throw new Exception("sorry, you cannot remove from nothing, the list is empty.");
            }

            if (position >= length) //we could allow this stuff and padd out the list etc, but for now...no soup.
            {
                throw new Exception("Position requested is > then the length of the list.");
            }

            if (position == 0) //head
            {
                head = head.Next;
                length--;
                return;
            }

            SingleListEntry<T> temp;
            temp = GetElementAt(position - 1);            
            if (position == length-1)
            { 
                tail = temp;
                tail.Next = null;
                length--;
                return;
            }


            temp.Next = temp.Next.Next;
            length--;            

        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            Test2();
            Test3();

        }

        public static void Test1()
        {
            SingleListExample<string> myList = new SingleListExample<string>();
            myList.Insert("joe");
            myList.Insert("mike");
            myList.Insert("adam");
            if (myList.Head != "joe") throw new Exception("fail");
            if (myList.Tail != "adam") throw new Exception("fail");
            if (myList.GetElement(1) != "mike") throw new Exception("fail");

        }

        public static void Test2()
        {
            SingleListExample<string> myList = new SingleListExample<string>();
            myList.Insert("joe");
            myList.Insert("mike");
            myList.Insert("adam");
            myList.InsertAfter("paul", 1);
            myList.InsertAfter("john", 0);
            myList.InsertAfter("nichole", 4);
            if (myList.Tail != "nichole") throw new Exception("fail");
            if (myList.Head!= "joe") throw new Exception("fail");

            if (myList.GetElement(0) != "joe") throw new Exception("fail");
            if (myList.GetElement(1) != "john") throw new Exception("fail");
            if (myList.GetElement(2) != "mike") throw new Exception("fail");
            if (myList.GetElement(3) != "paul") throw new Exception("fail");
            if (myList.GetElement(4) != "adam") throw new Exception("fail");
            if (myList.GetElement(5) != "nichole") throw new Exception("fail");

        }


        public static void Test3()
        {
            SingleListExample<string> myList = new SingleListExample<string>();
            myList.Insert("joe");
            myList.Insert("mike");
            myList.Insert("adam");
            myList.InsertAfter("paul", 1);
            myList.InsertAfter("john", 0);
            myList.InsertAfter("nichole", 4);

            myList.Remove(0);
            if (myList.Head != "john") throw new Exception("fail");

            myList.Remove(myList.length-1);
            if (myList.Tail != "adam") throw new Exception("fail");

            myList.Remove(1);
            if (myList.Head != "john") throw new Exception("fail");
            if (myList.Tail != "adam") throw new Exception("fail");
            if (myList.GetElement(0) != "john") throw new Exception("fail");
            if (myList.GetElement(1) != "paul") throw new Exception("fail");
            if (myList.GetElement(2) != "adam") throw new Exception("fail");


        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间SingleLinkedHeadTail
{
类单列输入
{
公共T数据{get;set;}
公共SingleListEntry下一个{get;set;}
公共SingleListEntry(T数据)
{
数据=数据;
}
}
公共类单列表示例
{
私人单亲家长;
私人单尾;
公共整数长度{get;set;}
公共T形头
{
得到
{
if(head!=null)
{
返回头数据;
}
返回默认值(T);
}
}
公共T形尾巴
{
得到
{
if(tail!=null)
{
返回尾部数据;
}
返回默认值(T);
}
}
公共空白插入(T数据)
{
if(head==null)
{
head=新的SingleListEntry(数据);
尾=头;
长度++;
}
其他的
{
tail.Next=新的SingleListEntry(数据);
tail=tail.Next;
长度++;
}
}
public void InsertAfter(T数据,int位置)
{
如果(位置<0)
{
抛出新异常(“无汤-位置必须为0或更高”);
}
if(head==null)
{
抛出新异常(“对不起,您不能在不插入任何内容之后插入,列表为空。”);
}
if(position>=length)//我们可以允许使用这些东西并填充列表等,但目前……没有汤。
{
抛出新异常(“请求的位置>列表的长度”);
}
if(position==length-1)//只是一个inswer
{
插入(数据);
返回;
}
SingleListEntry newElement=新的SingleListEntry(数据);
SingleListEntry temp=GetElementAt(位置);
newElement.Next=temp.Next;
temp.Next=新元素;
长度++;
}
公共T GetElement(内部位置)
{
返回GetElementAt(位置).Data;
}
私有SingleListEntry GetElementAt(内部位置)
{
单列输入温度=磁头;
//向下弹出N个级别直到位置
int计数器=0;
while(计数器<位置)
{
温度=下一个温度;
计数器++;
如果(temp==null&&counter=length)//我们可以允许使用这些东西并填充列表等,但目前……没有汤。
{
抛出新异常(“请求的位置>列表的长度”);
}
如果(位置==0)//头部
{
头=头。下一步;
长度--;
返回;
}
单输入温度;
温度=GetElementAt(位置-1);
如果(位置==长度-1)
{ 
尾=温度;
tail.Next=null;
长度--;
返回;
}
temp.Next=temp.Next.Next;
长度--;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
Test1();
Test2();
Test3();
}
公共静态void Test1()
{
SingleListExample myList=新建SingleListExample();
myList.插入(“joe”);
myList.插入(“mike”);
myList.插入(“adam”);
if(myList.Head!=“joe”)抛出新异常(“fail”);
if(myList.Tail!=“adam”)抛出新异常(“fail”);
if(myList.GetElement(1)!=“mike”)抛出新异常(“fail”);
}
公共静态void Test2()
{
SingleListExample myList=新建SingleListExample();
myList.插入(“joe”);
myList.插入(“mike”);
myList.插入(“adam”);
myList.InsertAfter(“paul”,1);
myList.InsertAfter(“john”,0);
myList.InsertAfter(“nichole”,4);
if(myList.Tail!=“nichole”)抛出新异常(“fail”);
if(myList.Head!=“joe”)抛出新异常(“fail”);
如果(myList.GetElement(0)!=“joe”)抛出新异常(“fail”);
if(myList.GetElement(1)!=“john”)抛出新异常(“fail”);
if(myList.GetElement(2)!=“mike”)抛出新异常(“fail”);
if(myList.GetElement(3)!=“paul”)抛出新异常(“fail”);
if(myList.GetElement(4)!=“adam”)抛出新异常(“fail”);
如果(myList.GetElement(5)!=“nichole”)抛出n