我有一个链表,怎么在链表当中插入一个节点,还有删除任意位置的节点

zsy_good 2002-05-26 08:40:24
public class Item
{
public Item Next;
}
public class Items
{
private Item head= new Item(); //保存链表的头地址
private Item temp; //用来记录当前的节点
private int count=0;
public int Count //返回整个链表的长度
{
get
{
return count;
}
}
//添加和删除方法怎么写呀
...全文
537 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
spgoal 2002-05-26
  • 打赏
  • 举报
回复
这个是我写的,你参考参考
namespace List
{
/// <summary>
/// Summary description for ListNode.
/// </summary>
// 结点类
public class ListNode
{
public ListNode(int NewValue)
{
Value=NewValue;
}
/// <summary>
/// 前一个
/// </summary>

public ListNode Previous;
/// <summary>
/// 后一个
/// </summary>
public ListNode Next;
/// <summary>
/// 值
/// </summary>
public int Value;
}
public class Clist
{
protected ListNode Head; //头结点
protected ListNode p; //当前结点

protected int ListLen;
public Clist()
{
Head=null;
p=null;
ListLen=0;
}
~Clist(){}
/*public ListNode DestoryList()
{

}*/
/// <summary>
/// 是否在链头
/// </summary>
public bool IsBof()
{
if (Head==p)
{
return true;
}
return false;
}
/// <summary>
///是否在链尾
/// </summary>
public bool IsEof()
{
if(p.Next==null)
return true;
return false;
}
/// <summary>
/// 清空链表
/// </summary>
public void ClearList()
{
Head=null;
p=null;
ListLen=0;
return;
}
/// <summary>
/// 链表是否为空
/// </summary>
public bool ListEmpty()
{
if(ListLen==0)
return true;
return false;
}

/// <summary>
/// 链表长度
/// </summary>
public int ListLength
{
get{return ListLen;}
}
/// <summary>
/// 指针后移
/// </summary>
public void MoveNext()
{
if(!IsEof())
{
p=p.Next;
}
return;
}
/// <summary>
/// 指针前移
/// </summary>
public void MovePrev()
{
if(!IsBof())p=p.Previous;
return;
}
/// <summary>
/// 指针移到表头
/// </summary>
public void MoveFirst()
{
p=Head;
return;
}
/// <summary>
/// 返回指定位置结点的值
/// </summary>
public int GetElem(int i)
{

ListNode move;
if(!ListEmpty() && i>=1 && i<=ListLength)
{
int j;
move=Head;
for(j=1;j<i;j++)
//while(move!=null)
{
move=move.Next;
}
return move.Value;
}
return 0;
}
//public int PriorElem(ListNode ThisList,int cur_e){}
//public int NextElem(ListNode ThisList,int cur_e){}
/// <summary>
/// 添加新结点
/// </summary>
public void AppendNode(int e)
{
ListNode NewNode=new ListNode(e);
if(ListEmpty())
{
Head=NewNode;
p=NewNode;
}
else
{
p.Next=NewNode;
NewNode.Previous=p;
p=NewNode;
}
ListLen++;
return;
}
/// <summary>
/// 前插结点
/// </summary>
public void ListInsert(int i,int e)
{
ListNode NewNode=new ListNode(e);
ListNode q;
ListNode move;
int j;
if(!ListEmpty() && i>=1 && i<=ListLength)
{
if(i==1)
{
Head.Previous =NewNode;
NewNode.Next =Head;
Head=NewNode;
p=NewNode;
ListLen++;
}
else
{
move=Head;
for(j=1;j<i;j++)
//while(move.Next!=null)
{
move=move.Next;
}
q=move.Previous;
NewNode.Next =move;
move.Previous =NewNode;
q.Next =NewNode;
NewNode.Previous =q;
move=NewNode;
p=NewNode;
ListLen++;
}


}
else
{
Head=NewNode;
p=NewNode;
}
return;

}
/// <summary>
/// 删除指定位置的结点
/// </summary>
public void ListDelete(int i)
{
ListNode q;
int j;
if(!ListEmpty() && i>=1 && i<=ListLength)
{
if(i==1)
{
q=Head.Next;
q.Previous =null;
Head.Next=null;
Head=q;
}
else
{
if(i<ListLength)
{
MoveFirst();
for(j=1;j<=i;j++)
MoveNext();
q=p.Next;
q.Previous =p.Previous;
p.Previous.Next=q;
p=q;
}
else
{
MoveFirst();
for(j=1;j<=i;j++)
MoveNext();
q=p.Previous ;
q.Next=null;
p=q;
}

}
ListLen--;
}
}
/// <summary>
/// 判断链表中是否存在e
/// </summary>
public bool Isin(int e)
{
bool bl=false;
ListNode move;
int i;
move=Head;
while(move!=null)
//for(i=1;i<=ListLength;i++)
{
if(move.Value==e)
{
bl=true;
break;
}
else
{
move=move.Next;
}
}

return bl;
}
public string PrintList()
{

ListNode move;
string strtext="";
move=Head;
while(move!=null)
{
strtext=strtext + move.Value.ToString() + ",";
move=move.Next;
}

return strtext;
}

}
}
zsy_good 2002-05-26
  • 打赏
  • 举报
回复
public Item Add(string text) //添加一个新的节点
{ //因为temp是头节点所以用temp.Next
if (count == 0)
end = head;
end.Next = new Item(); //创建一个新的节点把他放到最后节点的后面
end = end.Next ; //把end指向新节点的头部
temp = end;
end.Text = text; //节点的Index从0开始
end.Next = null;
count++; //整个链表的节点数加一
return temp;
}

public Item Item(int index)
{
if (index < 0 || index > count) //如果index<0 或大于链表节点的总数,则返回
return null;
Item tmp = head.Next ; //把第一个节点给tmp
for (int i= 0; i<index;i++)
{
tmp=tmp.Next; //下移一个节点
}
return tmp;
}
public void Remove(int index) //删除指定位置的节点
{
if (index < 0 || index > count) //如果index<0 或大于链表节点的总数,则返回
return;
Item tmp = head; //把第一个节点给tmp
for (int i = 0;i < index-1;i++)
tmp = tmp.Next;
Item tmp1 = tmp;
tmp1=tmp1.Next ;
tmp1=tmp1.Next;
tmp.Next =tmp1;
count--; //如果当前节点的index
return ;

//我先添加10个界点 }
//我删除 a.Remove(2);删除到剩一个节点
//然后我在添加10各界点
//当我在显示的时候就会报错,我到底那里错了呀
//这是我显示节点的代码
listBox1.Items.Clear();
int j=a.Count ;
for (int i =0 ; i<j ;i++)
listBox1.Items.Add (a.Item(i).Text);
zsy_good 2002-05-26
  • 打赏
  • 举报
回复
public void Add(Item t)
{
Item tt=new Item();
tt=t;
Item k=head;
while (k.Next==null) //这个while一定要用吗?可以可以在每次添加的时候记录下最后一个节点然后直接添加。这样的话要是有好多节点就会慢很多
k=k.Next;
tt.Next=k.Next;
k.Next=tt;
count++;
}
Heineken 2002-05-26
  • 打赏
  • 举报
回复
public void Add(Item t)
{
Item tt=new Item();
tt=t;
Item k=head;
while (k.Next==null)
k=k.Next;
tt.Next=k.Next;
k.Next=tt;
count++;
}
public void Delete(int t)
{
if (t>count)
return;
Item tt=head;
Item kk;
for (int i=0;i<t-1;i++)
tt=tt.Next;
kk=tt.Next;
kk=tt.Next;
tt.Next=kk;
}
wl_95421 2002-05-26
  • 打赏
  • 举报
回复
你不需要自己写吧
有一个ArrayList类
你可以用的

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧