Commit 2a252acf authored by Liam E. Roeth's avatar Liam E. Roeth

add delete_list, separate print_node and print_node_at

add delete_list()
separate print_node(NODE* head), which prints the data of head, and
  print_node_at(NODE* head, int pos), which traverses head to index pos, then
  prints that node
parent 7ad5eedc
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include<stdlib.h> #include<stdlib.h>
#include "llist.h" #include "llist.h"
void print_node(NODE *head, int pos){ void print_node_at(NODE *head, int pos){
NODE *currentNode; NODE *currentNode;
currentNode = head; currentNode = head;
...@@ -13,9 +13,11 @@ void print_node(NODE *head, int pos){ ...@@ -13,9 +13,11 @@ void print_node(NODE *head, int pos){
printf("The data at index %d is: %d", pos, currentNode->data); printf("The data at index %d is: %d", pos, currentNode->data);
} }
void print_node(NODE *head){
printf("%d\n", head->data);}
void print_list(NODE *head){ void print_list(NODE *head){
printf("Printing list...\n"); // printf("Printing list...\n");
NODE *currentNode; NODE *currentNode;
currentNode = head; currentNode = head;
...@@ -87,6 +89,14 @@ int delete_node_after(NODE *head){ ...@@ -87,6 +89,14 @@ int delete_node_after(NODE *head){
return 0; return 0;
} }
void delete_list(NODE *head){
if(head == NULL)
return;
NODE *tmp = head->next;
free(head);
delete_list(tmp);
}
NODE *construct_list(datatype list[], int size){ NODE *construct_list(datatype list[], int size){
if(size<=0) if(size<=0)
return NULL; return NULL;
......
...@@ -12,13 +12,17 @@ typedef struct Node_Err ...@@ -12,13 +12,17 @@ typedef struct Node_Err
int err; int err;
}NODE_ERR; }NODE_ERR;
void print_node(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);
NODE_ERR psearch(NODE *head, datatype data); NODE_ERR psearch(NODE *head, datatype data);
NODE_ERR search(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);
void delete_list(NODE *head);
//int delete_node_at(NODE *head, int pos);
//int delete_node(NODE *head, datatype data);
NODE *construct_list(datatype list[], int size); NODE *construct_list(datatype list[], int size);
int add_to_end(NODE *head, datatype new_data); 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);
......
...@@ -3,15 +3,20 @@ ...@@ -3,15 +3,20 @@
#include "llist.h" #include "llist.h"
int main(){ int main(){
NODE *two = construct(2, NULL); NODE *two = construct(5, NULL);
NODE *one = construct(1, two); NODE *one = construct(1, two);
NODE *root = construct(0, one); NODE *root = construct(3, one);
print_list(root); print_list(root);
NODE *target = search(root, 2); printf("Searching for 5...\n");
print_node(target); NODE_ERR target = search(root, 5);
printf("I found it at index %d\n", target.err);
printf("The number I found there was ");
print_node(target.node);
printf("Deleting list...\n");
delete_list(root); delete_list(root);
printf("Now it is garbage:\n");
print_list(root); print_list(root);
return 1; 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