Commit 79a374e7 authored by Liam E. Roeth's avatar Liam E. Roeth

fix search, add simple makefile and readme

parent a423667b
interactive UI:
$ make
noninteractive test of each function:
$ make test
......@@ -41,6 +41,7 @@ void print_list(NODE *head){
}
NODE_INT psearch(NODE *head, datatype data){
//search for a value. prints updates
//return:
// positive : index where data was found; NODE* return is pointer to where
// -1 : not found; NODE* return is NULL
......@@ -54,9 +55,11 @@ NODE_INT psearch(NODE *head, datatype data){
}
NODE_INT search(NODE *head, datatype data){
//searches for a value.
//return:
// positive : index where data was found; NODE* return is pointer to where
// -1 : not found; NODE* return is NULL
// NODE* is NULL : not found; int is index of last null
// NODE* is non-null : int is index where data was found;
//---------------------NODE* return is pointer to where
NODE_INT curr;
curr.node = head;
int i;
......@@ -68,11 +71,13 @@ NODE_INT search(NODE *head, datatype data){
curr.node=curr.node->next;
}
curr.node = NULL;
curr.num = -1;
curr.num = i;
return curr;
}
NODE_INT search_before(NODE *head, datatype data, unsigned before){
//searches for a value, then returns the item 'before' before it.
//only traverses the list once.
//return:
// node non-null, num>=0 : normal; found data at index[num+before]
// node NULL, num 0 : head is NULL
......@@ -85,8 +90,9 @@ NODE_INT search_before(NODE *head, datatype data, unsigned before){
out.num = 0;
if(head==NULL)
return out;
if(before == 0)
if(before == 0){
return search(head,data);
}
before;
NODE **queue = calloc(before, sizeof(NODE*));
int i=0;
......@@ -208,11 +214,11 @@ int delete_node(NODE *head, datatype data){
// 0 : normal
// 1 : head is NULL
// 2 : data is sole item in list: this function will not free head
// -1 : data not found
// 4 : data not found
NODE_INT result = search_before(head,data,1);
if(result.node == NULL){
if(result.num > 0)//not found
return -1;
return 4;
if(result.num == 0)//head is null
return 1;
if(head->next == NULL)//data is sole item in list
......
run : main.out
./main.out
.PHONY : run all test
test.out : llist.c llist.h test.c
gcc -o test.out llist.c test.c
main.out : llist.c llist.h main.c
gcc -o main.out llist.c main.c
all : test.out main.out
test : test.out
./test.out
......@@ -23,20 +23,28 @@ int main(){
printf("I found it at index %d.\n", target.num);
fputs("The number I found there was: ", stdout);
print_node(target.node);
puts("Deleting the element at index 3...");
target = traverse(root, 3);
fputs("At index 3 there is: ",stdout);
print_node(target.node);
puts("Deleting the element at index 3...");
delete_node_at(root, 3);
puts("Now it's garbage:");
fputs("Now it's garbage: ",stdout);
print_node(target.node);
puts("Deleting the first occurrence of 1...");
puts("Deleting the first occurrence of 5...");
target = search(root, 5);
delete_node(root, 5);
puts("Now it's garbage:");
fputs("Now it's garbage: ",stdout);
print_node(target.node);
print_list(root);
puts("Deleting the first item by index,");
delete_node_at(root, 0);
puts("and the new first item by value...");
delete_node(root, 3);
printf("The head is still at %p.\n",root);
print_list(root);
puts("Deleting list...");
delete_list(root);
puts("Now it's garbage:");
puts("Now it's garbage (or at least probably not data):");
print_list(root);
return 0;
......
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