LinkedList Wrong Display(string builder)
- by Chris
Hello,
The following program is a basic linked list divided in 3 classes.
In the tester class (main) i add several numbers to the list (sorted).
But insteed of getting the numbers as a result i get the result: LinkedList.LinkedList
Is something wrong with the stringbuilder (the program was first in java where a string buffer was used, but that should be the same i think?)
LinkedListTester.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedList
{
public class LinkedListTester
{
static void Main(string[] args)
{
LinkedList ll = new LinkedList();
ll.addDataSorted(5);
ll.addDataSorted(7);
ll.addDataSorted(13);
ll.addDataSorted(1);
ll.addDataSorted(17);
ll.addDataSorted(8);
Console.WriteLine(ll);
}
}
}/LinkedList/
LinkedList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedList
{
public class LinkedList
{
//toestand
private LinkedListNode first;
private LinkedListNode last;
//gedrag
public LinkedList()
{
first = null;
last = null;
}
public void addDataInFront(int data)
{
first = new LinkedListNode(data, first);
if (last == null){
last = first;
}
}/*addDataInFront*/
public void addDataToBack(int data)
{
if (first == null)
{
addDataInFront(data);
}
else
{
last.setNext(new LinkedListNode(data, null));
last = last.getNext();
}
}/*addDataToBack*/
public void addDataSorted(int data)
{
if (first == null || first.getData() > data)
{
addDataInFront(data);
}
else
{
LinkedListNode currentNode = first;
while (currentNode.getNext() != null && currentNode.getNext().getData() < data)
{
currentNode = currentNode.getNext();
}
currentNode.setNext(new LinkedListNode(data, currentNode.getNext()));
currentNode = currentNode.getNext();
if (currentNode.getNext() == null)
{
last = currentNode;
}
}
}/*addDataSorted*/
public String toString()
{
StringBuilder Buf = new StringBuilder();
LinkedListNode currentNode = first;
while (currentNode != null)
{
Buf.Append(currentNode.getData());
Buf.Append(' ');
currentNode = currentNode.getNext();
}
return Buf.ToString();
}/*toString*/
}/*LinkedList*/
}/LinkedList/
LinkedListNode:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedList
{
public class LinkedListNode
{
//toestand
private int data;
private LinkedListNode next;
private LinkedListNode previous;
//gedrag
public LinkedListNode(int data, LinkedListNode next)
{
this.data = data;
this.next = next;
this.previous = null;
}
public LinkedListNode(int data, LinkedListNode next, LinkedListNode previous)
{
this.data = data;
this.next = next;
this.previous = previous;
}
public LinkedListNode getNext()
{
return next;
}
public LinkedListNode getPrevious()
{
return previous;
}
public void setNext(LinkedListNode next)
{
this.next = next;
}
public void setPrevious(LinkedListNode previous)
{
this.previous = previous;
}
public int getData()
{
return data;
}
}/*LinkedListNode*/
}/LinkedList/