Commit 1eed34dd authored by Liam E. Roeth's avatar Liam E. Roeth

add search_before, delete_node; fix traverse_before, delete_node_at

main and test not updated yet
parent 81a41a5c
......@@ -63,6 +63,39 @@ NODE_INT search(NODE *head, datatype data){
return curr;
}
NODE_INT search_before(NODE *head, datatype data, unsigned before){
//return:
// node non-null, num>=0 : normal; found data at index[num+before]
// node NULL, num 0 : head is NULL
// node NULL, num<0 : found data before enough items processed. num is index.
//--------------------e.g. search_before({1,2,3},2,3) -> (NULL, -2)
// node NULL, num>0 : did not find data; returns index of final NULL
//--------------------e.g. search_before({1,2,3},4,3) -> (NULL, 3)
NODE_INT out;
out.node = NULL;
out.num = 0;
if(head==NULL)
return out;
if(before == 0)
return search(head,data);
before;
NODE **queue = calloc(before, sizeof(NODE*));
int i=0;
while(head != NULL && head->data != data){
queue[i] = head;
i = (i+1)%before;
out.num++;
head = head->next;
}
if(head == NULL){
out.node = NULL;
return out;
}
out.node = queue[i];
out.num -= before;
return out;
}
NODE *construct(datatype data, NODE *next){
//return:
// pointer to new node : normal
......@@ -98,59 +131,39 @@ NODE_INT traverse(NODE *head, int pos){
return out;
}
NODE_INT search_before(NODE *head, datatype data, unsigned before){
//NODE_INT traverse_before(NODE *head, int pos, int offset){
// int newPos = pos-offset;
// NODE_INT returnCode = traverse(head, newPos);
// switch(returnCode.num){
// case 0: printf("Case 0-> here is the data: %d\n", returnCode.node->data);
// break;
// case 1: printf("Case 1-> here is the data: %d\n",returnCode.node->data);
// break;
// case 2: printf("Not in the list; returning final data point: %d\n",returnCode.node->data);
// break;
// }
//}
NODE_INT traverse_before(NODE *head, int pos, unsigned before){
//return:
// node non-null, num>-1: normal; found data at index num+before
// node NULL, num 0 : head is NULL
// node NULL, num<0 : completed before enough data processed. num is index.
// e.g. search_before({1,2,3},2,3) -> (NULL, -2)
// node non-null, num<0 : did not find data; returns node 'before' before last
// if n is index of node returned, num is -(n+1)
// 0 : normal
// 1 : head is null; NODE* return is NULL
// 2 : reached end of list; NODE* return is final item
// 3 : reached end of list, but before put result back in list: NODE* is result
// 4 : before is larger than pos or pos is negative; NODE* return is NULL
int effectivePos = pos - before;
NODE_INT out;
out.node = NULL;
out.num = 0;
if(head==NULL)
return out;
if(before == 0)
return search(head,data);
before;
NODE **queue = calloc(before, sizeof(NODE*));
int i=0;
while(head != NULL && head->data != data){
queue[i] = head;
i = (i+1)%before;
out.num++;
head = head->next;
out.node=NULL;
out.num=4;
if(effectivePos >= 0){
out = traverse(head, effectivePos);
if(out.num==0)
out.num = length(out.node) > before ? 0 : 3;
}
out.node = queue[i];
out.num -= before;
if(head == NULL)
out.num = (-1)-out.num;
return out;
}
NODE_INT traverseBefore(NODE *head, int pos, int offset){
int newPos = pos-offset;
NODE_INT returnCode = traverse(head, newPos);
switch(returnCode.num){
case 0: printf("Case 0-> here is the data: %d\n", returnCode.node->data);
break;
case 1: printf("Case 1-> here is the data: %d\n",returnCode.node->data);
break;
case 2: printf("Not in the list; returning final data point: %d\n",returnCode.node->data);
break;
}
}
int delete_node_at(NODE *head, int pos){
NODE_INT nodeToDelete = traverse(head, pos);
NODE_INT priorNode = traverse(head, pos-1);
priorNode.node->next = nodeToDelete.node->next;
//NODE *nextNode = nodeToDelete.node->next;
free(nodeToDelete.node);
return 0;
}
int delete_node_after(NODE *head){
//return:
// 0 : normal
......@@ -171,6 +184,54 @@ void delete_list(NODE *head){
delete_list(tmp);
}
int delete_node_at(NODE *head, int pos){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : head->next is NULL: this function will not free head
// 3 : pos is beyond end of list
if(head == NULL)
return 1;
if(head->next == NULL)
return 2;
if(pos == 0){
head->data = head->next->data;
delete_node_after(head);
return 0;
}
NODE_INT priorNode = traverse_before(head, pos, 1);
if(priorNode.num == 2 || priorNode.num == 3)
return 3;
NODE *tmp = priorNode.node->next;
priorNode.node->next = tmp->next;
free(tmp);
return 0;
}
int delete_node(NODE *head, datatype data){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : data is sole item in list: this function will not free head
// -1 : data not found
NODE_INT result = search_before(head,data,1);
if(result.node == NULL){
if(result.num > 0)//not found
return -1;
if(result.num == 0)//head is null
return 1;
if(head->next == NULL)//data is sole item in list
return 2;
//data is first item: preserve address of head while deleting it
head->data = head->next->data;
delete_node_after(head);
return 0;
}
//data is somewhere in the list
delete_node_after(result.node);
return 0;
}
NODE *construct_list(datatype list[], int size){
//return:
// pointer to head : normal
......
......@@ -20,11 +20,11 @@ NODE_INT search(NODE *head, datatype data);
NODE_INT search_before(NODE *head, datatype data, unsigned before);
NODE *construct(datatype data, NODE *next);
NODE_INT traverse(NODE *head, int pos);
NODE_INT traverseBefore(NODE *head, int pos, int offset);
NODE_INT traverse_before(NODE *head, int pos, unsigned before);
int delete_node_after(NODE *head);
void delete_list(NODE *head);
int delete_node_at(NODE *head, int pos);
//int delete_node(NODE *head, datatype data);
int delete_node(NODE *head, datatype data);
NODE *construct_list(datatype list[], int size);
int add_to_end(NODE *head, datatype new_data);
int insert_node(NODE *head, int pos, datatype new_data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment