[英]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;
}
This the head of ReservationList class(ReservationList.h)这是 ReservationList 类的头(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
And these are the methods I have called so far constructor and insert methods这些是我迄今为止调用的构造函数和插入方法
//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;
}
This is the output这是 output
program starts!
before printing empty list
after printing empty list
inserting starts!
inside insert 1
[Done] exited with code=3221225477 in 0.622 seconds
with the "std::cout" i tracked the error it is at tmp->Code = resCode;
使用“std::cout”,我跟踪了它在
tmp->Code = resCode;
处的错误。 part of the insert method the problem is at after std::cout<<"inside insert 1"<<std::endl;
部分插入方法问题出在
std::cout<<"inside insert 1"<<std::endl;
however when I comment the tmp->Code = resCode;
但是,当我评论
tmp->Code = resCode;
it gives error at the line just after.它在后面的那一行给出错误。 As I understand there is problem with accessing the variables inside struct or assigning them.
据我了解,访问结构内的变量或分配它们存在问题。
This code snippet此代码段
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;
invokes undefined behavior because the pointer tmp
is uninitialized and does not point to a valid object of the type ReservationNode
.调用未定义的行为,因为指针
tmp
未初始化并且未指向ReservationNode
类型的有效 object 。
It seems you forgot to call the operator new
to create an object of the type ReservationNode
.您似乎忘记调用 operator
new
来创建ReservationNode
类型的 object 。
Also calling the function find
two times又调用了 function
find
了两次
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
is inefficient.效率低下。
The function can be defined simpler without a duplicated code. function 可以更简单地定义,无需重复代码。
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;
}
Pay attention to that it would be much better if the data member size
will have the unsigned integer type size_t
instead of the signed integer type int
.请注意,如果数据成员
size
具有无符号 integer 类型size_t
而不是有符号 integer 类型int
会更好。 The same is valid for the function parameter index
. function 参数
index
也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.