tags:

views:

171

answers:

2

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?

A: 

Assuming this is just a learning exercise (there is no other reason to write such a class), the totalNode field looks very suspect. It will either have the value 0 or 1. Is it suppose to be the count of the number of nodes starting from a given node? How about if you reverse the list?

And in the constructor of Node, the head field will always be null, so the if is redundant.

Maybe you should try running the code and then updating your question!

Daniel Earwicker
+1  A: 

If you want your linked list to be able to chop up a string into several nodes, then make sure the method to do so is actually contained on the LL and not on the node object. In theory you're never supposed to know about the node object unless you're looking for the next/previous value.

Your char-linked-list should have two methods: One to add a char, and one to add a string that will be chopped up into several nodes. The node object itself should only have a single constructor that takes a char.

Basically trying to rewrite the data structure to only consist of a single class is a really bad idea. You really need at least two objects. One to keep track of the nodes, and a node object to be kept track of.

Qua
Thank you for the advice. I guess I will just stick to the two object way of doing things. I've already got it working while reading a text file so... no more help needed! Case closed =]
Stradigos