我被要求实现函数MyList
(即使它在C#中默认已经存在)。
我设法做了很多代码,但是我坚持最后的功能(我尝试用两种不同的方式解决它,我将在下面展示,这显然不起作用)。< / p>
他希望我们做最后的功能:
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);
}
}
}
我在这里尝试的是,我会做一个双循环,以便指针与位置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;
}
}
答案 0 :(得分:3)
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;
}
}
答案 1 :(得分:0)
问题在于,您在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;
}
}
}
答案 2 :(得分:0)
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;
}
}