简体   繁体   中英

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)

#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

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; part of the insert method the problem is at after std::cout<<"inside insert 1"<<std::endl; however when I comment the 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 .

It seems you forgot to call the operator new to create an object of the type ReservationNode .

Also calling the function find two times

ReservationNode  *curr , *prev; 
curr = find( index );
prev = find( index - 1 );

is inefficient.

The function can be defined simpler without a duplicated code.

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 . The same is valid for the function parameter index .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM