Hello everyone, thanks for taking the time to stop by my question.
Below you will find my working SLL, but I want to make more use of C# and, instead of having two classes, SLL and Node, I want to use Node's constructors to do all the work (To where if you pass a string through the node, the constructor will chop it up into char nodes). The problem is, after an a few hours of tinkering, I'm not really getting anywhere...
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace PalindromeTester
{
class Program
{
static void Main(string[] args)
{
SLL mySLL = new SLL();
mySLL.add('a');
mySLL.add('b');
mySLL.add('c');
mySLL.add('d');
mySLL.add('e');
mySLL.add('f');
Console.Out.WriteLine("Node count = " + mySLL.count);
mySLL.reverse();
mySLL.traverse();
Console.Out.WriteLine("\n The header is: " + mySLL.gethead);
Console.In.ReadLine();
}
class Node
{
private char letter;
private Node next;
public Node()
{
next = null;
}
public Node(char c)
{
this.data = c;
}
public Node(string s)
{
}
public char data
{
get { return letter; }
set { letter = value; }
}
public Node nextNode
{
get { return next; }
set { next = value; }
}
}
class SLL
{
private Node head;
private int totalNode;
public SLL()
{
head = null;
totalNode = 0;
}
public void add(char s)
{
if (head == null)
{
head = new Node();
head.data = s;
}
else
{
Node temp;
temp = new Node();
temp.data = s;
temp.nextNode = head;
head = temp;
}
totalNode++;
}
public int count
{
get { return totalNode; }
}
public char gethead
{
get { return head.data; }
}
public void traverse()
{
Node temp = head;
while(temp != null)
{
Console.Write(temp.data + " ");
temp = temp.nextNode;
}
}
public void reverse()
{
Node q = null;
Node p = this.head;
while(p!=null)
{
Node r=p;
p=p.nextNode;
r.nextNode=q;
q=r;
}
this.head = q;
}
}
}
}
Here's what I have so far in trying to work it into Node's constructors:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace PalindromeTester
{
class Program
{
static void Main(string[] args)
{
Node myNode = new Node("hello");
Console.Out.WriteLine(myNode.count);
myNode.reverse();
myNode.traverse();
Console.In.ReadLine();
}
class Node
{
private char letter;
private Node next;
private Node head;
private int totalNode;
public Node()
{
head = null;
totalNode = 0;
}
public Node(char c)
{
if (head == null)
{
head = new Node();
head.data = c;
}
else
{
Node temp;
temp = new Node();
temp.data = c;
temp.nextNode = head;
head = temp;
}
totalNode++;
}
public Node(string s)
{
foreach (char x in s)
{
new Node(x);
}
}
public char data
{
get { return letter; }
set { letter = value; }
}
public Node nextNode
{
get { return next; }
set { next = value; }
}
public void reverse()
{
Node q = null;
Node p = this.head;
while (p != null)
{
Node r = p;
p = p.nextNode;
r.nextNode = q;
q = r;
}
this.head = q;
}
public void traverse()
{
Node temp = head;
while (temp != null)
{
Console.Write(temp.data + " ");
temp = temp.nextNode;
}
}
public int count
{
get { return totalNode; }
}
}
}
}
Ideally, the only constructors and methods I would be left with are Node(), Node(char c), Node(string s), Node reserve() and I'll be reworking traverse into a ToString overload.
Any suggestions?