我如何解决他的linklistforstudent?我被卡住了 [英] How do I solve he linkedlistforstudent? I'm stuck

查看:80
本文介绍了我如何解决他的linklistforstudent?我被卡住了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出linkListForStudents的三个错误,但我被困在第126,152和168行。你能帮忙吗?



我尝试过:



I've been trying to figure out the three errors for the linkedListForStudents, but I am stuck on line 126, 152 and 168. Can you please help?

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace p6_linkedListForStudents
{
    public class Node
    {
        public struct student
        {
            public int id;
            public string name;
            public float GPA;
        }
        public student item;
        public Node link;
        public Node(student theItem)
        {
            item = theItem;
            link = null;
        }
    }
    public class LinkedList
    {
        public Node header;
        public LinkedList()
        {
            header = null;
        }
        public Node Search(int key)
        {
            Node current = header;
            while (current != null && current.item.id != key) //.id after item
                current = current.link;
            return current;
        }
        public void Append(Node.student newItem)
        {
            Node newNode = new Node(newItem);
            newNode.link = header;
            header = newNode;
        }
        public Node Remove()
        {
            Node x = header;
            if (header != null)
                header = header.link;
            return x;
        }
        public Node searchPrevious(int key)
        {
            if (header == null)
                return header;
            else
            {
                Node current = header;
                while (!(current.link == null) && (current.link.item.id != key))//.id after item
                    current = current.link;
                return current;
            }
        }
        public void Insert(Node.student newItem, int preKey) //student type
        {
            Node current;
            Node newNode = new Node(newItem);
            current = Search(preKey);
            if (current == null)
                Console.WriteLine("there is no such preKey!");
            else
            {
                newNode.link = current.link;
                current.link = newNode;
            }
        }

        public void Delete(int key)
        {
            if (header == null) /* The list is empty! */
                Console.WriteLine("The list is empty!");
            else
            {
                if (header.item.id == key) /* The only node in the list is to be deleted. */
                    header = header.link;
                else /* The list has more than one node. */
                {
                    Node p = searchPrevious(key);
                    if (p.link == null) Console.WriteLine("There is no such item!");
                    else p.link = p.link.link;
                }
            }
        }
        public void PrintList()
        {
            if (header == null)
                Console.WriteLine("The list is empty!");
            else
            {
                Node current = header;
                Console.WriteLine(current.item.id);
                while (!(current.link == null))
                {
                    current = current.link;
                    Console.WriteLine(current.item.id);
                    Console.WriteLine(current.item.name);
                    Console.WriteLine(current.item.GPA);
                }
            }
        }
    }
    static class linkedListApplication
    {
        static void Main()
        {
            int Key, preKey; //student type
            int NewItem;
            //Node.student Student;
            /* create an empty linked list */
            LinkedList LL = new LinkedList();
            /* create a linked list of 5 nodes */
            Random rnd = new Random(100);
            Console.WriteLine("The following 5 items/integers will be appended into the linked list:");
            for (int i = 0; i <= 4; i++)
            {
                NewItem = rnd.Next() * 100;//.id after item, .name
                LL.Append(NewItem);
                Console.WriteLine(NewItem + " ");
            }
            /* print the list */
            Console.WriteLine("The following are the items/integers in the current linked list from the header:");
            LL.PrintList();
            Console.WriteLine("Enter 1 for search, 2 for insertion, 3 for deletion, 4 for append, 5 for remove");
            int s = int.Parse(Console.ReadLine());
            while (s == 1 || s == 2 || s == 3 || s == 4 || s == 5)
            {
                if (s == 1)
                {
                    Console.WriteLine("Enter an key/integer that you want to search:");
                    Key = int.Parse(Console.ReadLine());
                    Node n = LL.Search(Key);
                    if (n != null)
                        Console.WriteLine("The item/integer is found: {0}", n.item);
                    else
                        Console.WriteLine("there is no such key!");
                };
                if (s == 2)
                {
                    Console.WriteLine("Enter a new item/integer that you want to insert:");
                    NewItem = int.Parse(Console.ReadLine());
                    Console.WriteLine("Enter the preKey/integer that the new item will be inserted after it:");
                    preKey = int.Parse(Console.ReadLine());
                    LL.Insert(NewItem, preKey);
                    Console.WriteLine("The items/integers of the current linked list from the header:");
                    LL.PrintList();
                };
                if (s == 3)
                {
                    Console.WriteLine("Enter the key/integer of the item that you want to delete:");
                    Key = int.Parse(Console.ReadLine());
                    LL.Delete(Key);
                    Console.WriteLine("The items/integers in the current linked list from the header.");
                    LL.PrintList();
                };
                if (s == 4)
                {
                    Console.WriteLine("Enter the item/integer that you want to append:");
                    NewItem = int.Parse(Console.ReadLine());
                    LL.Append(NewItem);
                    Console.WriteLine("The items/integers in the current linked list from the header");
                    LL.PrintList();
                };
                if (s == 5)
                {
                    Node RemoveNode = LL.Remove();
                    if (RemoveNode != null)
                    {
                        Console.WriteLine("The removed item is: {0}", RemoveNode.item);
                        Console.WriteLine("The items/integers in the current linked list from the header");
                        LL.PrintList();
                    }
                    else
                        Console.WriteLine("The linked list is empty!");
                };
                Console.WriteLine("\n");
                Console.WriteLine("Enter 1 for search, 2 for insertion, 3 for deletion, 4 for append, 5 for remove");
                s = int.Parse(Console.ReadLine());
            }
        }
    }
}

推荐答案

当您尝试报告问题时只是说我被困在第126,152和168行并不是特别有帮助。

告诉我们它是什么行,告诉我们错误信息是什么(如果有的话,当你得到的时候)它。)



如果你不这样做,我们必须尝试找到这些线,猜猜你可能会遇到什么错误...



所以我加载你的代码,并使用CTRL + G转到行号。

第126行是这一行:

When you try to report problems just saying "I am stuck on line 126, 152 and 168" isn't particularly helpful.
Show us what line it is, tell us what the error message (if any is, and when you get it.)

If you don't we have to try and find the lines, and guess what errors you might be getting...

So I load your code, and use CTRL+G to go to the line number.
Line 126 is this one:
Console.WriteLine(NewItem + " ");

除了它有点无意义之外没有我能看到的错误 - 实际上它与此相同:

Which has no error that I can see, other than "it's a little pointless" - in practical terms it's identical to this:

Console.WriteLine(NewItem);



第152行是:


Line 152 is this:

Console.WriteLine("The items/integers of the current linked list from the header:");

哪个没有错误。

第168行是这样的:

Which has no errors.
Line 168 is this:

Console.WriteLine("The items/integers in the current linked list from the header");

这又没有错误。

如果是上面的行,则所有这些都涉及 LL 未在该代码中的任何地方声明,因此我们不知道它可能是什么......

Which again has no error.
If it's the lines above them then those all involve LL which isn't declared anywhere in that code so we have no idea what it might be...


这篇关于我如何解决他的linklistforstudent?我被卡住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆