复制另一个链表
本文关键字:链表 另一个 复制 | 更新日期: 2023-09-27 18:15:47
基本上我有一个小问题。我就是做不到。我到处都找过了。我正在尝试使用Concat方法复制列表
public void Concat(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
可以正常工作
然而,我的Copy方法没有任何运气:
public void Copy(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
Concat(list2);
AppendItem(list2.list.Data);
temp = temp.Next;
}
}
我知道这段代码可能很愚蠢,我只是一直在改变代码/实验和尝试。
当我执行代码时,没有显示任何内容。
list2.AddItem(56);
list2.AddItem(88);
list2.AddItem(17);
list.AddItem(40);
list.AddItem(11);
list.AddItem(77);
list3.Copy(list2);
list3.Copy(list);
System.Console.WriteLine("Copy List" + list3.DisplayItems());
本质上我想要这样的输出(可能输入的数字顺序错了):
Copy List 56 88 17 40 11 77
谢谢。
编辑-我是一个白痴,忘记了AppendItem方法。
public void AppendItem(T item)
{
LinkGen<T> temp = list;
if (list == null)
{
new LinkGen<T>(item, null);
}
else
{
while (temp.Next != null)
{
temp = temp.Next;
}
temp.Next = new LinkGen<T>(item, null);
}
}
编辑,
class LinkGen<T>
{
private T data;
private LinkGen<T> next;
public LinkGen(T item)
{
data = item;
next = null;
}
public LinkGen(T item, LinkGen<T> list)
{
data = item;
next = list;
}
public LinkGen<T> Next
{
set { this.next = value; }
get { return this.next; }
}
public T Data
{
set { this.data = value; }
get { return this.data; }
}
}
}
class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen()
{
this.list = null;
}
public void AddItem(T item)
{
list = new LinkGen<T>(item, list);
}
public string DisplayItems() //write items to string and return
{
LinkGen<T> temp = list;
string buffer = "";
while (temp != null) // move one link and add head to the buffer
{
buffer += temp.Data + "";
temp = temp.Next;
}
return buffer;
}
public int NumberOfItems() // returns number of items in list
{
LinkGen<T> temp = list;
int count = 0;
while (temp != null) // move one link and add 1 to count
{
count++;
temp = temp.Next;
}
return count;
}
public void RemoveItem(int item)
{
LinkGen<T> current = list;
LinkGen<T> previous = null;
while (current != null)
{
if (current.Data.Equals(item))
{
if (previous != null)
{
previous.Next = current.Next;
current = current.Next;
}
else
{
previous = current;
current = current.Next;
list = current;
}
}
else
previous = current;
current = previous.Next;
}
}
public void InsertInOrder(T item)
{
LinkGen<T> temp = list;
if (list == null || list.Data.CompareTo(item) < 0)
{
list = new LinkGen<T>(item, this.list);
}
else
{
while (temp != null)
{
if (list.Data.CompareTo(item) == 0 || list.Data.CompareTo(item) > 0)
{
temp.Next = new LinkGen<T>(item, temp.Next);
temp = null;
}
else
{
temp = temp.Next;
}
}
}
}
public void AppendItem(T item)
{
LinkGen<T> temp = list;
if (list == null)
{
new LinkGen<T>(item, null);
}
else
{
while (temp.Next != null)
{
temp = temp.Next;
}
temp.Next = new LinkGen<T>(item, null);
}
}
public void Concat(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
建议修改
class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen() { }
private LinkGen<T> LastNode ()
{
var temp = list;
while (temp != null && temp.Next != null)
temp = temp.Next;
return temp;
}
public void AppendItem(T item)
{
if (list == null)
list = new LinkGen<T>(item, null);
else
{
LinkGen<T> temp = LastNode();
temp.Next = new LinkGen<T>(item, null);
}
}
public void Concat(LinkListGen<T> list2)
{
if (list2 == null)
return;
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
public void Copy(LinkListGen<T> list2)
{
Concat(list2);
}
class LinkGen<T2>
{
public LinkGen(T2 item): this(item, null) { }
public LinkGen(T2 item, LinkGen<T2> list)
{
Data = item;
Next = list;
}
public LinkGen<T2> Next { set; get; }
public T2 Data { set ; get ; }
}
}
答案-
public void Copy(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
如果有人有和我一样的问题。他们终于找到了解决办法。是的,我知道....我是个白痴!