SE250:lab-4:klod004

From Marks Wiki
Jump to navigation Jump to search

LAB 4 This lab was a very long lab and I couldnt really finish all the task but I will try to explain whatever i did. Task 1 was too write a function to calculate the length of the linked list that was passed to it as a parameter.

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

This basically goes through each link one at a time and increments the counter by one as long as the list has not reached the end. And then the list is reassigned to the next link.

Task 2 This task was about writing code to retrieve the element in a particular position in the list. The element is stored in the head of the link.

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

The code for this was rather easy to think about. Basically returning the head of the first link. For the second and third links... list->tail(this refers to the second link then)..link->tail->head...and this approach was used for third and fourth links.

Task 3 This task took the idea from the second task and made it into one fucntion. The parameters are the list itself and the position of the element that was to be returned.

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

This code goes through all the elements one at a time till the ith element is reached. The if statement checks to see if the ith element is reached, if it is, then the head of that link is returned, and then the link is reassigned.

Task 4 The objective of this task was to compare the 2 lists that were passed to as parameters. If the lists are the same, then the return vale is 1, else its 0.

int equal(Cons* list1, Cons* list2){
	int lengthEqual, listEqual;
	lengthEqual = (length(list1)==length(list2));
	if(!lengthEqual)
 		return 0;
	listEqual = 1;
	while(listEqual){
		if(list1 == nil)
			return listEqual;
		listEqual = (list1->head == list2->head);
		if(!listEqual)
	  		return 0;
	  	list1 = list1->tail;
	  	list2 = list2->tail;
	 }
}

This code first checks if the lengths of both arrays are the same, which is they are not, then returns a false value straight away. It then checks in the while loop, if the list has not reached an end or is not a nil list (which it would return a false value);then it checks if the corresponding values in each array is the same. Finally reassigning the list to the next link.

Task 5 This task was constructed to try and find a particular element in the list. Basically looking at the heads of all the links and comparing with the element we are looking for. If found, then it returns the link itself.

Cons* find(element_t element, Cons* list){
	int i;
	for(i=0; i<length(list);i++){
		if(list->head == element){
			return list;
		}
		list = list->tail;
	}
	return nil;
}

This code looks through all the elements and finds the element which is being searched for. If the element is not found, then the function returns a nil link.

Task 6 The entire list had to be copied with this task, the hardest part was to try to interpret what the line......allocate length(xs)...... meant. But it was then solved with a lot of help from the tutorer.

Cons* copy_list(Cons* list){
	Cons* newList;
	while(list != nil){
		newList = cons(list->head, (Cons*) 0);
	 	newList->head = list->head;
		newList->tail = list->tail;
		list = list->tail;
	}
	return newList;
}

This code maybe hard to understand, but it is fairly simple. First it declares a new list, and then a loop is created which checks if the original list that was passed to it as a parameter is not empty, and then initiates the new list by assigning the head of the list as the head in this new list, and also constructing a new cons cell at the same time. It the goes into the list and reassigns the head of the new list to the head of the list and assigns the tail of the new list to the old list's tail. And the final code just reassigns the list to the tail of the link so to point to the next link.

This lab was a good lab to do, just that it was a bit too long. Too many tasks were given. We need more tutorers in the labs if we are expected to finish this in the delegated 2 hours. One is not enough.