链接列表删除功能卡住了 [英] Linked list delete function stucks

查看:81
本文介绍了链接列表删除功能卡住了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学生记录制作小代码,我希望通过regNo删除记录。但当我实现它卡住的功能,程序显示错误报告!我缺少的地方!还告诉我,排序和遍历同样的事情吗?



我尝试过:



i am making small code for student record like thing and i want to delete a record by the regNo. but when i implement the function it stuck and program shows error report !! where i am lacking !! also tell me, is sorting and traversing same thing ?

What I have tried:

struct student
{
	string name;
	int regno;
	int age;
	double GPA;
	int pono;

	student() {}
	~student() {}
	struct student *next;
};




void deleteRecord()
{
	cout << endl;
	cout << "Delete record:" << endl;
	cout << endl;
	student *del;
	del = head;
	del = del->next;
	if (del->next == NULL)
		cout << "End of the list." << endl;
	else
		del = del->next;
	if (del == head)
		cout << "Beginning of list." << endl;
	else
	{
		student *prev;
		prev = head;
		while (prev->next != del)
		{
			prev = prev->next;
		}
		del = prev;
	}
	cout << endl;
	cout << "RegNo to delete: ";
	cin >> del->regno;

	if (del->next == NULL)
		cout << "Nothing follows." << endl;
	else
	{
		student *temp;
		temp = del->next;
		del->next = temp->next;
		delete temp;
	}
}




int main()
{
deleterecord()}

推荐答案

从问题的结尾开始:

Starting from the end of your question:
引用:

是排序和遍历相同的东西?

is sorting and traversing same thing ?

没有。

排序是对节点进行排序的过程,使它们按特定顺序排列。如果你有四个节点:

No.
Sorting is the process of ordering nodes so they are in a particular sequence. If you have four nodes:

4 7 3 1

然后排序它们将成为:

Then sorted they would become:

1 3 4 7



Or

7 4 3 1

如果按降序排序。

遍历是一个简单的过程,即从头到尾遍历每个节点依次访问每个节点但不改变任何内容或位置。

<回到你的主要问题:我们不知道你遇到了什么错误,但修复它实际上是你的任务的一部分。

所以使用调试器和跟进你的代码正在做什么。

这是你的功课的一部分,让代码正常工作是任务的一部分:实际上,它通常是最有趣和最耗时的一点!



所以在函数的第一行放置一个断点,然后逐步完成代码,预先解决应该发生的事情。将其与发生的事情相比较,当它们不匹配时,您将开始了解原因。您可以使用调试器查看变量内容,逐行逐步执行代码,对于某些系统,您甚至可以在代码运行时编辑代码!



但我们不知道您使用的系统和IDE,因此我们无法给出明确的指示。谷歌的调试器和你的编译系统的名称,你应该至少找到有关如何使用它的基本说明。

If sorted in descending order.
Traversing is the simpler process of "walking through" each node from the head to the tail visiting each node in turn but not changing the content or position of any.

Going back to your "main" problem: We don't know what error you are experiencing, but fixing it is actually part of your assignment.
So use the debugger and follow through what your code is doing.
This is part of your homework, and getting the code to work correctly is part of the task: indeed, it's often the bit that is most interesting and time consuming!

So put a breakpoint on the first line of the function, and step through the code working out in advance what should be happening. Compare that with what did happen and when they don't match you will start to get an idea as to why. You can use the debugger to look at variable contents, as well as stepping through your code line-by-line, and for some systems you can even edit the code while it is running!

But we don't know what system and IDE you are using, so we can't give explicit instructions. Google for "debugger" and the name of your compilation system and you should find at least basic instructions on how to use it.


从你的代码,列表已经是空的。



使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。如果代码没有按预期执行,则表示您接近错误。
From your code, the list is already empty.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


如果要删除具有特定 regNo 你必须找到该记录,调整上一条记录的 next 成员,并将其删除。但是你的代码没有这样做。



从头开始的一个未经测试的例子:

If you want to delete the record with a specific regNo you have to find that record, adjust the next member of the previous record, and delete it. But your code is not doing that.

An untested example from scratch:
void deleteRecord(int regNo)
{
    student *prev = head;
    student *temp = head;
    // Find the element.
    while (temp && temp->regNo != regNo)
    {
        prev = temp;
        temp = temp->next;
    }
    // Element has been found.
    if (temp)
    {
        // Adjust head when deleting the first element
        if (temp == head)
            head = temp->next;
        // Adjust next of previous element.
        prev->next = temp->next;
        delete temp;
    }
}


这篇关于链接列表删除功能卡住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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