我无法在链表 class 中分配结构节点的变量。当我将它们分配给任何东西时,它会产生未定义的行为
I can't assign the struct node's variables in linked list class. when I assign them to anything it creates undefined behaviour
//method for printing list
void printList(const ReservationList& ls){
for(int i = 0; i < ls.getLength(); i++){ std::cout<<"ls["<<i<<"]== "<<i+1<<ls.retrieve(i,i+1)<<std::endl; }
}
//main method
int main(){
ReservationList r;
std::cout<<"program starts!"<<std::endl;
std::cout<<"before printing empty list"<<std::endl;
printList(r);
std::cout<<"after printing empty list"<<std::endl;
std::cout<<"inserting starts!"<<std::endl;
r.insert(0,1);
std::cout<<"after inserting 1"<<std::endl;
r.insert(0,2);
std::cout<<"after inserting 2"<<std::endl;
r.insert(0,3);
std::cout<<"after inserting 3"<<std::endl;
r.insert(0,4);
std::cout<<"after inserting 4"<<std::endl;
r.insert(0,5);
std::cout<<"after inserting 5"<<std::endl;
printList(r);
return 0;
}
这是 ReservationList 的头部 class(ReservationList.h)
#ifndef RESERVATION_H
#define RESERVATION_H
#include <iostream>
class ReservationList {
public:
ReservationList();/*
ReservationList( const ReservationList& aList );
~ReservationList();*/
bool isEmpty() const;
int getLength() const ;
bool retrieve(int index, int resCode) const;
bool insert(int index, int resCode);
bool remove(int index);
private:
struct ReservationNode {
int Code;
ReservationNode* next;
};
int size;
ReservationNode *head;
ReservationNode *find(int index) const;
};
#endif
这些是我到目前为止调用的构造函数和插入方法
//empty constructor
ReservationList::ReservationList() {
head = NULL;
size = 0;
}
//insert method
bool ReservationList::insert(int index, int resCode) {
if(index < 0 || index > size) { return 0; }
//making node to be added
ReservationNode* tmp;
std::cout<<"inside insert 1"<<std::endl;
tmp->Code = resCode;/*mistake is hear */
std::cout<<"inside insert 2"<<std::endl;
tmp->next = NULL;
std::cout<<"inside insert 3"<<std::endl;
if ( (index == 0) && (size == 0) ) {
std::cout<<"inside insert 4"<<std::endl;
head = tmp;
size++;
return 1;
}
else if ( (index == 0) && (size == 1) ){
tmp->next = head;
head = tmp;
size++;
return 1;
}
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
tmp->next = curr;
prev->next = tmp;
size++;
return 1;
}
这是输出
program starts!
before printing empty list
after printing empty list
inserting starts!
inside insert 1
[Done] exited with code=3221225477 in 0.622 seconds
使用“std::cout”我跟踪了插入方法的 tmp->Code = resCode;
部分的错误
问题出在 std::cout<<"inside insert 1"<<std::endl;
之后,但是当我评论 tmp->Code = resCode;
时,它在紧接着的那一行给出了错误。据我了解,访问结构中的变量或分配它们存在问题。
这段代码
ReservationNode* tmp;
std::cout<<"inside insert 1"<<std::endl;
tmp->Code = resCode;/*mistake is hear */
std::cout<<"inside insert 2"<<std::endl;
tmp->next = NULL;
调用未定义的行为,因为指针 tmp
未初始化且未指向 ReservationNode
.
类型的有效对象
您似乎忘记调用运算符 new
来创建 ReservationNode
.
类型的对象
同时调用函数find
两次
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
效率低下。
函数定义更简单,无需重复代码。
bool ReservationList::insert( int index, int resCode )
{
bool success = not ( index < 0 || index > size );
if ( success )
{
ReservationNode **current = &head;
while ( index-- )
{
current = &( *current )->next;
}
ReservationNode *new_node = new ReservationNode { resCode, *current };
*current = new_node;
++size;
}
return success;
}
注意如果数据成员size
是无符号整数类型size_t
而不是有符号整数类型int
会更好。同样对函数参数index
.
有效
//method for printing list
void printList(const ReservationList& ls){
for(int i = 0; i < ls.getLength(); i++){ std::cout<<"ls["<<i<<"]== "<<i+1<<ls.retrieve(i,i+1)<<std::endl; }
}
//main method
int main(){
ReservationList r;
std::cout<<"program starts!"<<std::endl;
std::cout<<"before printing empty list"<<std::endl;
printList(r);
std::cout<<"after printing empty list"<<std::endl;
std::cout<<"inserting starts!"<<std::endl;
r.insert(0,1);
std::cout<<"after inserting 1"<<std::endl;
r.insert(0,2);
std::cout<<"after inserting 2"<<std::endl;
r.insert(0,3);
std::cout<<"after inserting 3"<<std::endl;
r.insert(0,4);
std::cout<<"after inserting 4"<<std::endl;
r.insert(0,5);
std::cout<<"after inserting 5"<<std::endl;
printList(r);
return 0;
}
这是 ReservationList 的头部 class(ReservationList.h)
#ifndef RESERVATION_H
#define RESERVATION_H
#include <iostream>
class ReservationList {
public:
ReservationList();/*
ReservationList( const ReservationList& aList );
~ReservationList();*/
bool isEmpty() const;
int getLength() const ;
bool retrieve(int index, int resCode) const;
bool insert(int index, int resCode);
bool remove(int index);
private:
struct ReservationNode {
int Code;
ReservationNode* next;
};
int size;
ReservationNode *head;
ReservationNode *find(int index) const;
};
#endif
这些是我到目前为止调用的构造函数和插入方法
//empty constructor
ReservationList::ReservationList() {
head = NULL;
size = 0;
}
//insert method
bool ReservationList::insert(int index, int resCode) {
if(index < 0 || index > size) { return 0; }
//making node to be added
ReservationNode* tmp;
std::cout<<"inside insert 1"<<std::endl;
tmp->Code = resCode;/*mistake is hear */
std::cout<<"inside insert 2"<<std::endl;
tmp->next = NULL;
std::cout<<"inside insert 3"<<std::endl;
if ( (index == 0) && (size == 0) ) {
std::cout<<"inside insert 4"<<std::endl;
head = tmp;
size++;
return 1;
}
else if ( (index == 0) && (size == 1) ){
tmp->next = head;
head = tmp;
size++;
return 1;
}
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
tmp->next = curr;
prev->next = tmp;
size++;
return 1;
}
这是输出
program starts!
before printing empty list
after printing empty list
inserting starts!
inside insert 1
[Done] exited with code=3221225477 in 0.622 seconds
使用“std::cout”我跟踪了插入方法的 tmp->Code = resCode;
部分的错误
问题出在 std::cout<<"inside insert 1"<<std::endl;
之后,但是当我评论 tmp->Code = resCode;
时,它在紧接着的那一行给出了错误。据我了解,访问结构中的变量或分配它们存在问题。
这段代码
ReservationNode* tmp;
std::cout<<"inside insert 1"<<std::endl;
tmp->Code = resCode;/*mistake is hear */
std::cout<<"inside insert 2"<<std::endl;
tmp->next = NULL;
调用未定义的行为,因为指针 tmp
未初始化且未指向 ReservationNode
.
您似乎忘记调用运算符 new
来创建 ReservationNode
.
同时调用函数find
两次
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
效率低下。
函数定义更简单,无需重复代码。
bool ReservationList::insert( int index, int resCode )
{
bool success = not ( index < 0 || index > size );
if ( success )
{
ReservationNode **current = &head;
while ( index-- )
{
current = &( *current )->next;
}
ReservationNode *new_node = new ReservationNode { resCode, *current };
*current = new_node;
++size;
}
return success;
}
注意如果数据成员size
是无符号整数类型size_t
而不是有符号整数类型int
会更好。同样对函数参数index
.