Commit f58ba3a6 authored by Liam E. Roeth's avatar Liam E. Roeth

cleanup

changed NODE_ERR to NODE_INT (and member 'err' to 'num')
commented most functions
updated traverse to return an error code, and others to check for it
makefile has 'gitindex' target, to add all code to git's index
makefile automatically converts spaces to tabs before compiles and gitindex
added .gitignore
parent f2097af6
run : main.out run : main.out
./main.out ./main.out
.PHONY : run compile test tabify .PHONY : run all test tabify
test.out : llist.c llist.h test.c test.out : tabify llist.c llist.h test.c
gcc -ggdb -std=c99 -o test.out llist.c test.c gcc -ggdb -std=c99 -o test.out llist.c test.c
main.out : llist.c llist.h main.c main.out : tabify llist.c llist.h main.c
gcc -std=c99 -o main.out llist.c main.c gcc -std=c99 -o main.out llist.c main.c
compile : test.out main.out all : test.out main.out
test : test.out test : test.out
./test.out ./test.out
tabify : *.h tabify : *.h *.c
for file in *.c *.h;\ for file in *.c *.h;\
do\ do\
unexpand -t4 --first-only $$file >temp ;\ unexpand -t4 --first-only $$file >temp ;\
mv temp $$file;\ mv temp $$file;\
done done
gitindex : tabify *.c *.h
git add *.c *.h
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "llist.h" #include "llist.h"
void print_node_at(NODE *head, int pos){ void print_node_at(NODE *head, int pos){
//prints by index
NODE *currentNode; NODE *currentNode;
currentNode = head; currentNode = head;
...@@ -14,11 +15,10 @@ void print_node_at(NODE *head, int pos){ ...@@ -14,11 +15,10 @@ void print_node_at(NODE *head, int pos){
} }
void print_node(NODE *head){ void print_node(NODE *head){
//prints head
printf("%d\n", head->data);} printf("%d\n", head->data);}
void print_list(NODE *head){ void print_list(NODE *head){
// printf("Printing list...\n");
NODE *currentNode; NODE *currentNode;
currentNode = head; currentNode = head;
...@@ -31,33 +31,42 @@ void print_list(NODE *head){ ...@@ -31,33 +31,42 @@ void print_list(NODE *head){
} }
} }
NODE_ERR psearch(NODE *head, datatype data){ NODE_INT psearch(NODE *head, datatype data){
//return:
// positive : index where data was found; NODE* return is pointer to where
// -1 : not found; NODE* return is NULL
printf("Searching list for: %d\n", data); printf("Searching list for: %d\n", data);
NODE_ERR node = search(head, data); NODE_INT node = search(head, data);
if(node.node != NULL) if(node.node != NULL)
printf("Location found, index: %d.\n", node.err); printf("Location found, index: %d.\n", node.num);
else else
puts("Not found in this list."); puts("Not found in this list.");
return node; return node;
} }
NODE_ERR search(NODE *head, datatype data){ NODE_INT search(NODE *head, datatype data){
NODE_ERR curr; //return:
// positive : index where data was found; NODE* return is pointer to where
// -1 : not found; NODE* return is NULL
NODE_INT curr;
curr.node = head; curr.node = head;
int i; int i;
for(i=0;curr.node != NULL;i++){ for(i=0;curr.node != NULL;i++){
if(curr.node->data == data){ if(curr.node->data == data){
curr.err=i; curr.num=i;
return curr; return curr;
} }
curr.node=curr.node->next; curr.node=curr.node->next;
} }
curr.node = NULL; curr.node = NULL;
curr.err = -1; curr.num = -1;
return curr; return curr;
} }
NODE *construct(datatype data, NODE *next){ NODE *construct(datatype data, NODE *next){
//return:
// pointer to new node : normal
// NULL : malloc failed
NODE *out = malloc(sizeof(NODE)); NODE *out = malloc(sizeof(NODE));
if(out != NULL){ if(out != NULL){
out->data = data; out->data = data;
...@@ -66,20 +75,33 @@ NODE *construct(datatype data, NODE *next){ ...@@ -66,20 +75,33 @@ NODE *construct(datatype data, NODE *next){
return out; return out;
} }
NODE *traverse(NODE *head, int pos){ NODE_INT traverse(NODE *head, int pos){
//return:
// 0 : normal
// 1 : head is null; NODE* return is NULL
// 2 : reached end of list; NODE* return is final element
NODE_INT out;
out.node = head;
out.num = 1;
if(head==NULL) if(head==NULL)
return NULL; return out;
int i; int i;
out.num = 0;
for(i=0;i<pos || pos<0;i++){ for(i=0;i<pos || pos<0;i++){
if(head->next != NULL) if(out.node->next != NULL)
head = head->next; out.node = out.node->next;
else else{
out.num = 2;
break; break;
}
} }
return head; return out;
} }
int delete_node_after(NODE *head){ int delete_node_after(NODE *head){
//return:
// 0 : normal
// 1 : head is NULL, or head->next is NULL, which means no node after to delete
if(head==NULL || head->next == NULL) if(head==NULL || head->next == NULL)
return 1; return 1;
NODE* tmp = head->next; NODE* tmp = head->next;
...@@ -97,6 +119,9 @@ void delete_list(NODE *head){ ...@@ -97,6 +119,9 @@ void delete_list(NODE *head){
} }
NODE *construct_list(datatype list[], int size){ NODE *construct_list(datatype list[], int size){
//return:
// pointer to head : normal
// NULL : malloc failed
if(size<=0) if(size<=0)
return NULL; return NULL;
NODE *next = construct(list[size-1],NULL); NODE *next = construct(list[size-1],NULL);
...@@ -116,10 +141,20 @@ int add_to_end(NODE *head, datatype new_data){ ...@@ -116,10 +141,20 @@ int add_to_end(NODE *head, datatype new_data){
} }
int insert_node(NODE *head, int pos, datatype new_data){ int insert_node(NODE *head, int pos, datatype new_data){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : malloc failed
// 3 : pos is larger than length(head)
if(head==NULL) if(head==NULL)
return 1; return 1;
if(pos!=0) if(pos!=0){
return insert_node_after(traverse(head,pos-1),new_data); NODE_INT traverseResult = traverse(head,pos-1);
int insertResult;
if(traverseResult.num == 2 && pos > 0)
return 3;
return insert_node_after(traverseResult.node,new_data);
}
else{ else{
int out = insert_node_after(head,head->data); int out = insert_node_after(head,head->data);
if(out>0) if(out>0)
...@@ -130,6 +165,10 @@ int insert_node(NODE *head, int pos, datatype new_data){ ...@@ -130,6 +165,10 @@ int insert_node(NODE *head, int pos, datatype new_data){
} }
int insert_node_after(NODE *head, datatype data){ int insert_node_after(NODE *head, datatype data){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : malloc failed
if(head==NULL) if(head==NULL)
return 1; return 1;
NODE *new = construct(data, head->next); NODE *new = construct(data, head->next);
...@@ -141,6 +180,7 @@ int insert_node_after(NODE *head, datatype data){ ...@@ -141,6 +180,7 @@ int insert_node_after(NODE *head, datatype data){
int length(NODE *head){ int length(NODE *head){
//returns length of list
if(head==NULL) if(head==NULL)
return 0; return 0;
int i=1; int i=1;
......
...@@ -6,19 +6,20 @@ typedef struct Node ...@@ -6,19 +6,20 @@ typedef struct Node
struct Node *next; struct Node *next;
}NODE; }NODE;
typedef struct Node_Err typedef struct Node_Int
{ {
NODE *node; NODE *node;
int err; int num;
}NODE_ERR; }NODE_INT;
void print_node_at(NODE *head, int pos); void print_node_at(NODE *head, int pos);
void print_list(NODE *head); void print_list(NODE *head);
void print_node(NODE *head); void print_node(NODE *head);
NODE_ERR psearch(NODE *head, datatype data); NODE_INT psearch(NODE *head, datatype data);
NODE_ERR search(NODE *head, datatype data); NODE_INT search(NODE *head, datatype data);
NODE *construct(datatype data, NODE *next); NODE *construct(datatype data, NODE *next);
NODE *traverse(NODE *head, int pos); NODE_INT traverse(NODE *head, int pos);
//NODE_INT traverse_before(NODE *head, int pos, int before);
int delete_node_after(NODE *head); int delete_node_after(NODE *head);
void delete_list(NODE *head); void delete_list(NODE *head);
//int delete_node_at(NODE *head, int pos); //int delete_node_at(NODE *head, int pos);
......
...@@ -19,8 +19,8 @@ int main(){ ...@@ -19,8 +19,8 @@ int main(){
print_list(root); print_list(root);
printf("The head is still at %p.\n", root); printf("The head is still at %p.\n", root);
puts("Searching for 5..."); puts("Searching for 5...");
NODE_ERR target = search(root, 5); NODE_INT target = search(root, 5);
printf("I found it at index %d.\n", target.err); printf("I found it at index %d.\n", target.num);
fputs("The number I found there was: ", stdout); fputs("The number I found there was: ", stdout);
print_node(target.node); print_node(target.node);
puts("Deleting list..."); puts("Deleting list...");
......
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