-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorList.c
More file actions
38 lines (34 loc) · 950 Bytes
/
errorList.c
File metadata and controls
38 lines (34 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdlib.h>
#include <string.h>
#include "errorList.h"
/*add new error*/
error* add_error(char errorDescription[], error *prev) {
error *newError = (error*)malloc(sizeof(error));
if (newError == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
strcpy(newError->errorDescription, errorDescription);
newError->previous = prev;
return newError;
}
/*print error list in reverse order using recursion */
void print_error_list(error *ptr){
if (ptr==NULL)
return;
if (ptr->previous == NULL){
printf("%s\n", ptr->errorDescription);
return;
}
print_error_list(ptr->previous);
printf("%s\n", ptr->errorDescription);
}
/*gets the last node in the list and free the memory allocated to the entire list*/
void free_error_list(error *ptr){
error *prev;
while (ptr != NULL){
prev = ptr->previous;
free(ptr);
ptr = prev;
}
}