Activity 27.4.
Please write the function
append() which takes two digit_t pointers as arguments:
-
a pointer to the current last element of the list
-
a pointer to the element that is to be appended to the list
Since by appending a new digit the end of the list will change, the function needs to return a pointer to the new last element of the list. Please also print the (three-digit) number stored in the newly created list and next free the space reserved for all digits.
Compile and run the code below in your own C environment.
#include <stdio.h>
#include <stdlib.h>
struct digit {
int num;
struct digit *next;
};
typedef struct digit digit_t;
digit_t *createDigit(int);
void append(digit_t * start, digit_t * newDigptr);
int main(void) {
digit_t *start, *newDigptr, *end;
start = createDigit(5);
newDigptr = createDigit(3);
append(start, newDigptr);
newDigptr = createDigit(7);
append(start, newDigptr);
/* print the number */
/* free all allocted space */
return 0;
}
digit_t *createDigit(int dig) {
digit_t *ptr;
ptr = (digit_t *) malloc(sizeof(digit_t));
ptr->num = dig;
ptr->next = NULL;
return(ptr);
}
void append(digit_t * end, digit_t * newDigptr) {
}

