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
gcc -ggdb -std=c99 -o test.out llist.c test.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
gcc -std=c99 -o test2.out llist.c test2.c
gcc -o test2.out llist.c test2.c
all : test.out main.out
......
......@@ -2,16 +2,25 @@
#include<stdlib.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
NODE *currentNode;
currentNode = head;
NODE_INT currentNode;
for (int i = 0; i<pos; i++){
currentNode = currentNode->next;
currentNode = traverse(head, pos);
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){
......@@ -131,20 +140,6 @@ NODE_INT traverse(NODE *head, int pos){
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){
//return:
// 0 : normal
......
......@@ -12,7 +12,7 @@ typedef struct Node_Int
int num;
}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_node(NODE *head);
NODE_INT psearch(NODE *head, datatype data);
......
......@@ -10,28 +10,39 @@ int menu(struct Node *head){
int input, index, value;
puts("\nWelcome to the menu for creation of linked lists.");
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);
scanf("%d",&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);
scanf("%d",&value);
add_to_end(head, value);
break;
case 2: //delete node at
fputs("You have decided to delete a node. Input it: ",stdout);
scanf("%d",&index);
delete_node_at(head, index);
break;
case 3: //insert
case 1: //insert
fputs("You have decided to insert a value at a specific point. Input the node index now: ",stdout);
scanf("%d",&index);
fputs("Please input the value now: ",stdout);
scanf("%d",&value);
insert_node(head, index, value);
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
fputs("You have selected to search for a value. Please input the value now: ",stdout);
scanf("%d",&value);
......@@ -46,18 +57,18 @@ int menu(struct Node *head){
puts("Printing full list.");
print_list(head);
break;
case 7: //delete list
puts("Deleting the list...");
delete_list(head);
return 1;
case 8: //return data two areas before
case 7: //return data one areas before
puts("Returning data at offset");
fputs("Please input the node index now: ",stdout);
scanf("%d",&index);
fputs("Please input the offset prior now: ",stdout);
scanf("%d",&value);
traverseBefore(head,index,value);
print_node(traverse_before(head,index,value).node);
break;
case 8: //delete list
puts("Deleting the list...");
delete_list(head);
return 1;
}
return 0;
}
......
......@@ -23,9 +23,20 @@ int main(){
printf("I found it at index %d.\n", target.num);
fputs("The number I found there was: ", stdout);
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...");
delete_list(root);
puts("Now it is garbage:");
puts("Now it's garbage:");
print_list(root);
return 0;
......
......@@ -3,16 +3,5 @@
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