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
./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
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
compile : test.out main.out
all : test.out main.out
test : test.out
./test.out
tabify : *.h
tabify : *.h *.c
for file in *.c *.h;\
do\
unexpand -t4 --first-only $$file >temp ;\
mv temp $$file;\
done
gitindex : tabify *.c *.h
git add *.c *.h
......@@ -3,6 +3,7 @@
#include "llist.h"
void print_node_at(NODE *head, int pos){
//prints by index
NODE *currentNode;
currentNode = head;
......@@ -14,11 +15,10 @@ void print_node_at(NODE *head, int pos){
}
void print_node(NODE *head){
//prints head
printf("%d\n", head->data);}
void print_list(NODE *head){
// printf("Printing list...\n");
NODE *currentNode;
currentNode = 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);
NODE_ERR node = search(head, data);
NODE_INT node = search(head, data);
if(node.node != NULL)
printf("Location found, index: %d.\n", node.err);
printf("Location found, index: %d.\n", node.num);
else
puts("Not found in this list.");
return node;
}
NODE_ERR search(NODE *head, datatype data){
NODE_ERR curr;
NODE_INT search(NODE *head, datatype data){
//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;
int i;
for(i=0;curr.node != NULL;i++){
if(curr.node->data == data){
curr.err=i;
curr.num=i;
return curr;
}
curr.node=curr.node->next;
}
curr.node = NULL;
curr.err = -1;
curr.num = -1;
return curr;
}
NODE *construct(datatype data, NODE *next){
//return:
// pointer to new node : normal
// NULL : malloc failed
NODE *out = malloc(sizeof(NODE));
if(out != NULL){
out->data = data;
......@@ -66,20 +75,33 @@ NODE *construct(datatype data, NODE *next){
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)
return NULL;
return out;
int i;
out.num = 0;
for(i=0;i<pos || pos<0;i++){
if(head->next != NULL)
head = head->next;
else
if(out.node->next != NULL)
out.node = out.node->next;
else{
out.num = 2;
break;
}
return head;
}
return out;
}
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)
return 1;
NODE* tmp = head->next;
......@@ -97,6 +119,9 @@ void delete_list(NODE *head){
}
NODE *construct_list(datatype list[], int size){
//return:
// pointer to head : normal
// NULL : malloc failed
if(size<=0)
return NULL;
NODE *next = construct(list[size-1],NULL);
......@@ -116,10 +141,20 @@ int add_to_end(NODE *head, 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)
return 1;
if(pos!=0)
return insert_node_after(traverse(head,pos-1),new_data);
if(pos!=0){
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{
int out = insert_node_after(head,head->data);
if(out>0)
......@@ -130,6 +165,10 @@ int insert_node(NODE *head, int pos, datatype new_data){
}
int insert_node_after(NODE *head, datatype data){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : malloc failed
if(head==NULL)
return 1;
NODE *new = construct(data, head->next);
......@@ -141,6 +180,7 @@ int insert_node_after(NODE *head, datatype data){
int length(NODE *head){
//returns length of list
if(head==NULL)
return 0;
int i=1;
......
......@@ -6,19 +6,20 @@ typedef struct Node
struct Node *next;
}NODE;
typedef struct Node_Err
typedef struct Node_Int
{
NODE *node;
int err;
}NODE_ERR;
int num;
}NODE_INT;
void print_node_at(NODE *head, int pos);
void print_list(NODE *head);
void print_node(NODE *head);
NODE_ERR psearch(NODE *head, datatype data);
NODE_ERR search(NODE *head, datatype data);
NODE_INT psearch(NODE *head, datatype data);
NODE_INT search(NODE *head, datatype data);
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);
void delete_list(NODE *head);
//int delete_node_at(NODE *head, int pos);
......
......@@ -19,8 +19,8 @@ int main(){
print_list(root);
printf("The head is still at %p.\n", root);
puts("Searching for 5...");
NODE_ERR target = search(root, 5);
printf("I found it at index %d.\n", target.err);
NODE_INT target = search(root, 5);
printf("I found it at index %d.\n", target.num);
fputs("The number I found there was: ", stdout);
print_node(target.node);
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