SE250:lab-4:jpar277

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

Lab Overview

Well, I think the tasks weren't too difficult, maybe a little time consuming and brain racking - seriously, this Lab required so much thinking! It was kind of John to give us all the test code - I actually didn't find out until I got to the equals function XD!!

Length Function

int length(Cons* list) {
	int i = 0;
	while (list != nil) {
		i++;
		list = list->tail;
	}
	return i;
}

Thats my code for the length function, not really anything to say about it except that I forgot about the list part lol my first attempt was

int length(Cons*) {
	int i = 0;
	while (Cons != nil) {
		i++;
		Cons = Cons->tail;
	}
	return i;
}

very very wrong!

First Element Function

element_t first(Cons* list) {
	return list->head;
}

Rather obvious.

Second Element Function

element_t second(Cons* list) {
	list = list->tail;
	return list->head;
}

We want to move to the second element which is why the line "list = list->tail;" is there, that will move us to the next element. I could've reduced it down to 1 line "return list->tail->head"

Third Element Function

element_t third(Cons* list) {
	int i = 0;
	while (i != 2) {
		list = list->tail;
		i++;
	}
	return list->head;
}

We want to move to the third element this time, preparing myself for the "nth element function", I decided to use a loop. Again, I could've reduced it down to 1 line "return list->tail->tail->head"

Fourth Element Function

element_t fourth(Cons* list) {
	int i = 0;
	while (i != 3) {
		list = list->tail;
		i++;
	}
	return list->head;
}

Like the third element function, I used a loop. Could've reduced the code down to 1 line once more "return list->tail->->tail->head"

nth Element Function

element_t nth(int i, Cons* list) {
	int j = 0;
	while (j != i) {
		list = list->tail;
		j++;
	}
	return list->head;
}

Using the loop structure in the previous first~fourth element functions, I created a similar loop using the algebraic expression i.

Equal Function

int equal(Cons* list1, Cons* list2) {
	int i = 0;
	int j = 0;
	i = length(list1);
	if (i == length(list2)) {
		while (j != i) {
			if (list1->head != list2->head) {
				return 0;
			} else {
				list1 = list1->tail;
				list2 = list2->tail;
				j++;			
			}
		}
		return 1;
	}
	return 0;
}

It took me a while to get my head around this one. Required so much thinking! First I wanted to check if the lengths of the two lists were the same, if they're not then theres no point going through them. Then we want to go through each element in both lists and compare them.

Find Function

Cons* find(element_t i, Cons* list) {
	int j = 0;
	int k = 0;
	j = length(list);
	while (k != j) {
		if (i == list->head) {
			return list;
		}
		list = list->tail;
		k++;
	}
	return list;
}

Basically, the while loop will go through each element in the list and compare it with the element we're looking for (i in my code). It will exit the loop when it reaches the end, obviously if its reached the end, it hasn't found the element and because we're at the end, it will return and empty list.