SE250:lab-4:sshi080

From Marks Wiki
Jump to navigation Jump to search

Lab Overview

First of all I arrived late (10:45), slept in :D. so I was rushing to get things started. This lab is based on linked lists and how they operate. We get some starter code to get us going first, then we have to write several functions. I started off by trying to remember how a cons list works as a I didn't really pay attention in class :P but after quickly looking at the starter code, I got a pretty clear picture on how a cons list works.

Half way through the LAB I realised we have to use the test-code.c to test our code, I couldn't get those tests to work so I just tested the code through my own printfs.

Task 1

This tasks involves writing a simple function to return the length of the cons list.

Code:

int length(Cons* list) {
	int i = 0;
	
	if(list == nil) {
		return 0;
	}

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

	return i;
}

Results

printf("Length of cons list(3): %d\n", length(cons(1,cons(2,cons(3,nil)))));

The function outputs the correct length (inserted 3 linked lists) and returned 3.

printf("Length of cons list(0): %d\n\n", length(nil));

When inputting a nil list, it returns 0.

Task 2

This task is to write 3 functions to return the first, second and third element values (head) of a cons list

Code:

element_t first(Cons* list) {
	if(list == nil) {
		return 0;
	}
	return list->head;
}

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

element_t third(Cons* list) {
	if(list == nil) {
		return 0;
	}
	return list->tail->tail->head;
}

Obviously these functions are not scalable, and requires manual labour of writing more and more "->"s.

Results

  printf("First element(1): %d\n", first(cons(1,cons(4,cons(7,nil)))));
  printf("First element(4): %d\n", second(cons(1,cons(4,cons(7,nil)))));
  printf("First element(7): %d\n", third(cons(1,cons(4,cons(7,nil)))));

Running the above code returns the results 1,4,7 as expected.

Task 3

This task is to write a function that will enhance the previous task, but returning the value of the i'th element in a cons list.

Here is what I came up with:

element_t nth(int i, Cons* list) {
	int j = 0;
	Cons *find1 = list;

	if(list == nil) {
		return 0;
	}

	for(j; j < i; j++) {
		if(list->tail == nil) {
			break;
		}
		
		find1 = find1->tail;
	}

	return find1->head;
}

Results

  printf("Using nth element function, 2nd element: %d\n", nth(1, cons(1,cons(2,cons(3,nil)))));
  printf("Using nth element function, pass in nil list: %d\n", nth(1, nil));
  printf("Using nth element function, try to get value beyond the length of the list: %d\n", nth(3, cons(1, cons(2, nil))));

I had a few compile errors at the start due to bad pointers but I quickly fixed them.
The results all displayed correctly after the fixes.
This is what returned on the console output, which was expected:

Task3:
Using nth element function, 2nd element: 2
Using nth element function, pass in nil list: 0
Using nth element function, try to get value beyond the length of the list: 0

Task 4

This task is to write a function to compare two linked lists and their values to see if they're equal. If they are equal exactly it will return 1, otherwise it will return 0.

Here is what I came up with:

int equal(Cons* list1, Cons* list2) {
	int i;

	if (length(list1) != length(list2)) {
		return 0;
	}
	
	for(i = 0; i < length(list1); i++) {
		if (nth(i, list1) != nth(i, list2)) {
			return 0;
		}
	}

	return 1;
}

As you can see, this function uses existing functions present in my code. There is no need to write new code that does the same things as my other functions do.

Results

  printf("Check if two lists are equal(0, length different): %d\n", equal(cons(1, cons(2, nil)), cons(1,nil)));
  printf("Check if two lists are equal(0, length same but values different): %d\n", equal(cons(1, nil), cons(1, nil)));
  printf("Check if two lists are equal(1, length and values same): %d\n", equal(cons(1, nil), cons(1, nil)));
  printf("Check if two lists are equal(1, length and values same, but longer): %d\n", equal(cons(1, cons(2, nil)), cons(1, cons(2, nil))));

The results were correct, surprisingly after a few crashes initially for some unknown reason.

Output:

Task4:
Check if two lists are equal(0, length different): 0
Check if two lists are equal(0, length same but values different): 1
Check if two lists are equal(1, length and values same): 1
Check if two lists are equal(1, length and values same, but longer): 1