C# 在LinkedList代码C前的新手#
我前面有这段代码,但我无法构建包含多个元素的链表。我看到在方法“EINSETZENACH”中,if语句中的代码块永远不会执行。原因是光标永远不会移动!=0 原始代码是用Java编写的。 我很感谢你给我的提示C# 在LinkedList代码C前的新手#,c#,linked-list,C#,Linked List,我前面有这段代码,但我无法构建包含多个元素的链表。我看到在方法“EINSETZENACH”中,if语句中的代码块永远不会执行。原因是光标永远不会移动!=0 原始代码是用Java编写的。 我很感谢你给我的提示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EinfachVerketteteListe { class Program { st
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EinfachVerketteteListe
{
class Program
{
static void Main(string[] args)
{
Liste myList = new Liste();
myList.einsetzenVor(1, "Nr01");
myList.einsetzenVor(2, "Nr02");
myList.einsetzenNach(2, "Nr02");
myList.einsetzenNach(3, "Nr03");
myList.inhalt(3);
myList.laenge();
myList.ToString();
}
}
这是单元类
class Zelle
{
// Contents
public Object inhalt;
// Next cell
public Zelle next;
public Zelle(Object el)
{
inhalt = el;
}
public Zelle(Object el, Zelle z)
{
inhalt = el;
next = z;
}
public Zelle(Zelle z)
{
next = z;
}
}
我们的自定义列表类
public class Liste
{
// Start element
private Zelle anfang;
// Current Element
private Zelle cursor;
/// <summary>
/// There are no more items in the list, start is null
/// </summary>
/// <returns>start == null</returns>
public Boolean IstLeer()
{
return anfang == null;
}
/// <summary>
/// Length of the list
/// </summary>
/// <returns>l = listlenght</returns>
public int laenge()
{
Zelle cur = anfang;
int l = 0;
while (cur != null)
{
l++;
cur = cur.next;
}
return l;
}
/// <summary>
/// Check of the declared position is valid
/// </summary>
/// <param name="p">Position</param>
/// <returns>true/false</returns>
public Boolean istGueltigePosition(int p)
{
return (p >= 1) && (p <= laenge() );
}
/// <summary>
///
/// </summary>
/// <param name="p">Set cursor on a specific position</param>
public void setzeCursor(int p)
{
cursor = null;
if (istGueltigePosition(p))
{
Zelle cur = anfang;
// cur.next is null. The reason is that there is only one element in the list
// How can I fix this block. However I assume the code will work.
// Maybe I handle something not in the correct order.
for (int i = 0; i < p; i++)
{ cur = cur.next; }
cursor = cur;
}
}
公共类列表
{
//起始元素
私人安芳;
//当前元素
私人泽尔光标;
///
///列表中没有其他项,开始为空
///
///start==null
公共布尔IstLeer()
{
返回anfang==null;
}
///
///名单的长度
///
///l=列表长度
公共工程
{
泽勒库尔=安芳;
int l=0;
while(cur!=null)
{
l++;
cur=cur.next;
}
返回l;
}
///
///对声明位置的检查有效
///
///位置
///对/错
公共布尔值istGueltigePosition(int p)
{
return(p>=1)&&(p假设您成功地将第一个元素添加到列表中,现在希望添加第二个元素
检查有效位置后,将当前单元格设置为开始(Zelle cur=anfang;
)。之后,您希望到达要插入对象的位置。但由于它是第一个元素,因此没有以下元素。因此循环后cur
将始终为null
由于列表中没有从零开始的索引,因此应使用列表中的第一个索引开始循环。请尝试以下循环:
for (int i = 1; i < p; i++)
{
if (cur.next == null)
{
break;
}
cur = cur.next;
}
cursor = cur;
for(int i=1;i
如果代码被翻译成英文,将非常有帮助。只需显示代码中代表您的问题的相关部分即可。有人可以向我展示创建链表的最简单代码,该链表提供了以下方法:在特定位置插入元素,在特定位置删除元素,显示特定元素的数据。Y我们的答案很有帮助,我现在可以使用代码了。现在还不清楚如何查看第一个“Zelle”(例如,我创建了一个for循环,它调用“inhalt”方法,但在代码中,它不是从起始单元格开始的。有“inhalt”和“naechstes”方法booth无法显示第一个元素。原因是调用的方法“istGueltigePosition”执行检查“position>=1”@ChrieJ请为您的新问题发布一个新问题,并将此答案标记为解决方案,如果它解决了您在问题中提出的问题。
if (cursor != null)
{
Zelle z = new Zelle(e, cursor.next);
cursor.next = z;
}
}
/// <summary>
/// Insert cell after element p
/// </summary>
/// <param name="p">Position</param>
/// <param name="e">Daten</param>
public void einsetzenVor(int p, Object e)
{
if (p > 1) einsetzenNach(p-1,e);
else
{
// Insert at the beginning
Zelle z = new Zelle(e, anfang);
anfang = z;
}
}
public void loesche(int p)
{
if (istGueltigePosition(p))
{
if (p == 1) // Lösche 1. Object
anfang = anfang.next;
else
{
setzeCursor(p - 1);
cursor.next = cursor.next.next;
}
}
}
/// <summary>
/// Show the content
/// </summary>
/// <param name="p">position</param>
/// <returns></returns>
public Object inhalt(int p)
{
setzeCursor(p);
if (cursor == null) return null;
return cursor.inhalt;
}
/// <summary>
/// next cell/data
/// </summary>
/// <returns>Daten</returns>
public Object naechstes()
{
if (cursor == null) return null;
Object e = cursor.inhalt;
cursor = cursor.next;
return e;
}
}
}
for (int i = 1; i < p; i++)
{
if (cur.next == null)
{
break;
}
cur = cur.next;
}
cursor = cur;