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

final

added extra functions to test.c and main.c
cleaned up print_node_at()
parent 1eed34dd
...@@ -7,10 +7,10 @@ test.out : tabify llist.c llist.h test.c ...@@ -7,10 +7,10 @@ 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 : tabify 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 -o main.out llist.c main.c
test2.out : tabify llist.c llist.h test2.c test2.out : tabify llist.c llist.h test2.c
gcc -std=c99 -o test2.out llist.c test2.c gcc -o test2.out llist.c test2.c
all : test.out main.out all : test.out main.out
......
...@@ -2,16 +2,25 @@ ...@@ -2,16 +2,25 @@
#include<stdlib.h> #include<stdlib.h>
#include "llist.h" #include "llist.h"
void print_node_at(NODE *head, int pos){ int print_node_at(NODE *head, int pos){
//return:
// 0 : normal
// 1 : head is null
// 2 : pos is outside list
//prints by index //prints by index
NODE *currentNode; NODE_INT currentNode;
currentNode = head;
for (int i = 0; i<pos; i++){ currentNode = traverse(head, pos);
currentNode = currentNode->next; switch(currentNode.num){
case 0:
printf("The data at index %d is: %d\n", pos, currentNode.node->data);
return 0;
case 1:
return 1;
case 2:
puts("That's outside the list!");
return 2;
} }
printf("The data at index %d is: %d\n", pos, currentNode->data);
} }
void print_node(NODE *head){ void print_node(NODE *head){
...@@ -131,20 +140,6 @@ NODE_INT traverse(NODE *head, int pos){ ...@@ -131,20 +140,6 @@ NODE_INT traverse(NODE *head, int pos){
return out; return out;
} }
//NODE_INT traverse_before(NODE *head, int pos, int offset){
// int newPos = pos-offset;
// NODE_INT returnCode = traverse(head, newPos);
// switch(returnCode.num){
// case 0: printf("Case 0-> here is the data: %d\n", returnCode.node->data);
// break;
// case 1: printf("Case 1-> here is the data: %d\n",returnCode.node->data);
// break;
// case 2: printf("Not in the list; returning final data point: %d\n",returnCode.node->data);
// break;
// }
//}
NODE_INT traverse_before(NODE *head, int pos, unsigned before){ NODE_INT traverse_before(NODE *head, int pos, unsigned before){
//return: //return:
// 0 : normal // 0 : normal
......
...@@ -12,7 +12,7 @@ typedef struct Node_Int ...@@ -12,7 +12,7 @@ typedef struct Node_Int
int num; int num;
}NODE_INT; }NODE_INT;
void print_node_at(NODE *head, int pos); int 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_INT psearch(NODE *head, datatype data); NODE_INT psearch(NODE *head, datatype data);
......
...@@ -10,28 +10,39 @@ int menu(struct Node *head){ ...@@ -10,28 +10,39 @@ int menu(struct Node *head){
int input, index, value; int input, index, value;
puts("\nWelcome to the menu for creation of linked lists."); puts("\nWelcome to the menu for creation of linked lists.");
puts("Displaying possible actions:"); puts("Displaying possible actions:");
puts("1. Add at End;\n2. Delete Node;\n3. Insert Node;\n4. Search for value;\n5. Print a node;\n6. Print full list;\n7. Delete list and exit;\n8. Print node prior to selected.\n"); puts("0. Add at end;\n1. Insert node;\n2. Delete node by value;\n3. Delete node at index;\n4. Search for value;\n5. Print a node;\n6. Print full list;\n7. Print node prior to selected;\n8. Delete list and exit.\n");
fputs("Please input the number of your desired action: ",stdout); fputs("Please input the number of your desired action: ",stdout);
scanf("%d",&input); scanf("%d",&input);
switch(input) switch(input)
{ {
case 1: //append case 0: //append
fputs("You have decided to insert a value at the end. Input the desired value now: ",stdout); fputs("You have decided to insert a value at the end. Input the desired value now: ",stdout);
scanf("%d",&value); scanf("%d",&value);
add_to_end(head, value); add_to_end(head, value);
break; break;
case 2: //delete node at case 1: //insert
fputs("You have decided to delete a node. Input it: ",stdout);
scanf("%d",&index);
delete_node_at(head, index);
break;
case 3: //insert
fputs("You have decided to insert a value at a specific point. Input the node index now: ",stdout); fputs("You have decided to insert a value at a specific point. Input the node index now: ",stdout);
scanf("%d",&index); scanf("%d",&index);
fputs("Please input the value now: ",stdout); fputs("Please input the value now: ",stdout);
scanf("%d",&value); scanf("%d",&value);
insert_node(head, index, value); insert_node(head, index, value);
break; break;
case 2: //delete node by value
fputs("You have decided to delete a node. Input it: ",stdout);
scanf("%d",&value);
if(delete_node(head, value)!=2){
delete_list(head);
return 1;
}
break;
case 3: //delete node at index
fputs("You have decided to delete a node. Input the index: ",stdout);
scanf("%d",&index);
if(delete_node_at(head, index)!=2){
delete_list(head);
return 1;
}
break;
case 4: //search case 4: //search
fputs("You have selected to search for a value. Please input the value now: ",stdout); fputs("You have selected to search for a value. Please input the value now: ",stdout);
scanf("%d",&value); scanf("%d",&value);
...@@ -46,18 +57,18 @@ int menu(struct Node *head){ ...@@ -46,18 +57,18 @@ int menu(struct Node *head){
puts("Printing full list."); puts("Printing full list.");
print_list(head); print_list(head);
break; break;
case 7: //delete list case 7: //return data one areas before
puts("Deleting the list...");
delete_list(head);
return 1;
case 8: //return data two areas before
puts("Returning data at offset"); puts("Returning data at offset");
fputs("Please input the node index now: ",stdout); fputs("Please input the node index now: ",stdout);
scanf("%d",&index); scanf("%d",&index);
fputs("Please input the offset prior now: ",stdout); fputs("Please input the offset prior now: ",stdout);
scanf("%d",&value); scanf("%d",&value);
traverseBefore(head,index,value); print_node(traverse_before(head,index,value).node);
break; break;
case 8: //delete list
puts("Deleting the list...");
delete_list(head);
return 1;
} }
return 0; return 0;
} }
......
...@@ -23,9 +23,20 @@ int main(){ ...@@ -23,9 +23,20 @@ int main(){
printf("I found it at index %d.\n", target.num); 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 the element at index 3...");
target = traverse(root, 3);
delete_node_at(root, 3);
puts("Now it's garbage:");
print_node(target.node);
puts("Deleting the first occurrence of 1...");
target = search(root, 5);
delete_node(root, 5);
puts("Now it's garbage:");
print_node(target.node);
print_list(root);
puts("Deleting list..."); puts("Deleting list...");
delete_list(root); delete_list(root);
puts("Now it is garbage:"); puts("Now it's garbage:");
print_list(root); print_list(root);
return 0; return 0;
......
...@@ -3,16 +3,5 @@ ...@@ -3,16 +3,5 @@
int main() int main()
{ {
return 0;
} }
//
// 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