问题
I'm learning how to make a linked list in c#. I have the below code which is not working for me. I just want to add the nodes in the main as i have below then iterate over all nodes that will print to the console.
using System;
class node
{
public object data;
public node next;
public node()
{
data = null;
next = null;
}
public node(object o)
{
data = o;
next = null;
}
public node(object data, node next)
{
this.data = data;
this.next = next;
}
}
class linkedList
{
private node headNode;
private node tailNode;
int node_count;
public void add(object entry)
{
if (headNode == null)
{
node newNode = new node(entry);
headNode = newNode;
++node_count;
}
else
{
if (node_count == 1)
{
node newNode = new node(entry, headNode);
tailNode = newNode;
}
else
{
node newNode = new node(entry, tailNode);
tailNode = newNode;
}
++node_count;
}
}
public void returnData()
{
if (headNode.next != null)
{
while (headNode.next != null)
{
Console.WriteLine(headNode.data + "\n");
}
}
else
Console.WriteLine("Not Available");
}
}
class Exercise
{
static int Main()
{
linkedList ll = new linkedList();
ll.add(8);
ll.add(2);
ll.add(7);
ll.add(4);
ll.add(9);
ll.add(10);
ll.returnData();
Console.ReadLine();
return 0;
}
}
回答1:
Your code is completely broken.
Here's minimal changes needed to run it
using System;
class node
{
public object data;
public node next;
public node()
{
data = null;
next = null;
}
public node(object o)
{
data = o;
next = null;
}
public node(object data, node next)
{
this.data = data;
this.next = next;
}
}
class linkedList
{
private node headNode;
private node tailNode;
int node_count;
public void add(object entry)
{
node newNode = new node(entry);
if (headNode == null)
headNode = newNode;
if (tailNode != null)
tailNode.next = newNode;
tailNode = newNode;
++node_count;
}
public void returnData()
{
node currentNode = headNode;
if (currentNode == null)
Console.WriteLine("Not Available");
while (currentNode != null) {
Console.WriteLine(currentNode.data);
currentNode = currentNode.next;
}
}
}
class Exercise
{
static int Main()
{
linkedList ll = new linkedList();
ll.add(8);
ll.add(2);
ll.add(7);
ll.add(4);
ll.add(9);
ll.add(10);
ll.returnData();
return 0;
}
}
回答2:
you go a little turned around :D
Firstly all class names sould start with uppercap so Node (not node) and LinkedList (not linkedList).
Now you can either use this function but this will not work if u try to return a list with only one node... beacuse you are adding the nodes incorectly..
public void returnData()
{
if (tailNode.next != null)
{
Node currentNode = tailNode;
while (currentNode != null)
{
Console.WriteLine(currentNode.data + "\n");
currentNode = currentNode.next;
}
}
else
Console.WriteLine("Not Available");
}
But if you want a more regular classical linkedlist you can change the linkedlist add function to this...
public void add(object entry)
{
if (headNode == null)
{
Node newNode = new Node(entry);
headNode = newNode;
++Node_count;
}
else
{
if (Node_count == 1)
{
Node newNode = new Node(entry);
headNode.next = newNode;
tailNode = newNode;
}
else
{
Node newNode = new Node(entry);
tailNode.next = newNode;
tailNode = newNode;
}
++Node_count;
}
}
and the returnData to...
public void returnData()
{
if (headNode.next != null)
{
Node currentNode = headNode;
while (currentNode != null)
{
Console.WriteLine(currentNode.data + "\n");
currentNode = currentNode.next;
}
}
else
Console.WriteLine("Not Available");
}
this code adds Nodes to the end of the list if you want to add nodes to the front of the list you can modify the add function..
hope this helps if u have additional questions just ask
回答3:
To print your data you need to do while (headNode.next != null) in order for your list to be traversed.
It also looks like you never update node.next either
if (node_count == 1) // this seems unnecessary.
{
node newNode = new node(entry, headNode);
tailNode = newNode;
//should be node.next = newnode;
}
来源:https://stackoverflow.com/questions/7346384/how-to-create-multiple-nodes-in-a-linked-list-then-iterate-through-the-nodes