标题:C#链表问题,新手疑问
取消只看楼主
Eumenides_me
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:63
专家分:151
注 册:2012-9-26
结帖率:100%
 问题点数:0 回复次数:5 
C#链表问题,新手疑问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xxx
{
    interface ILinarList<T>
    {
        void InsertNode_End(T a);
        void InsertNode(T a, int x);
        int GetLength();
    }
    class SLinkList<T> : ILinarList<T>
    {
        private SNode<T> start;
        private int length;
        public SLinkList()
        {
            start = null;
            length = 0;
        }
        public void InsertNode_End(T a)
        {
            SNode<T> current;
            if (start == null)
            {
                start = new SNode<T>(a);
                length++;
                return;
            }
            current = start;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = new SNode<T>(a);
            length++;
        }
        public void InsertNode(T a, int x)
        {
            SNode<T> current;
            SNode<T> previous = null ;
            SNode<T> newNode = new SNode<T>(a);
            if (x == 1)
            {
                newNode.Next = start;
                start.Next = newNode;
                length++;
                return;
            }
            int i = 1;
            current = start;
            while (i != x)
            {
                previous = current;
                current = current.Next;
            }
            previous.Next = newNode;
            newNode.Next = current;
            length++;
        }
        public int GetLength()
        {
            return length;
        }
    }
    class SNode<T>
    {
        private T data;
        private SNode<T> next;
        public SNode(T a)
        {
            data = a;
            next = null;
        }
        public T Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
        public SNode<T> Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
    }
    class Stu_Node
    {
        private string stu_ID;
        private string stu_Name;
        private int stu_Score;
        public Stu_Node(string stu_ID, string stu_Name, int stu_Score)
        {
            this.stu_ID = stu_ID;
            this.stu_Name = stu_Name;
            this.stu_Score = stu_Score;
        }
        public override string ToString()
        {
            return stu_ID + stu_Name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ILinarList<SNode<Stu_Node>> stu_list = null;
            char Y_N;
            while (true)
            {
                int choice;
                Console.WriteLine("有以下可执行的操作:");
                Console.WriteLine("1.输入学生信息或在末尾加入学生信息");
                Console.WriteLine("2.在第k个学生前插入学生信息");
                Console.WriteLine("3.在指定学号或姓名的学生前插入学生信息");
                Console.WriteLine("4.删除第k个学生的信息");
                Console.WriteLine("5.删除指定学号或姓名的学生的信息");
                Console.WriteLine("6.查看第k个学生的信息");
                Console.WriteLine("7.查看指定学号或姓名的学生的信息");
                Console.WriteLine("8.显示学生表信息");
                Console.WriteLine("9.查看以注册信息的学生总人数");
                Console.WriteLine("10.清空学生信息表");
                Console.WriteLine("11.退出");
                Console.Write("请选择:");
                try
                {
                    choice = Int32.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("Please enter an integer");
                    continue;
                }

                if (choice > 11 || choice < 1)
                {
                    Console.WriteLine("Beyond the range of choice");
                    Console.Write("Press any key to continue...");
                    Console.Read();
                    continue;
                }
                else
                {
                    switch (choice)
                    {
                        case 1:
                            while (true)
                            {
                                string stu_ID, stu_name;
                                int stu_score;
                                char ch;
                                Console.Write("请输入学生学号:");
                                stu_ID = Console.ReadLine();
                                Console.Write("请输入学生姓名:");
                                stu_name = Console.ReadLine();
                                Console.Write("请输入学生成绩:");
                                try
                                {
                                    stu_score = Int32.Parse(Console.ReadLine());
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("score input errors");
                                    continue;
                                }

                                if (stu_score < 0 || stu_score > 100)
                                {
                                    Console.WriteLine("score overflow");
                                    continue;
                                }
                                Stu_Node stu_Node = new Stu_Node(stu_ID, stu_name, stu_score);
                                stu_list.InsertNode_End(stu_Node);
                                Console.WriteLine("是否继续输入?(Y/N)");
                                ch = char.Parse(Console.ReadLine());
                                if (ch == 'n' || ch == 'N')
                                {
                                    break;
                                }
                            }
                            break;
                        case 2:
                            while (true)
                            {
                                string stu_ID, stu_name;
                                int stu_score;
                                char ch;
                                int k;
                                Console.Write("请问在第多少个学生前插入数据?请输入:");
                                try
                                {
                                    k = Int32.Parse(Console.ReadLine());
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("error,Please enter an integer");
                                    continue;
                                }

                                if (k < 1 || k > stu_list.GetLength())
                                {
                                    Console.WriteLine("the number must between 1 and {0}", stu_list.GetLength());
                                    continue;
                                }
                                Console.Write("请输入学生学号:");
                                stu_ID = Console.ReadLine();
                                Console.Write("请输入学生姓名:");
                                stu_name = Console.ReadLine();
                                Console.Write("请输入学生成绩:");
                                try
                                {
                                    stu_score = Int32.Parse(Console.ReadLine());
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("score input errors");
                                    continue;
                                }

                                if (stu_score < 0 || stu_score > 100)
                                {
                                    Console.WriteLine("score overflow");
                                    continue;
                                }
                                Stu_Node stu_Node = new Stu_Node(stu_ID, stu_name, stu_score);
                                stu_list.InsertNode(stu_Node, k);
                                Console.WriteLine("是否继续插入?(Y/N)");
                                ch = char.Parse(Console.ReadLine());
                                if (ch == 'n' || ch == 'N')
                                {
                                    break;
                                }
                            }
                            break;
                    }
                    Console.WriteLine("是否继续进行操作?(Y/N)");
                    Y_N = char.Parse(Console.ReadLine());
                    if (Y_N == 'n' || Y_N == 'N')
                    {
                        break;
                    }
                }
            }
        }
    }
}

红色子图部分为报错部分,两个地方的错误相同,错误提示如下,绿色字体可以直接忽略
-----------------------------------------------------------------------------------------------------------------------------------------
错误    1    与“xxx.ILinarList<xxx.SNode<xxx.Stu_Node>>.InsertNode_End(xxx.SNode<xxx.Stu_Node>)”最匹配的重载方法具有一些无效参数
错误    2    参数 1: 无法从“xxx.Stu_Node”转换为“xxx.SNode<xxx.Stu_Node>”
-----------------------------------------------------------------------------------------------------------------------------------------

求解...
搜索更多相关主题的帖子: start public private interface void 
2012-09-27 12:54
Eumenides_me
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:63
专家分:151
注 册:2012-9-26
得分:0 
以下是引用ZWYyj在2012-9-27 13:42:53的发言:

错误报告说得很明白。xxx.ILinarList<xxx.SNode<xxx.Stu_Node>>.InsertNode_End(xxx.SNode<xxx.Stu_Node>)最匹配的重载方法具有一些无效参数,即圆括号里的参数错了。
stu_list.InsertNode(stu_Node),你的括号里参数类型为xxx.Stu_Node,而InsertNode_End()要求括号里是xxx.SNode<xxx.Stu_Node>这种类型。所以你要先声明一种SNode<Stu_Node>类的对象,作为括号里的参数。
例如:
SNode<Stu_Node> snode;
````
stu_list.InsertNode(snode);

为什么那个参数的类型会是xxx.SNode<xxx.Stu_Node>?我在借口中定义出入的类型为泛型T。在public void InsertNode_End(T a)方法中,有这样的赋值语句current.Next = new SNode<T>(a);a的类型是xxx.Stu_Node,SNode<T>中的泛型T对应的类型也是xxx.Stu_Node,看不出来是什么问题?能否详解一下,谢谢了哈
2012-09-27 14:04
Eumenides_me
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:63
专家分:151
注 册:2012-9-26
得分:0 
以下是引用ZWYyj在2012-9-27 14:28:52的发言:

InsertNode_End(xxx.SNode<xxx.Stu_Node>)不要搞混了,你使用了两次泛型定义。

两次泛型定义?一个是class SNode<T>?另一个是class SLinkList<T> : ILinarList<T>?我的本意是让泛型T代表xxx.Stu_Node,应该如何修改呢?泛型T指代xxx.SNode<xxx.Stu_Node>是什么时候发生的?
2012-09-27 15:03
Eumenides_me
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:63
专家分:151
注 册:2012-9-26
得分:0 
以下是引用ZWYyj在2012-9-27 15:36:39的发言:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xxx
{
    interface ILinarList<T>
    {
        void InsertNode_End(T a);
        void InsertNode(T a, int x);
        int GetLength();
    }
    class SLinkList<T> : ILinarList<T>
    {
        private SNode<T> start;
        private int length;
        public SLinkList()
        {
            start = null;
            length = 0;
        }
        public void InsertNode_End(T a)
        {
            SNode<T> current;
            if (start == null)
            {
                start = new SNode<T>(a);
                length++;
                return;
            }
            current = start;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = new SNode<T>(a);
            length++;
        }
        public void InsertNode(T a, int x)
        {
            SNode<T> current;
            SNode<T> previous = null;
            SNode<T> newNode = new SNode<T>(a);
            if (x == 1)
            {
                newNode.Next = start;
                start.Next = newNode;
                length++;
                return;
            }
            int i = 1;
            current = start;
            while (i != x)
            {
                previous = current;
                current = current.Next;
            }
            previous.Next = newNode;
            newNode.Next = current;
            length++;
        }
        public int GetLength()
        {
            return length;
        }
    }
    class SNode<T>
    {
        private T data;
        private SNode<T> next;
        public SNode(T a)
        {
            data = a;
            next = null;
        }
        public T Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
        public SNode<T> Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
    }
    class Stu_Node
    {
        private string stu_ID;
        private string stu_Name;
        private int stu_Score;
        public Stu_Node(string stu_ID, string stu_Name, int stu_Score)
        {
            this.stu_ID = stu_ID;
            this.stu_Name = stu_Name;
            this.stu_Score = stu_Score;
        }
        public override string ToString()
        {
            return stu_ID + stu_Name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            SLinkList<SNode<Stu_Node>> stu_list = new SLinkList<SNode<Stu_Node>>();//你这个地方原本用的接口,应换为类,然后实例化一
                                                                                  //个对象。接口不能实例化。
            SNode<Stu_Node> snode = null;//这里要声明一个SNode类的对象。即代替SLinkList<T>里面T,作为之前提到的圆括号里的参数类型
            char Y_N;
            while (true)
            {
                int choice;
                Console.WriteLine("有以下可执行的操作:");
                Console.WriteLine("1.输入学生信息或在末尾加入学生信息");
                Console.WriteLine("2.在第k个学生前插入学生信息");
                Console.WriteLine("3.在指定学号或姓名的学生前插入学生信息");
                Console.WriteLine("4.删除第k个学生的信息");
                Console.WriteLine("5.删除指定学号或姓名的学生的信息");
                Console.WriteLine("6.查看第k个学生的信息");
                Console.WriteLine("7.查看指定学号或姓名的学生的信息");
                Console.WriteLine("8.显示学生表信息");
                Console.WriteLine("9.查看以注册信息的学生总人数");
                Console.WriteLine("10.清空学生信息表");
                Console.WriteLine("11.退出");
                Console.Write("请选择:");
                try
                {
                    choice = Int32.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("Please enter an integer");
                    continue;
                }
                if (choice > 11 || choice < 1)
                {
                    Console.WriteLine("Beyond the range of choice");
                    Console.Write("Press any key to continue...");
                    Console.Read();
                    continue;
                }
                else
                {
                    switch (choice)
                    {
                        case 1:
                            while (true)
                            {
                                string stu_ID, stu_name;
                                int stu_score;
                                char ch;
                                Console.Write("请输入学生学号:");
                                stu_ID = Console.ReadLine();
                                Console.Write("请输入学生姓名:");
                                stu_name = Console.ReadLine();
                                Console.Write("请输入学生成绩:");
                                try
                                {
                                    stu_score = Int32.Parse(Console.ReadLine());
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("score input errors");
                                    continue;
                                }
                                if (stu_score < 0 || stu_score > 100)
                                {
                                    Console.WriteLine("score overflow");
                                    continue;
                                }
                              
                                Stu_Node stu_Node = new Stu_Node(stu_ID, stu_name, stu_score);
                                snode = new SNode<Stu_Node>(stu_Node);//实例化的SNode类
                                stu_list.InsertNode_End(snode);//圆括号里的参数类型为SNode类型
                                Console.WriteLine("是否继续输入?(Y/N)");
                                ch = char.Parse(Console.ReadLine());
                                if (ch == 'n' || ch == 'N')
                                {
                                    break;
                                }
                            }
                            break;
                        case 2:
                            while (true)
                            {
                                string stu_ID, stu_name;
                                int stu_score;
                                char ch;
                                int k;
                                Console.Write("请问在第多少个学生前插入数据?请输入:");
                                try
                                {
                                    k = Int32.Parse(Console.ReadLine());
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("error,Please enter an integer");
                                    continue;
                                }
                                if (k < 1 || k > stu_list.GetLength())
                                {
                                    Console.WriteLine("the number must between 1 and {0}", stu_list.GetLength());
                                    continue;
                                }
                                Console.Write("请输入学生学号:");
                                stu_ID = Console.ReadLine();
    ...


非常感谢你的回答,但是我看着还是感觉别扭啊,你给我纠正的那个snode = new SNode<Stu_Node>(stu_Node);这里调用了SNode方法,传入的实参是Stu_Node类型的,但是在public void InsertNode_End(T a)类中,有这样的一个语句start = new SNode<T>(a);这里也调用了SNode方法,但是传入的实参却是SNode类型的
2012-09-27 23:17
Eumenides_me
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:63
专家分:151
注 册:2012-9-26
得分:0 
以下是引用ZWYyj在2012-9-28 09:21:54的发言:

我觉得你对于C#类的概念、泛型的概念还不太熟,好好看看吧。。。

额、好吧,谢谢了哈
2012-09-28 12:28
Eumenides_me
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:63
专家分:151
注 册:2012-9-26
得分:0 
我知道你说我用了两次定义的地方了,[ILinarList<SNode<Stu_Node>> stu_list = null;]这里我将SNode<Stu_Node>定义成泛型T了,而[stu_list.InsertNode_End(stu_Node);]这里我又将stu_Node定义成泛型T了,所以会出现那种错误提示。只需要将[ILinarList<SNode<Stu_Node>> stu_list = null;]改成[ILinarList<Stu_Node> stu_list = null;]就行了。
2012-09-29 15:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-382196-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.172815 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved