为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
Why is it that, when I try to search for data that is beyond the first data entry in my double linked list, the program crashes?
对于我的作业,我应该创建一个双链表,并在它旁边有搜索、插入和删除功能。在开发搜索功能时,我遇到了一个错误,如果有人使用无法与链表中任何内容匹配的键进行搜索,程序将崩溃 - 打印链表第一个数据所在的内存地址。但是,除此之外,我还遇到了另一个错误,如果我要搜索第一个数据条目之外的任何数据,程序将崩溃 - 打印第一个条目的内存地址。
这些问题源于这样一个事实,即我的搜索函数没有正确迭代整个列表,但只能毫无问题地读取第一个条目。因此,如果我尝试访问超出第一个条目的任何数据,整个程序都会崩溃。我知道这最终是问题所在,但我对我做了什么导致了它以及我应该如何解决它感到困惑。
标题.h:
#pragma once
#ifndef Header_H
#define Header_H
#include "stdafx.h"
#include<iostream>
#include <string>
template<typename T>
struct Node {
T record;
Node<T> *next;
Node<T> *prev;
Node() {
next = NULL;
prev = NULL;
}
};
template<class T>
class datax {
Node<T> *top;
Node<T> *bot;
public:
datax() {
top = NULL;
bot = NULL;
}
~datax() {}
//Searches the list that has a requested key word
void find();
//Inserts into the list a requested word
void insert();
//Searches the list for a requested key word and pops it out
void Delete();
void show();
private:
};
template<typename T>
void datax<T>::show() {
Node<T> *temp = top;
while (temp != NULL) {
cout << temp->record << "";
temp = temp->next;
}
}
template<typename T>
void datax<T>::Delete() {
T get;
Node<T>*ser = top;
cin >> get;
while (ser != NULL && ser->record != get) {
ser = ser->next;
}
if (ser != NULL && ser->record == get) {
cout << "Removing data from " << ser << endl;
ser->next->prev = ser->prev;
delete ser;
}
else {
cout << "Data not found" << endl;
}
}
template<typename T>
void datax<T>::insert() {
T val;
Node<T> *ptr = new Node<T>;
cin >> val;
ptr->record = val;
ptr->next = NULL;
if (bot != NULL) {
bot->next = ptr;
ptr->prev = bot;
}
if (top == NULL) {
top = ptr;
}
bot = ptr;
}
template<typename T>
void datax<T>::find() {
T get;
Node<T>*ptr= top;
cin >> get;
if (ptr == NULL) {
cout << "Data not found" << endl;
}
else {
while (top != NULL && ptr->record != get) {
top->next = ptr;
}
if (ptr != NULL && ptr->record == get) {
cout << "Found: " << ptr << endl;
}
else {
cout << "Data not found" << endl;
}
}
}
#endif // !Header_H
主要:
// LinkedList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include <string>
#include"Header.h"
using namespace std;
int main()
{
int take;
datax<int> m;
while (true) {
cout << "nn1. Insertnn2.Findnn3.Delete" << endl;
cin >> take;
switch (take) {
case 1:
m.insert();
m.show();
break;
case 2:
m.find();
break;
case 3:
m.Delete();
m.show();
break;
default:
cout << "Error" << endl;
}
}
return 0;
}
看看你的搜索循环:
while (top != NULL && ptr->record != get) {
top->next = ptr;
}
现在,你的条件是你将继续循环,直到top
NULL
或ptr->record
是你要找的。但是在你的环形身体里,top
会改变吗?ptr
会改变吗??不!
换句话说,如果你第一次不退出这个循环,你永远不会。
您可能打算让它看起来像:
while (ptr != NULL && ptr->record != get) {
ptr = ptr->next;
}
或者,如果您感觉很花哨并想要一个单行(IMO 仍然可读(:
for(ptr = top; ptr && ptr->record != get; ptr = ptr->next) { }
相关文章:
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 我的程序没有从文件中读取并输入数据
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 员工测试驱动程序数据结构
- 如何在不破坏现有应用程序的情况下更改 API 中 stl 容器的数据类型?
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- 在 1 个服务器 n 客户端套接字 C++ MFC 应用程序中更新数据的客户端
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 将数据从C++程序发布到弹性搜索
- 数据库连接并将数据从C ++程序保存到MySQL
- boost::asio 不会触发读取处理程序,而 Wireshark 会看到传入的数据
- C++,在对象内分配多个数据时,堆栈分配是否更有效? 在下面的程序中,类A_Heap的效率会更低吗?
- 更改应用程序数据的路径
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?
- 如何安装性能数据帮助程序
- 使用 pipe() 和 fdopen() 将数据从 Python 脚本传递到 Windows 中的C++应用程序
- 需要解决在Storm数据程序中读取Makefile的问题