Yetco Ответов: 1

Как мне решить he linkedlistforstudent? Я застрял


Я пытался вычислить три ошибки для linkedListForStudents, но застрял на строках 126, 152 и 168. Не могли бы вы мне помочь?

Что я уже пробовал:

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());
            }
        }
    }
}

1 Ответов

Рейтинг:
2

OriginalGriff

Когда вы пытаетесь сообщить о проблемах, просто сказать: "я застрял на линии 126, 152 и 168" не особенно полезно.
Покажите нам, что это за строка, скажите нам, что это за сообщение об ошибке (если таковое имеется) и когда вы его получите.)

Если вы этого не сделаете, мы должны попытаться найти линии и угадать, какие ошибки вы можете получить...

Поэтому я загружаю ваш код и использую CTRL+G, чтобы перейти к номеру строки.
Строка 126 это одна из них:

Console.WriteLine(NewItem + " ");
В котором нет никакой ошибки, которую я могу видеть, кроме "это немного бессмысленно" - в практическом плане это идентично этому:
Console.WriteLine(NewItem);

Строка 152 это:
Console.WriteLine("The items/integers of the current linked list from the header:");
В котором нет ошибок.
Строка 168 это:
Console.WriteLine("The items/integers in the current linked list from the header");
Что опять же не имеет ошибки.
Если это линии над ними, то все они связаны LL который нигде не объявлен в этом коде, поэтому мы понятия не имеем, что это может быть...