SE250:lab-4:ssre005

From Marks Wiki
Jump to navigation Jump to search

The Following is the code I came up with for the lab:



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

return length;
}

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


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

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

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

int equal(Cons* list, Cons* list2){
    int same = 0;
    for( ; list != nil && list->head == list2->head ; list = list->tail ) {
	list2 = list2->tail;
	if (list->head == list2->head){
	    same = 1;
	} else {
            same = 0;
        }
        
        if (list1 -> next == nil)&&(list2 -> next != nil){
        same = 0;
        }
 
    
    }
	return same;
}

Cons* find(element_t elt, Cons* list){
    Cons* returnList;
    for( ; list != nil; list = list->tail ) {
	if (elt == list->head){
	    returnList = list;
	}else{
	    returnList = list;
	}
    }
    return list;
}

This is what I did for the copy_list code:


Cons* copy_list(Cons* list){
    Cons copy_list;
    int i;
    int length_of_list = length(Cons*);
    copy_list = nil;   
    
    for (i = length_of_list; i == 0; i--){
	copy_list = cons(list->head, copy_list);
  
    for( ; list != nil; list = list->tail ) {
	copy_list = cons(list->head, 
	copiedList->head = list->head;
	copiedList = copiedList->tail;
    }
}
}

As you can tell, my spiraling confusion with this task forced me give up for the time being. For the record, the first for loop in the code above was an attempt at implementing a similar code to that found in the minutes section for the lecture on Linked Lists. However, this became an awkward way of solving the problem as the lecture example involved placing integers from the for loop itself into the new cells rather than integers from another linked list.

Other students have suggested making the "append" function before the copy function so that it may be used within the copy function, making the implementation much easier.