SE250:lab-4:asin185
Lab 4
Intro
In this lab we explored linked list and the differences of their length and size.
length
int length( Cons* list) { int i; for ( i = 0; list != nil ; i++) { list = list->tail; } return i; }
Elements - First,Second,Third,Fourth
element_t first( Cons* list ) { return list->head; }
element_t second( Cons* list ) { int i; for ( i = 0; i < 1; i++) { if (list == nil) return 0; list = list->tail; } return list->head; }
element_t third( Cons* list ) { int i; for ( i = 0; i < 2; i++) { if (list == nil) return 0; list = list->tail; } return list->head; }
element_t fourth( Cons* list ) { int i; for ( i = 0; i < 3; i++) { if (list == nil) return 0; list = list->tail; } return list->head; }
Nth Element
element_t nth( int i, Cons* list ) { for ( ; i > 0; i--) { if (list == nil) return 0; list = list->tail; } return list->head; }
Equal function
int equal( Cons* first, Cons* second ) { while ( first->head == second->head ) { if (first->tail == nil && second->tail == nil) return 1; first = first->tail; second = second->tail; } return 0; }
Find function
int equal( Cons* first, Cons* second ) { while ( first->head == second->head ) { if (first->tail == nil && second->tail == nil) return 1; first = first->tail; second = second->tail; } return 0; }
Copy list function
Cons* copy_list( Cons* list ) { Cons* copied = nil; int size = length(list); int i; for ( i = 1; i < size + 1 ; i++ ) { copied = cons(nth(size-i,list), copied); } return copied; }
Append function
Cons* append( Cons* xs, Cons* ys ) { int size = length(xs); int i; for ( i = 1 ; i < size + 1; i++ ) ys = cons( nth( size-i, xs ), ys ); return ys; }
Nappend function
Cons* nappend( Cons** xs, Cons* ys )
Reverse function
Cons* reverse( Cons* xs ) { Cons* reversed = nil; int size = length(xs); int i = 0; for ( i = 0 ; i < size; i++ ) reversed = cons( nth( i, xs), reversed ); return reversed; }