SE250:lab-4:sgha014
length function: i ust looped through and kept incrementing the length...and then returning the length
int length(Cons* list) {
int len =0;
for( ; list!=nil; list =list->tail){
len++;
}
return len;
}
functions that return first, second, third or fourth element for this one i drew diagrams to kinda see what points to what and then after that it was pretty easy to write the function.
element_t first(Cons* list){
return list->head;
}
element_t second(Cons* list){
return list->tail->head;
}
element_t third(Cons* list){
return list->tail->tail->head;
}
element_t fourth(Cons* list){
return list->tail->tail->tail->head;
}
nth function: loop through the list and once you get to zero you return the i'th element of the list
element_t nth(int i, Cons* list){
while(i !=0){
list = list->tail;
i--;
}
return list->head;
}
equal function: i loop through the list and while im not at the end element, i compare the values stored int eh elements then update the list
int equal(Cons* list1, Cons* list2){
while((list1 !=nil) && (list2 !=nil)){
if(list1->head ==list2->head){
list1 = list1->tail;
list2 = list2->tail;
return 1;
}
else{
return 0;
}
}
}
this was a pretty good lab.. was a bit confused for a while but then once i got what i was doing it was fairly ok. the hardest part for me was the nth function...got really confused for some reason. But overall it was a good lab