我在弄虚拟线性表!代码很多!那就复制出错误的一个吧!using System;
using System.Drawing;
namespace WindowsApplication1
{
public class LinkedList
{
private int count;
private MemoryUnit _head;
private Heap _heap;
private Color _background;
public LinkedList(Heap heap, Color color)
{
_heap = heap;
_background = color;
}
public void Add(string value)
{
MemoryUnit newNode = _heap.NewUnit(_background);
newNode.Value = value;
if (_head == null)
{
_head = newNode;
}
else
{
GetByIndex(count - 1).Next = newNode;
}
count++;
}
public void Insert(int index, string value)
{
MemoryUnit newNode = _heap.NewUnit(_background);
newNode.Value = value;
if (index == 0)
{
if (_head == null)
{
_head = newNode;
}
else
{
newNode.Next = _head;
_head = newNode;
}
}
else
{
MemoryUnit PrevNode = GetByIndex(index - 1);
MemoryUnit nextNode = PrevNode.Next;
PrevNode.Next = newNode;
newNode.Next = nextNode;
}
count++;
}
public void RemoveAt(int index)
{
if (index == 0)
{
_head.SetUnuse();
_head = _head.Next;
}
else
{
MemoryUnit prevNode = GetByIndex(index - 1);
if (prevNode.Next == null)
{
throw new ArgumentOutOfRangeException("索引超出范围");
}
prevNode.Next.SetUnuse();
prevNode.Next = prevNode.Next.Next;
}
count--;
}
private MemoryUnit GetByUndex(int index)
{
if((index<0)||(index>=this.count))
{
throw new ArgumentOutOfRangeException("索引超出范围");
}
MemoryUnit tempNode=this._head;
for(int i=0;i<index;i++)
{
tempNode=tempNode.Next;
}
return tempNode;
}
}
}