Hello,
I'm writing a simple program implementing a list. You can add, remove, search, print and find the minimum element.
My problem is that the program crashes if I don't add the 'system("PAUSE");' line before the last 'printf("Minimo = %d.\n", minimo(l));' call in the main method.
Here is the code:
#include <cstdlib>
#include <iostream>
#include <limits.h>
using namespace std;
/****************************************************************************/
struct Record {
int val; // the value
struct Record* next; // pointer to the next Record
};
/****************************************************************************/
struct Lista {
Record* testa; // pointer to the first element in the list
};
/****************************************************************************/
// returns the pointer to the record with value k, or NULL
void* search(struct Lista* l, int k);
// delete the record pointed by x
void cancella(struct Lista* l, Record* x);
// prints the list
void stampaLista(struct Lista* l);
// add a new element to the list
void aggiungi(struct Lista* l, int val);
// returns the value of the minimum element in the list
int minimo(struct Lista* l);
// returns true if the list is empty
bool isEmpty(struct Lista* l);
/****************************************************************************/
void cancella(struct Lista* l, Record* x) {
if(isEmpty(l))
return;
// new pointer to the head of the list
struct Record* r = (struct Record*) malloc(sizeof(struct Record));
r = l->testa;
// if I have to delete the head of the list
if(l->testa == x) {
l->testa = l->testa->next;
free(r);
free(&r);
return;
}
// else,
do {
// if the element to delete is the 'next' one
if(r->next == x) {
void* v = r->next->next;
free(r->next);
r->next = (Record*) v;
break;
}
} while( (r = r->next) != NULL);
free(&r);
}
/****************************************************************************/
// prints
void stampaLista(struct Lista* l) {
if(isEmpty(l)) {
printf("La Lista è vuota.\n");
return;
}
struct Record* r = (struct Record*) malloc(sizeof(struct Record));
r = l->testa;
do {
printf("%d, ", r->val);
} while( (r = r->next) != NULL);
printf("\n");
free(&r);
}
/****************************************************************************/
bool isEmpty(Lista* l) {
if(l->testa == NULL)
return true;
return false;
}
/****************************************************************************/
void* search(struct Lista* l, int k) {
if(isEmpty(l))
return NULL;
struct Record* r = (struct Record*) malloc(sizeof(struct Record));
r = l->testa;
do {
if(r->val == k) {
return r;
}
} while( (r = r->next) != NULL);
free(&r);
return NULL;
}
/****************************************************************************/
int minimo(struct Lista* l) {
if(isEmpty(l))
return INT_MAX;
int min = INT_MAX;
struct Record* r = (struct Record*) malloc(sizeof(struct Record));
r = l->testa;
do {
if(r->val < min)
min = r->val;
} while( (r = r->next) != NULL);
free(&r);
return min;
}
/****************************************************************************/
void aggiungi(struct Lista* l, int val) {
// creates the new Record
struct Record* r = (struct Record*) malloc(sizeof(Record));
r->val = val;
r->next = NULL;
// if the list is empty, the new record is the head of the list
if(isEmpty(l)) {
l->testa = r;
return;
}
// else, search the last element in the list and appends the new Record
struct Record* r2 = (struct Record*) malloc(sizeof(struct Record));
r2 = l->testa;
do {
if(r2->next == NULL) {
r2->next = r;
break;
}
} while( (r2 = r2->next) != NULL);
free(&r2);
}
/****************************************************************************/
int main(int argc, char *argv[]) {
Lista* l = (Lista*) malloc(sizeof(Lista));
l->testa = NULL;
stampaLista(l);
aggiungi(l, 10);
aggiungi(l, 20);
aggiungi(l, 30);
aggiungi(l, 40);
aggiungi(l, 50);
stampaLista(l);
cancella(l, (Record*) search(l, 30));
stampaLista(l);
printf("Minimo = %d.\n", minimo(l));
cancella(l, (Record*) search(l, 10));
stampaLista(l);
////////////////// HERE HERE HERE /////////////////////
// IF I TAKE THIS OUT, IT CRASHES
system("PAUSE");
printf("Minimo = %d.\n", minimo(l));
system("PAUSE");
return EXIT_SUCCESS;
}
I really have no idea :(
Thank you in advance.