马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这段代码为什么在输入:
3
1
之后在打印"It in"后直接停止,没有打印出错信息?
main.cpp#include "all.h"
#define NOT_FOUND -1
#define INVALID_INDEX -2
#define NODE_CREATION_FAILURE -3
#define INVALID_ORDER -4
using namespace std;
struct Node{
int value = 0;
Node *nextv = NULL;
};
int length = 0;
Node *pHead = NULL, *pEnd = NULL;
void Create(){
Node *pNow = NULL;
pNow = (Node *)malloc(sizeof(Node));
if(pNow == NULL){
throw NODE_CREATION_FAILURE;
}
pNow -> value = 0;
pNow -> nextv = NULL;
pHead = pNow;
length++;
}
void add(int index, int value){ // 在第index个节点前插入
Node *pTN = pHead, *pTn = pHead -> nextv, *pNow = NULL;
pNow = (Node *)malloc(sizeof(Node));
if(pNow == NULL){
throw NODE_CREATION_FAILURE;
}
pNow -> value = value;
pNow -> nextv = NULL;
if(index < 0){
throw INVALID_INDEX;
}
if(index == 0){
pNow -> nextv = pHead;
pHead = pNow;
}
index--;
for(int i = 0;i < index;i++){ // 找到第index个节点
if(i > length){
break;
}
pTn = pTn -> nextv;
pTN = pTN -> nextv;
}
pNow -> nextv = pTn;
pTN -> nextv = pNow;
length++;
}
int del(int index){
Node *pTN = pHead, *pTn = pHead -> nextv;
int r;
if(index < 0){
throw INVALID_INDEX;
}
if(index == 0){
pHead = pTN -> nextv;
r = pTN -> value;
free(pTN);
return r;
}
index--;
for(int i = 0;i < index;i++){
if(i > length){
break;
}
pTn = pTN;
pTN = pTN -> nextv;
}
r = pTn -> value;
pTN -> nextv = pTn -> nextv;
free(pTn);
length--;
return r;
}
int show(int index){
Node *pTN = pHead;
for(int i = 0;i < index;i++){
if(i > length){
throw INVALID_INDEX;
}
pTN = pTN -> nextv;
}
return pTN -> value;
}
int find(int value){
Node *pTN = pHead;
int index = 0;
for(int i = 0;;i++, index++){
if(pTN -> value == value){
return index;
}
if(i > length){
cout << "NOT FOUND" << endl;
throw NOT_FOUND;
}
pTN = pTN -> nextv;
}
}
void print(){ // 打印链表
Node *pTN = pHead;
for(int i = 0;;){
cout << (pTN -> value) << endl;
sleep(1);
if(i > length || pTN -> nextv == NULL){
break;
}
pTN = pTN -> nextv;
i++;
}
}
void printx(){
Node *pTN = pHead, *pTn = NULL;
for(int i = 0;;i++){
cout << (pTN -> value) << endl;
sleep(1);
if(i > length || pTN -> nextv == NULL){
break;
}
pTn = pTN -> nextv;
free(pTN);
pTN = pTn;
}
}
int main(){
char inu[6];
int ind, val;
Create();
while(1){
cout << "Please enter a order(1.add node, 2.delete node, 3.find, 4.show, 5.print, 6.length, (e\\q).exit):";
cin >> inu;
system("cls");
try{
switch (inu[0]) {
case '1':
cout << "Please enter the value:";
cin >> val;
cout << "Please enter the index:";
cin >> ind;
add(ind, val);
break;
case '2':
cout << "Please enter the index:";
cin >> ind;
cout << "The value is " << del(ind) << endl;
break;
case '3':
cout << "Please enter the find value:";
cin >> val;
cout << "It in " << find(val) << endl;
break;
case '4':
cout << "Please enter the index:";
cin >> ind;
cout << "The value is " << show(ind) << endl;
break;
case '5':
print();
break;
case '6':
cout << length << endl;
break;
case 'q':
case 'e':
printx();
putchar('m');
return 0;
default:
if(strcmp(inu, "Bleen")){
cout << "Error: invalid index!" << endl;
sleep(5);
}else{
throw INVALID_ORDER;
}
break;
}
}
catch(int error){
switch(error){
case NOT_FOUND:
cout << "Error: not found!" << endl; // 应该执行这条语句
break;
case INVALID_INDEX:
cout << "Error: invalid index!" << endl;
break;
case NODE_CREATION_FAILURE:
cout << "Error: node creation failure!" << endl;
break;
case INVALID_ORDER:
cout << "Error: invalid order!" << endl;
break;
}
}catch(...){
cout << "Error: unknown error!" << endl;
}
}
putchar('n');
return -1;
}
all.h#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fstream>
这个问题可能是由于在你的 `find` 函数中的循环条件出现了问题。在你的代码中,你的 `find` 函数如下:
int find(int value){
Node *pTN = pHead;
int index = 0;
for(int i = 0;;i++, index++){
if(pTN -> value == value){
return index;
}
if(i > length){
cout << "NOT FOUND" << endl;
throw NOT_FOUND;
}
pTN = pTN -> nextv;
}
}
在这个函数中,你的循环条件是一个无限循环 `for(int i = 0;;i++, index++)`,然后你在循环体内部通过 `if(i > length)` 来判断是否应该结束循环。但是,这个判断条件可能会导致问题。因为在你的代码中,`length` 是链表的长度,而 `i` 是循环的次数,它们的初始值都是0。当你在链表中添加一个节点后,`length` 会增加1,但是 `i` 仍然从0开始。所以,当你的链表只有一个节点时,`i` 会先于 `length` 增加,导致 `if(i > length)` 条件成立,然后抛出 `NOT_FOUND` 异常。
为了解决这个问题,你应该将 `if(i > length)` 改为 `if(i >= length)`。这样,当 `i` 和 `length` 都为0时,不会抛出 `NOT_FOUND` 异常,只有当 `i` 真正超过 `length` 时,才会抛出异常。这样就可以正确地处理只有一个节点的情况了。
另外,你的 `find` 函数在遍历链表时,没有检查当前节点是否为 `NULL`。如果链表为空或者你试图访问链表的尾部之后的节点,`pTN -> value` 和 `pTN -> nextv` 将会导致未定义的行为。你应该在访问 `pTN -> value` 和 `pTN -> nextv` 之前,检查 `pTN` 是否为 `NULL`。
|