简体   繁体   English

重做我的单链列表

[英]Reworking my singly linked list

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). 在下面,您将找到我可以使用的SLL,但我想更多地使用C#,而不是要使用SLL和Node这两个类,而要使用Node的构造函数来完成所有工作(如果将字符串传递给节点,构造函数会将其分成char节点)。 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: 到目前为止,这是我尝试将其用于Node的构造函数中的内容:

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. 理想情况下,剩下的唯一构造函数和方法是Node(),Node(char c),Node(string s),Node reserve(),并且我将遍历遍历为ToString重载。

Any suggestions? 有什么建议么?

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. 如果您希望链表将一个字符串切成几个节点,则请确保该方法实际上包含在LL中,而不是包含在节点对象中。 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. 您的char-linked-list应该有两种方法:一种是添加一个char,另一种是添加将被分割成多个节点的字符串。 The node object itself should only have a single constructor that takes a char. 节点对象本身应该只有一个带有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. 一个用来跟踪节点的节点对象。

Assuming this is just a learning exercise (there is no other reason to write such a class), the totalNode field looks very suspect. 假设这只是一个学习练习(没有其他原因可以编写这样的类), totalNode字段看起来非常可疑。 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? 它的值为0或1。是否应该是从给定节点开始的节点数的计数? 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. 并且在Node的构造函数中, head字段将始终为null ,因此if是多余的。

Maybe you should try running the code and then updating your question! 也许您应该尝试运行代码,然后更新您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM