Struct, linked list,delete the whole list

830 Views Asked by At

How can I delete all objects from a linked list,implemented in struct? After my delete-funktion (loeschen) are shown comic numbers and I don´t know any more where my pointers are.

struct domino {
int data1;
int data2;
domino *next;


};

int readSteine (){ //Reading from numbers from file
    FILE *file;


    if((file=fopen("datei.dat", "r") )==NULL) { /
        std::cout<<"File cant be open"<<std::endl;
        return 0;
    }else {
        int beginning;
        int temp;
        fscanf(file, "%d", &beginning);

        for(int i=0; i<beginning; i++) {
            domino *domino1= new domino;
             //if(i>0) temp2->next=domino1; 
            fscanf(file, "%i", &temp);
            domino1->data1=temp;
            fscanf(file, "%i", &temp);
            domino1->data2=temp;
            printf("[%d:%d]", domino1->data1, domino1->data2);
            domino1->next=0;
       }
    }return 0;
}

Function for deleting of the list:

void deletelist (domino *head) {
    domino* tmp;
    while(head != 0) { 
        tmp=head->next;
        delete head;
        head=tmp;
    }

}


int main() {
    domino *pHead=NULL; 
domino s;
    readSteine();
    deletelist(pHead);
    std::cout<<s.data1<<"...."<<s.data2<<std::endl;

    return 0;
}
3

There are 3 best solutions below

2
On

Your loeschen will never works becouse you pass to it just a NULL. You first need to correctly construct the list. First, you need to pass your Kopf (a kind of reference to it) to the readSteine function, and assign it to each new next, and after that assign to it the new domino, and so on to form the linked list..

I strongly recommend you to read Programming: Principles and Practice Using C++ .

You mix C functions and style of programming with C++. You dont even need to know these C functions which are hard to learn right in order to begin to learn C++.

0
On

Below is an example of a clear method. The idea is to traverse over the list and delete the nodes one by one.

Node *pDel = _pHead;

    /* Traverse the list and delete the node one by one from the head */
    while (pDel != NULL) {
        /* take out the head node */
        _pHead = _pHead->_pNext;
        delete pDel;
        /* update the head node */
        pDel = _pHead;
    }
    /* Reset the head and tail node */
    _pTail = _pHead = NULL;
0
On

Define the function either like

void loeschen (dominostein **kopf) {
    dominostein* tmp;
    while(*kopf != 0) { //bzw. NULL
        tmp = ( *kopf )->next;
        delete *kopf;
        *kopf = tmp;
    }
}

and call it like

loeschen(&pKopf);

or the following way

void loeschen (dominostein * &kopf) {
    dominostein* tmp;
    while( kopf != 0) { //bzw. NULL
        tmp = ( kopf )->next;
        delete kopf;
        kopf = tmp;
    }
}

and call it like

loeschen(pKopf);

Your original function also is correct but after deleteing all nodes

loeschen(pKopf);

you should set pKopf to NULL

pKopf = NULL;

Or you simply should not use this pointer to access the deleted nodes.:)

Take into account that this statement

std::cout<<s.data1<<"...."<<s.data2<<std::endl;

does not make sense because data members data1 and data2 of object s were not inistilaized when the object was created

domino s;

And this object has nothing common with the list.

And function readSteine does not use node pHead defined in main

domino *pHead=NULL; 
//...
readSteine();

You shpuld rewrite the function. It is wrong.