Commit 07654e18 authored by Liam E. Roeth's avatar Liam E. Roeth

search_before, support for test2

added search_before()
added support for a temporary test file, test2.c  makefile etc.
parent 3d75117f
*.out
tabify
\ No newline at end of file
run : main.out
./main.out
.PHONY : run all test tabify
.PHONY : run all test test2
test.out : tabify llist.c llist.h test.c
gcc -ggdb -std=c99 -o test.out llist.c test.c
......@@ -9,17 +9,24 @@ test.out : tabify llist.c llist.h test.c
main.out : tabify llist.c llist.h main.c
gcc -std=c99 -o main.out llist.c main.c
test2.out : tabify llist.c llist.h test2.c
gcc -std=c99 -o test2.out llist.c test2.c
all : test.out main.out
test : test.out
./test.out
test2 : test2.out
./test2.out
tabify : *.h *.c
for file in *.c *.h;\
do\
unexpand -t4 --first-only $$file >temp ;\
mv temp $$file;\
done
touch tabify
gitindex : tabify *.c *.h
git add *.c *.h
......@@ -98,6 +98,37 @@ NODE_INT traverse(NODE *head, int pos){
return out;
}
NODE_INT search_before(NODE *head, datatype data, 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)
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 = queue[i];
out.num -= before;
if(head == NULL)
out.num = (-1)-out.num;
return out;
}
int delete_node_after(NODE *head){
//return:
// 0 : normal
......
......@@ -17,6 +17,7 @@ void print_list(NODE *head);
void print_node(NODE *head);
NODE_INT psearch(NODE *head, datatype data);
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 traverse_before(NODE *head, int pos, int before);
......
#include <stdio.h>
#include "llist.h"
int main()
{
}
//
// struct Node *start;
//
// int arr[] = {10,20,30,40,50};
// start = construct_list(arr, 5);
// NODE_INT Return;
// Return = search_before(start, 60, 22);
// if(Return.node) printf("%d",Return.node->data);
// printf(" | %p , %d\n",Return.node,Return.num);
// 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