add(float x, int pos) in a list (linked)



我被要求实现函数MyList(即使它在C#中默认已经存在)。

我设法完成了代码的很大一部分,但我被困在最后一个函数上(我试图以 2 种不同的方式解决它,我将在下面显示,这显然不起作用)。

他希望我们在最后一个函数中这样做:

public void add(float x, int pos)
{
// Add x at the position pos, pos = 0 refer to the first element.
}

起初我试图这样解决它:

if (pos == 0)
{
add(x);
}
else
{
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
for (int i = 0; i < pos; i++)
{
add(x);
}
}
}

我在这里尝试的是,我将执行一个双精度 for 循环,以便指针与位置pos位于同一位置,然后将float x添加到该位置。

但它没有用。以下 main() 的输出是:

static void Main(string[] args)
{
MyList l = new MyList();
l.add(0);
l.add(1);
l.add(2);
l.add(3);
l.add(4);
l.add(5);
l.add(6);
l.add(109, 2);
l.print();
}

输出:

109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
109
6
5
4
3
2
1
0

所以我用不同的方式尝试了它:

if (pos == 0)
{
add(x);
}
else
{
int count = 0;
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
count++;
if(count == pos)
{
add(x);
break;
}
}
}

我在这里尝试的是完全不同的。我实现了count,以便当count等于pos时,指针将位于pos位置,然后我可以将float x添加到该位置。但它也没有奏效。对于相同的先前 main(),这是输出:

109
6
5
4
3
2
1
0

这里的问题是,无论pos是什么职位。数字109将留在它的位置。

在这里,您将找到我所做的一切(除了add(float x, int pos)函数,我需要您的帮助来解决):

public class MyList
{
class Element
{
public float value;
public Element next;
}
Element first;
public MyList()
{
first = null;
}
public void add(float x)
{
Element e = new Element();
e.value = x;
e.next = first;
first = e;
}
public float get(int i)
{
if (first == null)
{
throw new Exception("Empty list... no elements inside");
}
Element tmp = first;
for (int j = 0; j < i; ++j)
{
tmp = tmp.next;
if (tmp == null)
{
throw new Exception("...");
}
}
return tmp.value;
}
public void print()
{
Element e = first;
while (e != null)
{
Console.WriteLine(e.value);
e = e.next;
}
}
public bool find(float x)
{
Element e = first;
while (e != null)
{
if (e.value == x)
{
return true;
}
e = e.next;
}
return false;
}
public float max()
{
float G = 0;
for (Element e = first; e != null; e = e.next)
{
if (e.value > G)
{
G = e.value;
}
}
return G;
}
public int count()
{
Element e = first;
int c = 0;
while (e != null)
{
c++;
e = e.next;
}
return c;
}
public int count(float x)
{
int c = 0;
for (Element e = first; e != null; e = e.next)
{
if (e.value == x)
{
c++;
}
}
return c;
}
public float sum()
{
float S = 0;
for (Element e = first; e != null; e = e.next)
{
S += e.value;
}
return S;
}
public float average()
{
return sum() / count();
}
public void removeFirst()
{
Element e = first;
first = e.next;
}
}
if (pos == 0)
{
add(x);
}
else
{
int count = 0;
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
count++;
if(count == pos)
{
add(x); // this always adds the element to the beginning
break;
}
}
}

关于它的事情是,无论您在代码中的哪个位置使用它,add(x)都会做同样的事情。您必须手动将元素插入该位置。

int count = 0;
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
count++;
if(count == pos)
{
var newElem = new Element();
newElem.value = x;
newElem.next = tmp.next;
tmp.next = newElem;
break;
}
}

问题是在你调用if(count == pos)中,add(x)总是在列表的开头添加x

Insead,您的add(float x, int pox)方法应如下所示:

if (pos == 0)
{
add(x);
}
else
{
int count = 0;
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
count++;
if(count == pos)
{
Element e = new Element();
e.value = x;
e.next = tmp.next;
tmp.next = e;
break;
}
}
}
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
for (int i = 0; i < pos; i++)
{
add(x);
}
}

在第一次尝试中,您将值添加x与列表中每个元素之后pos次数一样多。此外,由于添加会断开链接并将第一个元素推送到最后一个元素。你得到的结果是:[x重复pos次],最后是initial first value

int count = 0;
for (Element tmp = first; tmp != null; tmp = tmp.next)
{
count++;
if(count == pos)
{
add(x);
break;
}
}

在发送尝试中,您执行了正确的循环以查找插入值的位置。但是,您将值直接添加到链接列表的顶部,而没有使用刚刚发现的tmp位置。


因此,您只需要稍微更改第二种方法即可在tmp之后插入值:

for (Element tmp = first; tmp != null; tmp = tmp.next)
{
count++;
if(count == pos)
{
tmp.next = new Element { value = x, next = tmp.next };
break;
}
}

最新更新