SE250:lab-4:apra102
Task 1
Hmmmmm to start with when I looked at the lab tasks sheet I am kind of lost, later I read the whole paper twice that i could do that lab with in time. This is a lengthy. I find this lab really hard to understand. Especially tasks after 3. Any how i tried to manage up to task 4 but later I am lost. I really had a hard time to understand this linked lists especially that find and copy list. To find the length of list I wrote a function as follows:
int length(Cons* list) { int len =0; for( ; list!=nil; list =list->tail){ len++; } return len; }
Task 2
To find the elements in the named functions I tried for a long time. I got the concept theoretically but I was unable to code it. But when I asked the tutor it seemed to be really easy. And I came up with the code as follows:
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; }
Task 3
I even really had a hard time understanding this nth element. Which I really couldn't do for a long time, finally I came up with the following code:
element_t nth(int i, Cons* list){ while(i !=0){ list = list->tail; i--; } return list->head; }
Task 4
Equal function, by the time i came up to this task I am short of time to complete the rest of tasks, but still i tried to do that for a while, I got up to certain point. Should make a function that returns true or false. So, I should use a ‘for’ or a 'While' loop thats what I got in my mind. I got an idea but hard to put in to a code. I got the following as code for this:
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; } } }
Up to here I did quite ok. But later i couldn't do any thing.
Main Function
int main( ) { Cons* list = cons(1,cons(2,cons(3,nil))); Cons* list2 = cons(4,cons(5,cons(7,nil))); Cons* list3 = cons(1, cons(2,cons(3,cons(4,nil)))); Cons* search; print_list(list ); /* expect: List[1,2,3] */ printf("Length = %d\n", length( list )); /* should return 3 */ printf("%d\n",first(list)); printf("%d\n",second(list)); printf("%d\n", third(list)); printf("%d\n", fourth(list)); printf("%d\n", nth(1, list)); printf("%d\n", nth(2, list)); printf("%d\n", nth(0, list)); printf("\nEqual test:\n"); printf("%d\n", equal(list, list2)); printf("%d\n", equal(list, list3)); printf("%d\n", equal(list, list)); printf("%d\n", equal(list2, list3)); return 0; }
I used these printf functions to test my functionswhich I did up to now.
Conclusion
Over all I found this lab lengthy. But a good exercise on linked lists. It was a good lab to understand how these Linked Lists works but, up to now I don't have a clear idea how to do these stuff work. But in the next lecture when the lecture taught about the whole copy, find and lookup functions i got an idea, but still I need some practice on this topic and a clear idea of what I am suppose to do.