This is the code my professor gave us before assigning the code
Code:
#include <stdio.h>
#include <stdlib.h>
struct point{
int x;
int y;
struct point * next;
};
void print_list( struct point * );
struct point * make_point( int, int );
struct point * prepend_point( struct point *, struct point * );
struct point * remove_point( struct point * );
struct point * total_points( struct point * );
int list_size( struct point * );
struct point * remove_end( struct point * );
int main(){
struct point * p1 = make_point( 11, 11 );
struct point * p2 = make_point( 22, 22 );
struct point * p3 = make_point( 33, 33 );
struct point * p4 = make_point( 44, 44 );
printf( "Before adding elements:\n" );
print_list( p1 );
p1 = prepend_point( p1, p2 );
printf( "After adding p2:\n" );
print_list( p1 );
p1 = prepend_point( p1, p3 );
printf( "After adding p3:\n" );
print_list( p1 );
p1 = prepend_point( p1, p4 );
printf( "After last element:\n" );
print_list( p1 );
printf( "The size of the list is: %d\n", list_size( p1 ) );
struct point * the_total = total_points( p1 );
printf( "\nThe totals are x: %d, y: %d\n", the_total->x, the_total->y );
printf( "Removed last element\n" );
p1 = remove_end( p1 );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
printf( "The size of the list is: %d\n", list_size( p1 ) );
getchar();
return 0;
}
void print_list( struct point * list ){
while( list != NULL ){
printf( "\tIn print list: %d, %d\n", list->x, list->y );
list = list->next;
}
}
struct point * make_point( int a, int b ){
struct point * temp = (struct point *)malloc( sizeof( struct point ) );
temp->x = a;
temp->y = b;
temp->next = NULL;
return temp;
}
struct point * prepend_point( struct point * a , struct point * b ){
b->next = a;
return b;
}
struct point * remove_point( struct point * toast ){
/*if( toast != NULL ){
struct point * temp = toast->next;
toast = NULL;
free( toast );
return temp;
}else{
return NULL;
}*/
if( !toast ){
return NULL;
}else{
struct point * temp = toast->next;
toast = NULL;
free( toast );
return temp;
}
}
struct point * total_points( struct point * list ){
struct point * temp = make_point( 0, 0 );
while( list ){
printf( "beep\n" );
temp->x = temp->x + list->x;
temp->y = temp->y + list->y;
list = list->next;
}
return temp;
}
int list_size( struct point * list ){
int counter = 0;
while( list ){
list = list->next;
counter++;
}
return counter;
}
struct point * remove_end( struct point * list ){
if( !list ){
return NULL;
}else if( !list->next ){
free( list );
return NULL;
}else{
struct point * temp = list;
while( temp->next->next ){
temp = temp->next;
}
free( temp->next );
temp->next = NULL;
return list;
}
}
This is the code my professor gave before assigning the program. I was under the impression this was a linked list because he was for one to be inlcuded in the program