SE250:lab-4:gfun006

From Marks Wiki
Revision as of 05:19, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (7 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Ah sorry for having this lab report up so late! I was having a hard time with it during the actual lab...and we all had a busy week. But here it is finally! >_<

Task 1

In this task we had to write a function that returns the number of elements in the list. My code was as follows:

int length ( Cons* list) {
	int i = 0;

	for ( ; list != nil; list = list->tail ) {
		i++; }

	return i;
}

Originally I was actually really confused how to start this whole lab...I think I was still stuck in Java land and forgot how C worked. But I eventually got my head around it.

Task 2

A function had to be written so that it returned a given element in the list. My code:

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;
}

I struggled already in this task because of my lack of understanding in linked lists. But with some diagrams drawn and help from fellow students, I finally understood the concept behind it. I originally thought that for each head and a tail, it counted for 2 elements...that's why I was confused at the start. With that cleared up I got through this task really fast.

Task 3

In this task we had to return a nth element in the list, or 0 if no such elements existed. My code was:

element_t nth(int i, Cons* list) {
    int x;
       for (x = 0; x < i; x++) {
        list = list->tail;
    }
       return list->head;
}

For example, if I entered 2, I would expect the 2nd element in the list, which was 2. If we step through the code, when x = 0, list= list->tail(the first element's tail, thus pointing at second element's head). Then x = 1, list = list->tail(the second element's tail, thus pointing at third element's head). But since it returns list->head, the head of the second element is printed, which is 2.

Task 4

In this task we had to write a equal function to see if 2 lists have exactly the same elements as the other and in the same order, returning true if so and false if not.

int equal(Cons* lista, Cons* listb) {
    while ((lista != nil) && (listb != nil)) {
	if (lista->head == listb->head) {
		lista = lista->tail;
		listb = listb->tail;
		return 1;
	} 

	else
	        return 0;
	}
}

This took me a while to get around and even when I did, I had too many if statements in if statements that it got too messy. I simplified it to the above and it does the following: Checks both lists to see if they actually have anything in them first, if they do, then it first checks if the first elements are the same (because if they aren't, then it will automatically return 0. Then it goes through each list and goes through each linked list until the end, and returning 1 if all is well.

Task 5

In this task we had to write a find function that searches for an element and returns a Cons cell that has this element, or an empty list if it does not exist.

I did not manage to make this work...but the code I got up to here was:

element_t find(element_t element, Cons* list) { 

	for ( ; list != nil; list = list->tail ) {
		if (list->head == element)
			return list; 

		else
			return nil; }

}

Which gave me a ridiculous output. I decided from this point I was going to move on from this lab...