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

separate search and psearch

moved the portions of search() that call printf to psearch(), so that it can be
used silently
parent 118be38a
...@@ -30,20 +30,30 @@ void print_list(NODE *head){ ...@@ -30,20 +30,30 @@ void print_list(NODE *head){
} }
NODE* search(NODE *head, int pos){ NODE_ERR psearch(NODE *head, datatype data){
printf("Searching list for: %d", pos); printf("Searching list for: %d", data);
NODE_ERR node = search(head, data);
NODE *currentNode; if(node.node != NULL)
currentNode = head; printf("Location found, index: %d.", node.err);
else
printf("Not found in this list.");
return node;
}
while (currentNode != NULL){ NODE_ERR search(NODE *head, datatype data){
if(currentNode->data == pos){ NODE_ERR currentNode;
printf("Location found, index: %d.", currentNode); curr.node = head;
return currentNode; int i;
for(i=0;curr.node != NULL;i++){
if(curr.node->data == data){
curr.err=i;
return curr;
} }
currentNode=currentNode->next; curr.node=curr.node->next;
} }
printf("Not found in this list."); curr.node = NULL;
curr.err = -1;
return curr;
} }
NODE *construct(datatype data, NODE *next){ NODE *construct(datatype data, NODE *next){
......
...@@ -14,7 +14,8 @@ typedef struct Node_Err ...@@ -14,7 +14,8 @@ typedef struct Node_Err
void print_node(NODE *head, int pos); void print_node(NODE *head, int pos);
void print_list(NODE *head); void print_list(NODE *head);
NODE *search(NODE *head, datatype data); NODE_ERR psearch(NODE *head, datatype data);
NODE_ERR search(NODE *head, datatype data);
NODE *construct(datatype data, NODE *next); NODE *construct(datatype data, NODE *next);
NODE *traverse(NODE *head, int pos); NODE *traverse(NODE *head, int pos);
int delete_node_after(NODE *head); int delete_node_after(NODE *head);
......
...@@ -21,7 +21,7 @@ void menu(struct Node *head){ ...@@ -21,7 +21,7 @@ void menu(struct Node *head){
case 4: int val; case 4: int val;
printf("You have selected to search for a value. Please input the value now: "); printf("You have selected to search for a value. Please input the value now: ");
scanf("%d",&val); scanf("%d",&val);
search(val,*head); psearch(val,*head);
break; break;
case 5: int node; case 5: int node;
printf("You have selected to print a node. Please input the node index now: "); printf("You have selected to print a node. Please input the node index now: ");
......
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