Kellen D. Hoose's FALL 2013 Opus
This semester I'm going to do my best to improve upon my skills and knowledge, hopefully even make some progress in this pursuit.
To avoid future issues and clutter I will be listing all my files; names and descriptions. This will allow easier lookup and referencing in the future.
Today we covered how to setup and use the IRC chat and join our class channel, then we were shown where the Opus was and how to edit it.
lab46:~/src/DATA$ ./ptr1 [a] address is: 0x224BA9CC [a] contains: 12 [b] address is: 0x224BA9C0 [b] contains: 0x224BA9CC [b] dereferences to: 12 [a] address is: 0x224BA9CC [a] contains: 137 [b] address is: 0x224BA9C0 [b] contains: 0x224BA9CC [b] dereferences to: 137 [a] address is: 0x224BA9CC [a] contains: 2600 [b] address is: 0x224BA9C0 [b] contains: 0x224BA9CC [b] dereferences to: 2600 lab46:~/src/DATA$
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 7 int a, *b; 8 a=12; 9 b=&a; 10 fprintf(stdout, "[a] address is: 0x%X\n", &a); 11 fprintf(stdout, "[a] contains: %d\n", a); 12 // fprintf(stdout, "[a] dereferences to: %d\n", a); 13 14 fprintf(stdout, "[b] address is: 0x%X\n", &b); 15 fprintf(stdout, "[b] contains: 0x%X\n", b); 16 fprintf(stdout, "[b] dereferences to: %d\n",*b); 17 18 a=137; 19 20 fprintf(stdout, "[a] address is: 0x%X\n", &a); 21 fprintf(stdout, "[a] contains: %d\n", a); 22 // fprintf(stdout, "[a] dereferences to: %d\n", a); 23 24 fprintf(stdout, "[b] address is: 0x%X\n", &b); 25 fprintf(stdout, "[b] contains: 0x%X\n", b); 26 fprintf(stdout, "[b] dereferences to: %d\n",*b); 27 28 *b=2600; 29 30 fprintf(stdout, "[a] address is: 0x%X\n", &a); 31 fprintf(stdout, "[a] contains: %d\n", a); 32 // fprintf(stdout, "[a] dereferences to: %d\n", a); 33 34 fprintf(stdout, "[b] address is: 0x%X\n", &b); 35 fprintf(stdout, "[b] contains: 0x%X\n", b); 36 fprintf(stdout, "[b] dereferences to: %d\n",*b); 37 return(0); 38 }
Today we began to cover the variable size program required to reacquaint ourselves with variable usage. When we finished for the day we had the following finished:
lab46:~/src/DATA$ ./varsize a signed char is 1 bytes lower bound is: -128 Upper bound is: 127 -------- an unsigned char is 1 bytes lower bound is: 0 Upper bound is: 255 -------- lab46:~/src/DATA$
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 signed char sc=0; 7 unsigned char uc=0; 8 9 printf("a signed char is %hhu bytes\n", sizeof(sc)); 10 printf("lower bound is: %hhd\n", ((unsigned char)(sc-1)/2)+1); 11 printf("Upper bound is: %hhd\n", ((unsigned char)(sc-1)/2)); 12 printf("--------\n"); 13 printf("an unsigned char is %hhu bytes \n", sizeof(uc)); 14 printf("lower bound is: %hhu\n", uc); 15 printf("Upper bound is: %hhu\n", uc-1); 16 printf("--------\n"); 17 return(0); 18 }
We are now given the challenge to expand this program to encompass all data types, or even to find bounds for some using a LOGICAL solution.
Today I finished the data size program for class and I have it listed below:
lab46:~/src/DATA$ ls link ptr1 ptr2 struct1.c varsize.c varsize2.c link.c ptr1.c struct1 varsize varsize2 lab46:~/src/DATA$ ./varsize2 a signed char is 1 bytes lower bound is: -128 Upper bound is: 127 -------- an unsigned char is 1 bytes lower bound is: 0 Upper bound is: 255 -------- a signed short int is 2 bytes lower bound is: -32768 Upper bound is: 32767 -------- an unsigned short int is 2 bytes lower bound is: 0 Upper bound is: 65535 -------- a signed int is 4 bytes lower bound is: -2147483648 Upper bound is: 2147483647 -------- an unsigned int is 4 bytes lower bound is: 0 Upper bound is: 4294967295 -------- a signed long int is 8 bytes lower bound is: -9223372036854775808 Upper bound is: 9223372036854775807 -------- an unsigned long int is 8 bytes lower bound is: 0 Upper bound is: 18446744073709551615 -------- a signed float is 0.000000 bytes lower bound is: 0.500000 Upper bound is: -0.500000 -------- an unsigned float is 0.000000 bytes lower bound is: 0.000000 Upper bound is: -1.000000 -------- lab46:~/src/DATA$
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 signed char sc=0; 7 unsigned char uc=0; 8 signed short ssi=0; 9 unsigned short usi=0; 10 signed int si=0; 11 unsigned int ui=0; 12 signed long sl=0; 13 unsigned long ul=0; 14 signed long long sll=0; 15 unsigned long long ull=0; 16 float f=0; 17 18 19 printf(" "); 20 printf("a signed char is %hhd bytes\n", sizeof(sc)); 21 printf("lower bound is: %hhd\n", ((unsigned char)(sc-1)/2)+1); 22 printf("Upper bound is: %hhd\n", ((unsigned char)(sc-1)/2)); 23 printf("--------\n"); 24 printf("an unsigned char is %hhu bytes \n", sizeof(uc)); 25 printf("lower bound is: %hhu\n", uc); 26 printf("Upper bound is: %hhu\n", uc-1); 27 printf("--------\n"); 28 printf("a signed short int is %hd bytes\n", sizeof(ssi)); 29 printf("lower bound is: %hd\n", ((unsigned short)(ssi-1)/2)+1); 30 printf("Upper bound is: %hd\n", ((unsigned short)(ssi-1)/2)); 31 printf("--------\n"); 32 printf("an unsigned short int is %hu bytes \n", sizeof(usi)); 33 printf("lower bound is: %hu\n", usi); 34 printf("Upper bound is: %hu\n", usi-1); 35 printf("--------\n"); 36 printf("a signed int is %d bytes\n", sizeof(si)); 37 printf("lower bound is: %d\n", ((unsigned int)(si-1)/2)+1); 38 printf("Upper bound is: %d\n", ((unsigned int)(si-1)/2)); 39 printf("--------\n"); 40 printf("an unsigned int is %u bytes \n", sizeof(ui)); 41 printf("lower bound is: %u\n", ui); 42 printf("Upper bound is: %u\n", ui-1); 43 printf("--------\n"); 44 printf("a signed long int is %ld bytes\n", sizeof(sl)); 45 printf("lower bound is: %ld\n", ((unsigned long int)(sl-1)/2)+1); 46 printf("Upper bound is: %ld\n", ((unsigned long int)(sl-1)/2)); 47 printf("--------\n"); 48 printf("an unsigned long int is %lu bytes \n", sizeof(ul)); 49 printf("lower bound is: %lu\n", ul); 50 printf("Upper bound is: %lu\n", ul-1); 51 printf("--------\n"); 52 printf("a signed float is %f bytes\n", sizeof(f)); 53 printf("lower bound is: %f\n", (( float)(f-1)/2)+1); 54 printf("Upper bound is: %f\n", ((float)(f-1)/2)); 55 printf("--------\n"); 56 printf("an unsigned float is %f bytes \n", sizeof(f)); 57 printf("lower bound is: %f\n", f); 58 printf("Upper bound is: %f\n", f-1); 59 printf("--------\n"); 60 61 return(0); 62 }
Today we worked on structures and created a program that organized entries of names, ages, and ID#s, then spits them out as requested, it is listed below;
lab46:~/src/DATA$ ./struct1 Enter person #1's name: dan Enter dan's age:18 Enter dan's id number:1 Enter person #2's name: kell Enter kell's age:18 Enter kell's id number:2 Enter person #3's name: paul Enter paul's age:19 Enter paul's id number:3 Enter person #4's name: jon Enter jon's age:20 Enter jon's id number:4 Enter person #5's name: dick Enter dick's age:50 Enter dick's id number:5 Enter person #6's name: matt Enter matt's age:30 Enter matt's id number:6 Enter person #7's name: senator Enter senator's age:40 Enter senator's id number:7 Enter person #8's name: zach Enter zach's age:20 Enter zach's id number:8 Look up data for person #: 1 Name: kell Age: 18 ID: 2 lab46:~/src/DATA$
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 struct person { 7 char *name; 8 int age; 9 long int id; 10 }; 11 12 typedef struct person Person; 13 // struct person People[8]; 14 Person People[8]; 15 int i=0; 16 17 for(i=0;i<8;i++) 18 { 19 printf("Enter person #%d's name: ",(i+1)); 20 People[i].name=(char*)malloc(sizeof(char)*20); 21 scanf("%s", People[i].name); 22 printf("Enter %s's age:", People[i].name); 23 scanf("%d", &People[i].age); 24 printf("Enter %s's id number:", People[i].name); 25 scanf("%ld", &People[i].id); 26 } 27 i=-1; 28 while(i==-1) 29 { 30 printf("Look up data for person #: "); 31 scanf("%d", &i); 32 if(!((i>=0)&&(i<=7))) 33 { 34 printf("Invalid person #. Tme:%s\n. Try Again!\n"); 35 i=-1; 36 } 37 } 38 printf("Name: %s\n", People[i].name); 39 printf("Age: %d\n", People[i].age); 40 printf("ID: %ld\n", People[i].id); 41 return(0); 42 }
Today we began doing linked lists, the program we began today would take input in the form of numbers. The program stops taking input once '-1' is entered, then outputs the previously input numbers in the order they were input. The program is shown below:
lab46:~/src/DATA$ ls link ptr1 ptr2 struct1.c varsize.c varsize2.c link.c ptr1.c struct1 varsize varsize2 lab46:~/src/DATA$ ./link Enter a value(-1 to end): 2 Enter a value(-1 to end): 3 Enter a value(-1 to end): 4 Enter a value(-1 to end): 5 Enter a value(-1 to end): 6 Enter a value(-1 to end): 7 Enter a value(-1 to end): 8 Enter a value(-1 to end): 9 Enter a value(-1 to end): 10 Enter a value(-1 to end): 11 Enter a value(-1 to end): -1 2 ->3 ->4 ->5 ->6 ->7 ->8 ->9 ->10 ->11 ->NULL lab46:~/src/DATA$
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node 5 { 6 int value; 7 struct node *next; 8 }; 9 10 typedef struct node Node; 11 12 int main() 13 { 14 int input; 15 Node *list, *tmp; 16 list=tmp=NULL; 17 while(input!=-1) 18 { 19 printf("Enter a value(-1 to end): "); 20 scanf("%d", &input); 21 if(input!=-1) 22 { 23 if(list==NULL) 24 { 25 list=tmp=(Node*)malloc(sizeof(Node)); 26 tmp->next=NULL; 27 list->value=input; 28 } 29 else 30 { 31 tmp->next=(Node*)malloc(sizeof(Node)); 32 tmp->next->next=NULL; 33 tmp->next->value=input; 34 tmp=tmp->next; 35 } 36 } 37 } 38 tmp=list; 39 while(tmp!=NULL) 40 { 41 printf("%d ->", tmp->value); 42 tmp=tmp->next; 43 } 44 printf("NULL\n"); 45 return(0); 46 }
This Friday we had our progress checked by matt and began work on our modified linked list program, a branch off of 'link.c'
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node 5 { 6 int value; 7 struct node *next; 8 }; 9 10 typedef struct node Node; 11 12 int main() 13 { 14 int input; 15 Node *list, *tmp; 16 list=tmp=NULL; 17 while(input!=-1) 18 { 19 printf("Enter a value(-1 to end): "); 20 scanf("%d", &input); 21 if(input!=-1) 22 { 23 if(list==NULL) 24 { 25 list=tmp=(Node*)malloc(sizeof(Node)); 26 tmp->next=NULL; 27 list->value=input; 28 } 29 else 30 { 31 tmp->next=(Node*)malloc(sizeof(Node)); 32 tmp->next->next=NULL; 33 tmp->next->value=input; 34 tmp=tmp->next; 35 } 36 } 37 } 38 tmp=list; 39 input=0; 40 while(tmp!=NULL) 41 { 42 printf("[%d] %d ->", input,tmp->value); 43 tmp=tmp->next; 44 input=input+1; 45 } 46 printf("NULL\n"); 47 //insert into list 48 printf("Which node would you like to insert before?"); 49 scanf("%d",&input); 50 int seeker; 51 tmp=list; 52 Node *tmp2=NULL; 53 for(seeker=0;<=input;seeker++) 54 { 55 tmp=tmp->next; 56 } 57 printf("Enter a value to insert: "); 58 scanf("%d", &input); 59 tmp2=(node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp2->next=tmp->next; 62 tmp->next=tmp2; 63 return(0); 64 }
As is my habit I said screw it and put this off till it was just about too late, it's a bit simple but it does as requested, here is my truth table program executing and its code.
lab46:~/src/Discreet$ lab46:~/src/Discreet$ ./project0 |--------------------------------------| |Exclusive Disjunction/Non-equivalence:| |--------------------------------------| | P | Q | PyQ | |--------------------------------------| | T | T | F | | T | F | T | | F | T | T | | F | F | F | |--------------------------------------| For the following step; 1=True 0=False Please input value for P:1 Please input value for Q:1 The result is: False lab46:~/src/Discreet$ ./project0 |--------------------------------------| |Exclusive Disjunction/Non-equivalence:| |--------------------------------------| | P | Q | PyQ | |--------------------------------------| | T | T | F | | T | F | T | | F | T | T | | F | F | F | |--------------------------------------| For the following step; 1=True 0=False Please input value for P:1 Please input value for Q:0 The result is: True lab46:~/src/Discreet$ ./project0 |--------------------------------------| |Exclusive Disjunction/Non-equivalence:| |--------------------------------------| | P | Q | PyQ | |--------------------------------------| | T | T | F | | T | F | T | | F | T | T | | F | F | F | |--------------------------------------| For the following step; 1=True 0=False Please input value for P:0 Please input value for Q:0 The result is: False lab46:~/src/Discreet$
1 #include<stdlib.h> 2 #include<stdio.h> 3 4 int main() 5 { 6 int p; 7 int q; 8 int pyq; 9 10 11 12 printf("|--------------------------------------|\n"); 13 printf("|Exclusive Disjunction/Non-equivalence:|\n"); 14 printf("|--------------------------------------|\n"); 15 printf("| P | Q | PyQ |\n"); 16 printf("|--------------------------------------|\n"); 17 printf("| T | T | F |\n"); 18 printf("| T | F | T |\n"); 19 printf("| F | T | T |\n"); 20 printf("| F | F | F |\n"); 21 printf("|--------------------------------------|\n"); 22 23 24 25 printf("\n For the following step; 1=True 0=False\n Please input value for :"); 26 scanf("%d", &p); 27 printf("Please input value for Q:"); 28 scanf("%d",&q); 29 pyq=p+q; 30 31 32 33 34 if(pyq==2) 35 { 36 printf("The result is: False\n"); 37 } 38 else if(pyq==1) 39 { 40 printf("The result is: True\n"); 41 } 42 else if(pyq==0) 43 { 44 printf("The result is: False\n"); 45 } 46 47 return(0); 48 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node 5 { 6 int value; 7 struct node *next; 8 }; 9 10 typedef struct node Node; 11 12 int main() 13 { 14 int input; 15 Node *list, *tmp; 16 list=tmp=NULL; 17 while(input!=-1) 18 { 19 printf("Enter a value(-1 to end): "); 20 scanf("%d", &input); 21 if(input!=-1) 22 { 23 if(list==NULL) 24 { 25 list=tmp=(Node*)malloc(sizeof(Node)); 26 tmp->next=NULL; 27 list->value=input; 28 } 29 else 30 { 31 tmp->next=(Node*)malloc(sizeof(Node)); 32 tmp->next->next=NULL; 33 tmp->next->value=input; 34 tmp=tmp->next; 35 } 36 } 37 } 38 tmp=list; 39 input=0; 40 while(tmp!=NULL) 41 { 42 printf("[%d] %d ->", input,tmp->value); 43 tmp=tmp->next; 44 input=input+1; 45 } 46 printf("NULL\n"); 47 //insert into list 48 printf("Which node would you like to insert before?"); 49 scanf("%d",&input); 50 int seeker; 51 tmp=list; 52 Node *tmp2=NULL; 53 for(seeker=0;seeker<(input-1);seeker++) 54 { 55 tmp=tmp->next; 56 } 57 printf("Enter a value to insert: "); 58 scanf("%d", &input); 59 tmp2=(Node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp2->next=tmp->next; 62 tmp->next=tmp2; 63 return(0); 64 }
-this program asks you to fill a list, then to insert before a node including the first node if you would like, then it asks you to remove a node.
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node { 5 int value; 6 struct node *next; 7 }; 8 9 typedef struct node Node; 10 11 int main() 12 { 13 int input; 14 Node *list, *tmp; 15 16 list = tmp = NULL; 17 while (input != -1) 18 { 19 printf("Enter a value(-1 to end): "); 20 scanf("%d", &input); 21 if (input != -1) 22 { 23 if (list == NULL) 24 { 25 list = tmp = (Node *) malloc(sizeof(Node)); 26 tmp->next = NULL; 27 list->value = input; 28 } 29 else 30 { 31 tmp->next = (Node *) malloc(sizeof(Node)); 32 tmp->next->next = NULL; 33 tmp->next->value = input; 34 tmp = tmp->next; 35 } 36 } 37 } 38 tmp = list; 39 input = 0; 40 while (tmp != NULL) 41 { 42 printf("[%d] %d ->", input, tmp->value); 43 tmp = tmp->next; 44 input = input + 1; 45 } 46 printf("NULL\n"); 47 //insert into list 48 printf("Which node would you like to insert before?"); 49 scanf("%d", &input); 50 int seeker; 51 52 tmp = list; 53 Node *tmp2 = NULL; 54 55 for (seeker = 0; seeker < (input - 1); seeker++) 56 { 57 tmp = tmp->next; 58 } 59 if (input == 0) 60 { 61 printf("Enter value for new node: "); 62 scanf("%d", &input); 63 tmp2 = (Node *) malloc(sizeof(Node)); 64 tmp2->value = input; 65 tmp2->next = NULL; 66 tmp2->next = tmp; 67 list = tmp2; 68 input = 0; 69 tmp = list; 70 while (tmp != NULL) 71 { 72 printf("[%d] %d ->", input, tmp->value); 73 tmp = tmp->next; 74 input = input + 1; 75 } 76 printf("NULL\n"); 77 } 78 else 79 { 80 printf("Enter a value to insert: "); 81 scanf("%d", &input); 82 tmp2 = (Node *) malloc(sizeof(Node)); 83 tmp2->value = input; 84 tmp2->next = tmp->next; 85 tmp->next = tmp2; 86 input = 0; 87 tmp = list; 88 while (tmp != NULL) 89 { 90 printf("[%d] %d ->", input, tmp->value); 91 tmp = tmp->next; 92 input = input + 1; 93 } 94 printf("NULL\n"); 95 } 96 printf("Which node would you like to remove?: "); 97 scanf("%d", &input); 98 tmp = list; 99 for (seeker = 0; seeker < (input - 1); seeker++) 100 { 101 tmp = tmp->next; 102 } 103 if (input == 0) 104 { 105 list = tmp->next; 106 tmp->next = NULL; 107 } 108 109 else 110 { 111 tmp2 = tmp->next; 112 tmp->next = tmp2->next; 113 tmp2->next = NULL; 114 input = 0; 115 tmp = list; 116 } 117 tmp = list; 118 input = 0; 119 while (tmp != NULL) 120 { 121 printf("[%d] %d ->", input, tmp->value); 122 tmp = tmp->next; 123 input = input + 1; 124 } 125 126 printf("NULL\n"); 127 return (0); 128 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node { 5 int value; 6 struct node *next; 7 struct node *prev; 8 }; 9 typedef struct node Node; 10 11 struct list { 12 struct node *start; 13 struct node *end; 14 }; 15 typedef struct list List; 16 List *build(); 17 void displayf(List *); 18 void displayb(List *); 19 List *insert(List *, Node *, Node *); 20 List *getNode(List *, Node **); 21 22 main() 23 { 24 List *myList; 25 List *myList2; 26 Node *tmp; 27 28 myList = build(); 29 displayf(myList); 30 //call insert here 31 myList=getNode(myList, &tmp); 32 myList2=insert(myList2, myList->end, tmp); 33 // displayb(myList); 34 return (0); 35 } 36 37 List *build() 38 { 39 Node *tmp = NULL; 40 List *myList = (List *) malloc(sizeof(List)); 41 42 myList->start = myList->end = NULL; 43 int input = 0; 44 45 printf("Enter a value(-1 to quit): "); 46 scanf("%d", &input); 47 while (input != -1) 48 { 49 if (myList->start == NULL) 50 //went from start and end both =NULL to them both equalling a new node 51 { 52 myList->start = myList->end = 53 (Node *) malloc(sizeof(Node)); 54 myList->start->value = input; 55 myList->end->prev = myList->start->next = NULL; 56 } 57 else 58 { 59 myList->end->next = (Node *) malloc(sizeof(Node)); 60 myList->end->next->value = input; 61 myList->end->next->next = NULL; 62 myList->end->next->prev = myList->end; 63 myList->end = myList->end->next; 64 } 65 printf("Enter a value(-1 to quit): "); 66 scanf("%d", &input); 67 } 68 return (myList); 69 } 70 71 //Insert Section 72 List *insert(List *myList, Node *place, Node *newNode) 73 { 74 if (place == myList->start) 75 { 76 newNode->next = place; 77 place->prev = newNode; 78 newNode->prev = NULL; 79 myList->start = newNode; 80 } 81 //in case of before first node insertion 82 //links as before newnode appears, aasigning to where they are after appears 83 else 84 { 85 newNode->next = place; 86 place->prev->next = newNode; 87 newNode->prev = place->prev; 88 place->prev = newNode; 89 } 90 } 91 void displayf(List *myList) 92 { 93 Node *tmp; 94 int input; 95 tmp=myList->start; 96 input=0; 97 /* printf("[%d] %d ->", input, myList->start->value); 98 myList->start=myList->start->next; 99 input=input+1; 100 */ 101 while (tmp != NULL) 102 { 103 printf("[%d] %d ->", input, tmp->value); 104 tmp = tmp->next; 105 input = input + 1; 106 } 107 printf("NULL\n"); 108 } 109 110 //remove section 111 //passing by address, allows multiple value passing by single function 112 List *getNode(List *myList, Node **place) 113 { 114 //removal, start of list conditional 115 if(*place==myList->start) 116 { 117 myList->start=myList->start->next; 118 myList->start->prev=NULL; 119 (*place)->next=NULL; 120 } 121 122 //removal, end of list conditional 123 else if(*place==myList->end) 124 { 125 myList->end=myList->end->prev; 126 myList->end->next=NULL; 127 (*place)->prev=NULL; 128 } 129 130 //removal, every condition in between 131 else 132 { 133 (*place)->prev->next=(*place)->next;//skip over node one way 134 (*place)->next->prev=(*place)->prev;//skip over it the other 135 (*place)->prev=(*place)->next=NULL;//skipped over(cut off), NULL ends 136 } 137 return(myList); 138 }
1 #include "stack.h" 2 3 Node *peek(Stack *myStack) 4 { 5 // exercise left to the implementer 6 }
1 #include "stack.h" 2 3 Node *pop(Stack **myStack) 4 { 5 Node *tmp=NULL; 6 if((myStack)!=NULL) 7 { 7 tmp = getNode(&(*myStack) -> data, (*myStack) -> data -> end); 8 (*myStack) -> top = (*myStack) -> data -> end; 9 } 10 return (tmp); 11 }
1 #include "stack.h" 2 3 Stack *push(Stack *myStack, Node *newNode) 4 { 5 if ((myStack -> size <= 0) || ((myStack -> data -> qty) < (myStack -> si ze))) 6 { 7 myStack -> data = append(myStack -> data, myStack -> data -> end, ne wNode); 8 myStack -> top = myStack -> data -> end; 9 } 10 11 return(myStack); 12 }
1 #ifndef _STACK_H 2 #define _STACK_H 3 4 #include "list.h" 5 6 struct stack { 7 List *data; 8 Node *top; 9 int size; 10 }; 11 typedef struct stack Stack; 12 13 Stack *mkstack(int); 14 Stack *push (Stack *, Node *); 15 Node *pop (Stack **); 16 Node *peek (Stack *); 17 18 #endif
1 #include "stack.h" 2 3 Stack *mkstack(int size) 4 { 5 Stack *myStack; 6 7 myStack = (Stack *) malloc (sizeof(Stack)); 8 9 myStack -> data = mklist(); 10 myStack -> size = size; 11 myStack -> top = myStack -> data -> end; 12 13 return (myStack); 14 }
binary tree: - Three traversals;
It did take me quite a while, and asking for help but I have the makings of a binary tree program, yet it has some running errors, segmentation that occurs at the start, I'm unsure how to fix it.
executable as: binary.c
binary.c
1 #include <stdio.h> 2 #include "stack.h" 3 4 5 List *displaytree(List *maple); 6 List *addtree(List *pine, Node *pos1, Node *pos2); 7 8 int main() 9 { 10 List *tree = ( List* )malloc( sizeof( List ) ); 11 tree->start = tree->end = NULL; 12 Node *root; 13 int value0; 14 int choice; 15 fprintf(stdout, "What would you like for your root value? "); 16 fscanf(stdin, "%d", &value0); 17 18 19 root->value = value0; 20 tree = append( tree, tree->start, root); 21 tree = displaytree(tree); 22 23 while(choice!=-1) 24 { 25 fprintf(stdout, "|========================|\n"); 26 fprintf(stdout, "| 1 to insert to tree |\n"); 27 fprintf(stdout, "|========================|\n"); 28 fprintf(stdout, "| 2 to remove from tree |\n"); 29 fprintf(stdout, "|========================|\n"); 30 fprintf(stdout, "| 3 to display tree |\n"); 31 fprintf(stdout, "|========================|\n"); 32 fprintf(stdout, "| -1 to quit |\n"); 33 fprintf(stdout, "|========================|\n"); 34 fprintf(stdout, "Which would you like to do?\n"); 35 fscanf(stdin, "%d", &choice); 36 37 if(choice == 1) 38 { 39 int a; 40 fprintf(stdout, "What value are you adding? "); 41 fscanf(stdin, "%d", &a); 42 Node * tmp = (Node *)malloc(sizeof(Node)); 43 tmp->value = a; 44 tree = addtree(tree, root, tmp); 45 } 46 else if(choice == 3) 47 { 48 displaytree(tree); 49 } 50 else if(choice == -1) 51 { 52 fprintf(stdout, "Goodbye\n"); 53 } 54 else 55 { 56 fprintf(stdout, "Error, Invalid Input\n"); 57 } 58 } 59 return(0); 60 } 61 62 List *displaytree( List * maple) //this section output the content of the tree for the user to see. 63 { 64 Node *tmp1 = maple->start; 65 Node *tmp2 = maple->start; 66 fprintf(stdout, "\t%d\n", tmp1->value); 67 68 if(tmp1->next != NULL || tmp1->prev != NULL) 69 { 70 tmp1 = tmp1->next; 71 tmp2 = tmp2->next; 72 while(tmp1->next = NULL) 73 { 74 fprintf(stdout, "\t%d\n", tmp1->value); 75 fprintf(stdout, "\t%d\n", tmp2->value); 76 } 77 } 78 return(maple); 79 } 80 81 List *addtree( List *pine, Node *pos1, Node *pos2) //this section allows for the addition of new leaves to the binary tree. 82 { 83 int count = 1; 84 while(count != pine->qty) 85 { 86 if(pos2->value < pos1->value) 87 { 88 if(pos1->prev != NULL) 89 { 90 fprintf(stdout, "Down Left\n"); 91 pos1 = pos1->prev; 92 } 93 } 94 else if(pos2->value > pos1->value) 95 { 96 if(pos1->next != NULL) 97 { 98 fprintf(stdout, "Down Right\n"); 99 pos1 = pos1->next; 100 } 101 } 102 else if(pos2->value == pos1->value) 103 { 104 105 } 106 count++; 107 } 108 if(pos2->value < pos1->value) 109 { 110 fprintf(stdout, "Added a node to the left\n"); 111 pos1->prev = pos2; 112 pine->qty = (pine->qty) + 1; 113 } 114 else if(pos2->value > pos1->value) 115 { 116 fprintf(stdout, "Added a node to the right\n"); 117 pos1->next = pos2; 118 pine->qty = (pine->qty) + 1; 119 } 120 else 121 { 122 123 } 124 pos1 = pine->start; 125 return(pine); 126 } 127 128 List *append(List *myList, Node *place, Node *newNode) //allows for the appending of a leaf after the position of another leaf. 129 { 130 if(place==myList->end) 131 { 132 newNode->prev=place; 133 place->next=newNode; 134 newNode->next=NULL; 135 myList->end=newNode; 136 } 137 else 138 { 139 newNode->prev=place; 140 place->next->prev=newNode; 141 newNode->next=place->next; 142 place->next=newNode; 143 } 144 145 return(myList); 146 } 147 148 List *taketree( List *cherry, Node *pos ) //removal function for the binary tree 149 { 150 Node *tmp1 = cherry->start; 151 Node *tmp0; 152 int count = 0; 153 while(count != cherry->qty) 154 { 155 if(tmp1->value > pos->value) 156 { 157 tmp0 = tmp1; 158 if(tmp1->prev != NULL) 159 { 160 fprintf(stdout, "Down Left\n"); 161 tmp1 = tmp1->prev; 162 } 163 } 164 else if(tmp1->value < pos->value) 165 { 166 tmp0 = tmp1; 167 if(tmp1->next != NULL) 168 { 169 fprintf(stdout, "Down Right\n"); 170 tmp1 = tmp1->next; 171 } 172 } 173 else if(tmp1->value == pos->value) 174 { 175 176 } 177 count++; 178 } 179 fprintf(stdout, "tmp0 is %d\n", tmp0->value); 180 fprintf(stdout, "Removing node %d\n", tmp1->value); 181 if(tmp1->next != NULL && tmp1->prev != NULL) 182 { 183 Node *tmp2; 184 if(tmp1->prev != NULL) 185 { 186 tmp2 = tmp1->prev; 187 } 188 else 189 { 190 tmp2 = NULL; 191 } 192 193 Node *tmp3; 194 if(tmp1->next != NULL) 195 { 196 tmp3 = tmp1->next; 197 } 198 else 199 { 200 tmp3 = NULL; 201 } 202 if(tmp2 != NULL) 203 { 204 while(tmp2->next != NULL && tmp2->prev != NULL) 205 { 206 if(tmp2->next != NULL) 207 { 208 tmp2 = tmp2->next; 209 } 210 else 211 { 212 } 213 } 214 if(tmp1 == tmp0->prev) 215 { 216 tmp0->prev = tmp2; 217 tmp2->next = tmp3; 218 } 219 else 220 { 221 tmp0->next = tmp2; 222 tmp2->next = tmp3; 223 } 224 } 225 else 226 { 227 while(tmp3->next != NULL && tmp3->prev != NULL) 228 { 229 if(tmp3->next != NULL) 230 { 231 tmp3 = tmp3->next; 232 } 233 else 234 { 235 } 236 } 237 if(tmp1 == tmp0->prev) 238 { 239 tmp0->prev = tmp3; 240 tmp2->next = tmp2; 241 } 242 else 243 { 244 tmp0->next = tmp3; 245 tmp2->next = tmp2; 246 } 247 } 248 } 249 else 250 { 251 if(tmp1 == tmp0->prev) 252 { 253 tmp0->prev = NULL; 254 } 255 else 256 { 257 tmp0->next = NULL; 258 } 259 } 260 /* 261 tmp1->next = NULL; 262 tmp1->prev = NULL; 263 */ 264 free(tmp1); 265 266 return(cherry); 267 }
After a long wait, and tons of work I have completed my Singly Linked list, which uses a menu to run, making it much easier to navigate. The following is both the code for the menu, and then the individual functions at the bottom of the program. Menu includes the following functions:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct node{ 5 int value; 6 struct node*next; 7 }; 8 9 typedef struct node Node; 10 11 Node *build(Node *); 12 Node *insert(Node *); 13 Node *append(Node *); 14 Node *removenode(Node *); 15 Node *display(Node *); 16 17 int main() 18 { 19 Node *list = NULL; 20 int option; 21 22 while(option!=-1) 23 24 { 25 fprintf(stdout, "|===========Menu===========|\n"); 26 fprintf(stdout, "| 1.)Build list |\n"); 27 fprintf(stdout, "| 2.)Insert to list |\n"); 28 fprintf(stdout, "| 3.)Append to list |\n"); 29 fprintf(stdout, "| 4.)Remove from list |\n"); 30 fprintf(stdout, "| 5.)Display the list |\n"); 31 fprintf(stdout, "|-1.)Quit |\n"); 32 fprintf(stdout, "|==========================|\n"); 33 fprintf(stdout, "Please select an option: "); 34 fscanf(stdin, "%d", &option); 35 36 if(option==1) 37 { 38 list=build(list); 39 } 40 else if(option==2) 41 { 42 list=insert(list); 43 } 44 else if(option==4) 45 { 46 list=removenode(list); 47 } 48 else if(option==5) 49 { 50 list=display(list); 51 } 52 else if(option=-1) 53 { 54 fprintf(stdout, "Goodbye\n"); 55 } 56 else 57 { 58 fprintf(stdout, "Error, invalid input\n"); 59 } 60 } 61 62 return(0); 63 } 64 65 Node *build(Node*list) 66 { 67 int input=0; 68 Node*tmp; 69 list=tmp=NULL; 70 while(input!=-1) 71 { 72 fprintf(stdout, "Enter a value (-1 to end): "); 73 fscanf(stdin, "%d", &input); 74 75 if(input!=-1) 76 { 77 if(list==NULL) 78 { 79 list=tmp=(Node*)malloc(sizeof(Node)); 80 tmp->next=NULL; 81 list->value=input; 82 } 83 else 84 { 85 tmp->next=(Node*)malloc(sizeof(Node)); 86 tmp->next->next=NULL; 87 tmp->next->value=input; 88 tmp=tmp->next; 89 } 90 } 91 } 92 tmp=list; 93 94 return(list); 95 } 96 97 Node *insert(Node*list) 98 { 99 //insert into list 100 int input=0; 101 Node*tmp; 102 fprintf(stdout, "Which node would you like to insert before? "); 103 fscanf(stdin, "%d", &input); 104 int seeker; 105 tmp=list; 106 Node*tmp2=NULL; 107 for(seeker=0; seeker<(input-1); seeker++) 108 { 109 tmp=tmp->next; 110 } 111 if(input==0) 112 { 113 fprintf(stdout, "Enter value for new node: "); 114 fscanf(stdin, "%d", &input); 115 tmp2=(Node*)malloc(sizeof(Node)); 116 tmp2->value=input; 117 tmp2->next=NULL; 118 tmp2->next=tmp; 119 list=tmp2; 120 } 121 else 122 { 123 fprintf(stdout, "Enter a value to insert: "); 124 fscanf(stdin, "%d", &input); 125 tmp2=(Node*)malloc(sizeof(Node)); 126 tmp2->value=input; 127 tmp2->next=tmp->next; 128 tmp->next=tmp2; 129 } 130 tmp=list; 131 132 return(list); 133 } 134 Node *append(Node*list) 135 { 136 //Append to list 137 int input=0; 138 Node*tmp; 139 fprintf(stdout, "Enter what node you would like to append to: "); 140 fscanf(stdin, "%d", &input); 141 int behind; 142 tmp=list; 143 Node*tmp3=NULL; 144 for(behind=0; behind<input; behind++) 145 { 146 tmp=tmp->next; 147 } 148 149 fprintf(stdout, "What value would you like to append: "); 150 fscanf(stdin, "%d", &input); 151 tmp3=(Node*)malloc(sizeof(Node)); 152 tmp3->value=input; 153 tmp3->next=tmp->next; 154 tmp->next=tmp3; 155 156 tmp=list; 157 158 return(list); 159 } 160 161 Node *removenode(Node*list) 162 { 163 int input=0; 164 Node*tmp; 165 tmp=list; 166 Node*tmp2=NULL; 167 int begone; 168 fprintf(stdout, "Which node would you like to remove?: "); 169 fscanf(stdin, "%d", &input); 170 171 for(begone=0; begone<(input-1);begone++) 172 { 173 tmp=tmp->next; 174 } 175 if(input==0) 176 { 177 tmp2=tmp->next; 178 tmp->next=NULL; 179 list=tmp2; 180 } 181 else 182 { 183 tmp2=tmp->next; 184 tmp->next=tmp2->next; 185 tmp2->next=NULL; 186 } 187 tmp=list; 188 return(list); 189 } 190 191 Node *display(Node*list) 192 { 193 int input=0; 194 Node*tmp; 195 tmp=list; 196 while(tmp!=NULL) 197 { 198 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 199 tmp=tmp->next; 200 input=input++; 201 } 202 fprintf(stdout, "NULL\n"); 203 204 return(list); 205 } 206
Thanks to some assistance I have the majority of the stack header files and have listed them below, these will be vital in the operation of this program:
1 #ifndef _LIST_H 2 #define _LIST_H 3 4 #include "node.h" 5 6 struct list { 7 Node *start; 8 Node *end; 9 int qty; 10 }; 11 typedef struct list List; 12 13 List *mklist(); 14 List *insert(List *, Node *, Node *); 15 List *append(List *, Node *, Node *); 16 List *removeNode(List *, Node **); 17 void displayf(List *); 18 void displayb(List *); 19 List *clearlist(List *); 20 List *sortlist(List *); 21 22 #endif
1 #ifndef _NODE_H 2 #define _NODE_H 3 4 #include <stdlib.h> 5 6 struct node { 7 struct node *next; 8 struct node *prev; 9 int value; 10 }; 11 typedef struct node Node; 12 13 Node *mknode(int); 14 void rmnode(Node **); 15 Node *cpnode(Node *); 16 17 #endif
1 #ifndef _STACK_H 2 #define _STACK_H 3 #include "list.h" 4 5 struct stack { 6 List *data; 7 Node *top; 8 int size; 9 }; 10 typedef struct stack Stack; 11 12 Stack *mkstack(int); 13 Stack *push (Stack *, Node *); 14 Node *pop (Stack **); 15 Node *peek (Stack *); 16 17 #endif
After a few days of confusion I managed to get my hands of the completed files, which have been listed below.
1 #include "stack.h" 2 3 Node *peek(Stack *myStack) 4 { 5 return(myStack->top); 6 }
1 #include "stack.h" 2 3 Node *pop(Stack **myStack) 4 { 5 Node *tmp = NULL; 6 7 if ((*myStack) != NULL) 8 { 9 tmp = (*myStack) -> data -> end; 10 (*myStack) -> data = getNode((*myStack) -> data, (&tmp)); 11 (*myStack) -> top = (*myStack) -> data -> end; 12 } 13 14 return (tmp); 15 }
1 #include "stack.h" 2 3 Stack *push(Stack *myStack, Node *newNode) 4 { 5 if ((myStack -> size <= 0) || ((myStack -> data -> qty) < (myStack -> size))) 6 { 7 myStack -> data = append(myStack -> data, myStack -> data -> end, newNode); 8 myStack -> top = myStack -> data -> end; 9 } 10 11 return(myStack); 12 }
1 #include "stack.h" 2 3 Stack *mkstack(int size) 4 { 5 Stack *myStack; 6 7 myStack = (Stack *) malloc (sizeof(Stack)); 8 9 myStack -> data = mklist(); 10 myStack -> size = size; 11 myStack -> top = myStack -> data -> end; 12 13 return (myStack); 14 }
I've been extremely busy with work and other personal matters along with having internet canceled due to parents not paying bills, but regardless I have acquired the queue files and assembled them here;
1 #ifndef _STACK_H 2 #define _STACK_H 3 4 #include "list.h" 5 6 struct stack { 7 List *data; 8 Node *top; 9 int size; 10 }; 11 typedef struct stack Stack; 12 13 Stack *mkstack(int); 14 Stack *push (Stack *, Node *); 15 Node *pop (Stack **); 16 Node *peek (Stack *); 17 18 #endif
1 #ifndef _NODE_H 2 #define _NODE_H 3 4 #include <stdlib.h> 5 6 struct node { 7 struct node *next; 8 struct node *prev; 9 int value; 10 }; 11 typedef struct node Node; 12 13 Node *mknode(int); 14 void rmnode(Node **); 15 Node *cpnode(Node *); 16 17 #endif
1 #ifndef _LIST_H 2 #define _LIST_H 3 4 #include "node.h" 5 6 struct list { 7 Node *start; 8 Node *end; 9 int qty; 10 }; 11 typedef struct list List; 12 13 List *mklist(); 14 List *insert(List *, Node *, Node *); 15 List *append(List *, Node *, Node *); 16 List *removeNode(List *, Node **); 17 void displayf(List *); 18 void displayb(List *); 19 List *clearlist(List *); 20 List *sortlist(List *); 21 22 #endif
1 #ifndef _QUEUE_H 2 #define _QUEUE_H 3 4 #include "stack.h" 5 6 struct queue { 7 List *data; 8 Node *front; 9 Node *back; 10 int bufsize; 11 }; 12 typedef struct queue Queue; 13 14 Queue *mkqueue(int); 15 Queue *enqueue (Queue *, Node *); 16 Node *dequeue (Queue **); 17 18 #endif
1 #include "queue.h" 2 #include "stack.h" 3 #include "list.h" 4 Node *dequeue(Queue **myQueue) 5 { 6 Node *tmp = NULL; 7 8 if ((*myQueue) != NULL) 9 { 10 tmp = (*myQueue) -> data -> start; 11 (*myQueue) -> data = getNode((*myQueue) -> data, (&tmp)); 12 (*myQueue) -> front = (*myQueue) -> data -> start; 13 } 14 15 return (tmp); 16 }
1 #include "queue.h" 2 #include "stack.h" 3 #include "list.h" 4 5 Queue *enqueue(Queue *myQueue, Node *newNode) 6 { 7 if ((myQueue -> bufsize <= 0) || ((myQueue -> data -> qty) < (myQueue -> bufsize))) 8 { 9 myQueue -> data = append(myQueue -> data, myQueue -> data -> end, newNode); 10 myQueue -> back = myQueue -> data -> end; 11 } 12 13 return(myQueue); 14 }
1 #include "queue.h" 2 #include "stack.h" 3 #include "list.h" 4 5 6 Queue *mkqueue(int size) 7 { 8 Queue *myQueue= (Queue *)malloc(sizeof(Queue)); 9 myQueue->data = mklist(size); 10 myQueue->front=myQueue->data->start; 11 myQueue->back=myQueue->data->end; 12 return(myQueue); 13 }
1 #include <stdio.h> 2 #include "queue.h" 3 #include "stack.h" 4 5 int main() 6 { 7 Node *tmp; 8 Queue *myQueue; 9 myQueue = mkqueue(0); 10 tmp = create(1); 11 tmp -> value = fgetc(stdin); 12 fgetc(stdin); 13 myQueue = enqueue(myQueue, tmp); 14 myQueue->data->start = myQueue -> front = tmp; 15 myQueue->data->start->prev = NULL; 16 17 while(tmp->value != '\n') 18 { 19 tmp->next = create(1); 20 tmp->next->value = fgetc(stdin); 21 tmp->next->prev = tmp; 22 fgetc(stdin); 23 tmp=tmp->next; 24 myQueue = enqueue(myQueue, tmp); 25 myQueue->data->end=myQueue->back = tmp; 26 } 27 28 fprintf(stdout, "String is: "); 29 while(tmp != NULL) 30 { 31 tmp = dequeue(&myQueue); 32 fprintf(stdout, "%c", tmp->value); 33 rmnode((&tmp)); 34 if(myQueue->back == myQueue->front) 35 { 36 tmp = NULL; 37 } 38 } 39 fprintf(stdout, "\n"); 40 return(0); 41 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct node Node; 5 6 struct node{ 7 int value; 8 int place; 9 struct node *next; 10 struct node * *tmp3; 11 }; 12 13 Node *build(Node *start); 14 Node *insert(Node*start, Node *given, Node *newNode); 15 Node *append(Node *start, Node *given, Node *newNode); 16 //Node *getNode(Node *start, Node **tmp3); 17 Node *display(Node *start); 18 19 int main() 20 { 21 Node *start=NULL; 22 int option; 23 24 while(option!=-1) 25 { 26 fprintf(stdout,"|===========Menu==========|\n"); 27 fprintf(stdout,"| 1.)Build List |\n"); 28 fprintf(stdout,"| 2.)Insert to List |\n"); 29 fprintf(stdout,"| 3.)Append to List |\n"); 30 fprintf(stdout,"| 4.)Remove from List |\n"); 31 fprintf(stdout,"| 5.)Display the List |\n"); 32 fprintf(stdout,"| -1.)Quit |\n"); 33 fprintf(stdout,"|=========================|\n"); 34 fprintf(stdout, "Please select an option: "); 35 fscanf(stdin, "%d", &option); 36 37 if(option==1) 38 { 39 start=build(start); 40 start=display(start); 41 } 42 else if(option==2) 43 { 44 start=display(start); 45 Node *tmp, *tmp2=NULL; 46 int input=5; 47 fprintf(stdout,"Which node would you like to insert before?: "); 48 fscanf(stdin, "%d", &input); 49 int seeker; 50 tmp=start; 51 for(seeker=0; seeker<(input-1); seeker++) 52 { 53 tmp=tmp->next; 54 } 55 if(input==0) 56 { 57 fprintf(stdout,"Enter the value of the new node: "); 58 fscanf(stdin, "%d", &input); 59 tmp2=(Node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp->place=0; 62 } 63 else 64 { 65 fprintf(stdout, "Enter a value to insert: "); 66 fscanf(stdin, "%d", &input); 67 tmp2=(Node*)malloc(sizeof(Node)); 68 tmp2->value=input; 69 tmp->place=1; 70 } 71 start=insert(start, tmp, tmp2); 72 start=display(start); 73 } 74 else if(option==3) 75 { 76 start=display(start); 77 //assinging of values must occur before function execution 78 Node *tmp, *tmp4=NULL; 79 int input, choice; 80 int behind; 81 tmp=start; 82 fprintf(stdout,"Enter what node would you like to append after: "); 83 fscanf(stdin, "%d", &choice); 84 for(behind=0; behind<choice; behind++) 85 { 86 tmp=tmp->next; 87 } 88 //fprintf(stdout,"%d", behind); output tests 89 //start=display(tmp); output tests 90 tmp4=(Node*)malloc(sizeof(Node)); 91 tmp4->place=choice; 92 fprintf(stdout,"Enter value for node to be appended: "); 93 fscanf(stdin, "%d", &input); 94 tmp4->value=input; 95 96 start=display(start); 97 start=append(start, tmp, tmp4); 98 start=display(start); //display acts as test to prove it worked 99 } 100 else if(option==4) 101 { 102 //assinging before functions required 103 /* 104 start=display(start); 105 Node *tmp; 106 tmp=start; 107 Node **tmp3; 108 tmp3=&tmp; 109 int begone, input; 110 fprintf(stdout, "Which node would you like to remove?: "); 111 fscanf(stdin, "%d" , &input); 112 for(begone=0; begone<(input-1);begone++) 113 { 114 tmp=tmp->next; 115 } 116 if(input==0) 117 { 118 start->place=0; 119 } 120 else 121 { 122 start->place=1; 123 } 124 125 start=getNode(start, &tmp); 126 start=display(start); //display aids in proving succes of function 127 */ } 128 else if(option==5) 129 { 130 start=display(start); 131 } 132 else if (option=-1) 133 { 134 fprintf(stdout, "Goodbye\n"); 135 } 136 else 137 { 138 fprintf(stdout, "ERROR, INVALID SELECTION\n"); 139 } 140 } 141 return(0); 142 } 143 144 145 146 Node *build(Node *start) 147 { 148 int input=0; 149 Node *tmp; 150 start=tmp=NULL; 151 152 while(input!=-1) 153 { 154 fprintf(stdout, "Enter a value(-1 to end): "); 155 fscanf(stdin, "%d", &input); 156 157 if(input!=-1) 158 { 159 if(start==NULL) 160 { 161 start=tmp=(Node*)malloc(sizeof(Node)); 162 tmp->next=NULL; 163 start->value=input; 164 } 165 else 166 { 167 tmp->next=(Node*)malloc(sizeof(Node)); 168 tmp->next->next=NULL; 169 tmp->next->value=input; 170 tmp=tmp->next; 171 } 172 } 173 } 174 tmp=start; 175 176 return(start); 177 } 178 179 Node *insert(Node *start, Node *tmp, Node *tmp2) 180 { 181 int placing; 182 placing=tmp->place; 183 184 if(placing==0) 185 { 186 tmp2->next=NULL; 187 tmp2->next=tmp; 188 start=tmp2; 189 } 190 else 191 { 192 tmp2->next=NULL; 193 tmp2->next=tmp->next; 194 tmp->next=tmp2; 195 } 196 197 tmp=start; 198 return(start); 199 } 200 201 Node *append(Node *start, Node *tmp, Node *tmp4) 202 { 203 tmp4->next=tmp->next; 204 tmp->next=tmp4; 205 tmp=start; 206 207 return(start); 208 } 209 210 /* 211 Node *getNode(Node *start, Node **tmp3) 212 { 213 Node *tmp2=NULL; 214 Node *tmp; 215 int placing; 216 placing=start->place; 217 tmp=start; 218 219 if(start->place==0) 220 { 221 tmp2=**tmp3; 222 tmp->next=NULL; 223 start=tmp2; 224 } 225 226 else 227 { 228 tmp2=**tmp3; 229 tmp->next=tmp2->next; 230 tmp2->next=NULL; 231 } 232 233 tmp=start; 234 return(start); 235 } 236 */ 237 238 Node *display(Node *start) 239 { 240 int input=0; 241 Node *tmp; 242 tmp=start; 243 244 while(tmp!=NULL) 245 { 246 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 247 tmp=tmp->next; 248 input=input++; 249 } 250 fprintf(stdout, "NULL\n"); 251 252 return(start); 253 } 254
Above is my SLL project code, now perfect as the getNode function still has so double pointer recognition issues, will look online for material to reference.
After some outside help, I have fixed my SLL program to become SLL2, the getNode isn't perfect yet but I have gotten it to compile so after some pointer analyzing, everything should be working nicely.
SLL2.0
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct node Node; 5 6 struct node{ 7 int value; 8 int place; 9 struct node *next; 10 11 }; 12 13 Node *build(Node *start); 14 Node *insert(Node*start, Node *given, Node *newNode); 15 Node *append(Node *start, Node *given, Node *newNode); 16 //Node *getNode(Node *start, Node **tmp3); 17 Node *display(Node *start); 18 19 int main() 20 { 21 Node *start=NULL; 22 int option; 23 24 while(option!=-1) 25 { 26 fprintf(stdout,"|===========Menu==========|\n"); 27 fprintf(stdout,"| 1.)Build List |\n"); 28 fprintf(stdout,"| 2.)Insert to List |\n"); 29 fprintf(stdout,"| 3.)Append to List |\n"); 30 fprintf(stdout,"| 4.)Remove from List |\n"); 31 fprintf(stdout,"| 5.)Display the List |\n"); 32 fprintf(stdout,"| -1.)Quit |\n"); 33 fprintf(stdout,"|=========================|\n"); 34 fprintf(stdout, "Please select an option: "); 35 fscanf(stdin, "%d", &option); 36 37 if(option==1) 38 { 39 start=build(start); 40 start=display(start); 41 } 42 else if(option==2) 43 { 44 start=display(start); 45 Node *tmp, *tmp2=NULL; 46 int input=5; 47 fprintf(stdout,"Which node would you like to insert before?: "); 48 fscanf(stdin, "%d", &input); 49 int seeker; 50 tmp=start; 51 for(seeker=0; seeker<(input-1); seeker++) 52 { 53 tmp=tmp->next; 54 } 55 if(input==0) 56 { 57 fprintf(stdout,"Enter the value of the new node: "); 58 fscanf(stdin, "%d", &input); 59 tmp2=(Node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp->place=0; 62 } 63 else 64 { 65 fprintf(stdout, "Enter a value to insert: "); 66 fscanf(stdin, "%d", &input); 67 tmp2=(Node*)malloc(sizeof(Node)); 68 tmp2->value=input; 69 tmp->place=1; 70 } 71 start=insert(start, tmp, tmp2); 72 start=display(start); 73 } 74 else if(option==3) 75 { 76 start=display(start); 77 //Assinging of values must occur before function execution 78 Node *tmp, *tmp4=NULL; 79 int input, choice; 80 int behind; 81 tmp=start; 82 fprintf(stdout,"Enter what node would you like to append after: "); 83 fscanf(stdin, "%d", &choice); 84 for(behind=0; behind<choice; behind++) 85 { 86 tmp=tmp->next; 87 } 88 //fprintf(stdout,"%d", behind); output tests 89 //start=display(tmp); output tests 90 tmp4=(Node*)malloc(sizeof(Node)); 91 tmp4->place=choice; 92 fprintf(stdout,"Enter value for node to be appended: "); 93 fscanf(stdin, "%d", &input); 94 tmp4->value=input; 95 96 start=display(start); 97 start=append(start, tmp, tmp4); 98 start=display(start); //display acts as test to prove it worked 99 } 100 else if(option==4) 101 { 102 //assinging before functions required 103 104 start=display(start); 105 Node *tmp; 106 tmp=start; 107 Node **tmp3; 108 tmp3=&tmp; 109 int begone, input; 110 fprintf(stdout, "Which node would you like to remove?: "); 111 fscanf(stdin, "%d" , &input); 112 for(begone=0; begone<(input-1);begone++) 113 { 114 tmp=tmp->next; 115 } 116 if(input==0) 117 { 118 start->place=0; 119 } 120 else 121 { 122 start->place=1; 123 } 124 125 start=getNode(start, (*tmp3)); 126 start=display(start); //display aids in proving succes of function 127 } 128 else if(option==5) 129 { 130 start=display(start); 131 } 132 else if (option=-1) 133 { 134 fprintf(stdout, "Goodbye\n"); 135 } 136 else 137 { 138 fprintf(stdout, "ERROR, INVALID SELECTION\n"); 139 } 140 } 141 return(0); 142 } 143 144 145 146 Node *build(Node *start) 147 { 148 int input=0; 149 Node *tmp; 150 start=tmp=NULL; 151 152 while(input!=-1) 153 { 154 fprintf(stdout, "Enter a value(-1 to end): "); 155 fscanf(stdin, "%d", &input); 156 157 if(input!=-1) 158 { 159 if(start==NULL) 160 { 161 start=tmp=(Node*)malloc(sizeof(Node)); 162 tmp->next=NULL; 163 start->value=input; 164 } 165 else 166 { 167 tmp->next=(Node*)malloc(sizeof(Node)); 168 tmp->next->next=NULL; 169 tmp->next->value=input; 170 tmp=tmp->next; 171 } 172 } 173 } 174 tmp=start; 175 176 return(start); 177 } 178 179 Node *insert(Node *start, Node *tmp, Node *tmp2) 180 { 181 int placing; 182 placing=tmp->place; 183 184 if(placing==0) 185 { 186 tmp2->next=NULL; 187 tmp2->next=tmp; 188 start=tmp2; 189 } 190 else 191 { 192 tmp2->next=NULL; 193 tmp2->next=tmp->next; 194 tmp->next=tmp2; 195 } 196 197 tmp=start; 198 return(start); 199 } 200 201 Node *append(Node *start, Node *tmp, Node *tmp4) 202 { 203 tmp4->next=tmp->next; 204 tmp->next=tmp4; 205 tmp=start; 206 207 return(start); 208 } 209 210 /* 211 Node *getNode(Node *start, Node (*tmp3)) 212 { 213 Node *tmp2=NULL; 214 Node *tmp; 215 int placing; 216 placing=start->place; 217 tmp=start; 218 219 if(start==tmp3) 220 { 221 tmp2=tmp3; 222 tmp->next=NULL; 223 start=tmp2; 224 } 225 226 else 227 { 228 tmp2=tmp3; 229 tmp->next=tmp2->next; 230 tmp2->next=NULL; 231 } 232 233 tmp=start; 234 return(start); 235 } 236 */ 237 238 Node *display(Node *start) 239 { 240 int input=0; 241 Node *tmp; 242 tmp=start; 243 244 while(tmp!=NULL) 245 { 246 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 247 tmp=tmp->next; 248 input=input++; 249 } 250 fprintf(stdout, "NULL\n"); 251 252 return(start); 253 } 254
The output of which, focused on the malfunctioning getNode function called 'remove' in menu listing:
lab46:~/src/DATA/spring2014$ ./SLL2 |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 1 Enter a value(-1 to end): 1 Enter a value(-1 to end): 2 Enter a value(-1 to end): 3 Enter a value(-1 to end): 4 Enter a value(-1 to end): 5 Enter a value(-1 to end): 6 Enter a value(-1 to end): 7 Enter a value(-1 to end): 8 Enter a value(-1 to end): 9 Enter a value(-1 to end): -1 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 4 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL Which node would you like to remove?: 4 [0] 1 -> [1] 5 -> [2] 6 -> [3] 7 -> [4] 8 -> [5] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 4 [0] 1 -> [1] 5 -> [2] 6 -> [3] 7 -> [4] 8 -> [5] 9 -> NULL Which node would you like to remove?: 5 [0] 1 -> [1] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: -1 Goodbye
It seems that the input node to be removed becomes the start→next due to an error in my code, I will be looking on how to fix it.
I Finally got the damn thing working, so far behind schedule it's extremely upsetting but working all the same, the double pointers had me confused until the always benevolent teacher aided me with some advice.
1 /* 2 Author:Kellen Hoose 3 Created:February 2014 4 Purpose:For the SLL_moar project for class, menu driven program with seperate functions call ings and definitions, build, insert, append, remove, display, and quit as options. 5 */ 6 #include <stdio.h> 7 #include <stdlib.h> 8 9 typedef struct node Node; 10 11 struct node{ 12 int value; 13 int place; 14 struct node *next; 15 }; 16 17 Node *build(Node *start); 18 Node *insert(Node*start, Node *given, Node *newNode); 19 Node *append(Node *start, Node *given, Node *newNode); 20 Node *getNode(Node *start, Node **tmp3); 21 Node *display(Node *start); 22 23 int main() 24 { 25 Node *start=NULL; 26 int option; 27 28 while(option!=-1) 29 { 30 fprintf(stdout,"|===========Menu==========|\n"); 31 fprintf(stdout,"| 1.)Build List |\n"); 32 fprintf(stdout,"| 2.)Insert to List |\n"); 33 fprintf(stdout,"| 3.)Append to List |\n"); 34 fprintf(stdout,"| 4.)Remove from List |\n"); 35 fprintf(stdout,"| 5.)Display the List |\n"); 36 fprintf(stdout,"| -1.)Quit |\n"); 37 fprintf(stdout,"|=========================|\n"); 38 fprintf(stdout, "Please select an option: "); 39 fscanf(stdin, "%d", &option); 40 41 if(option==1) 42 { 43 start=build(start); 44 start=display(start); 45 } 46 else if(option==2) 47 { 48 start=display(start); 49 Node *tmp, *tmp2=NULL; 50 int input=5; 51 fprintf(stdout,"Which node would you like to insert before?: "); 52 fscanf(stdin, "%d", &input); 53 int seeker; 54 tmp=start; 55 for(seeker=0; seeker<(input-1); seeker++) 56 { 57 tmp=tmp->next; 58 } 59 if(input==0) 60 { 61 fprintf(stdout,"Enter the value of the new node: "); 62 fscanf(stdin, "%d", &input); 63 tmp2=(Node*)malloc(sizeof(Node)); 64 tmp2->value=input; 65 tmp->place=0; 66 } 67 else 68 { 69 fprintf(stdout, "Enter a value to insert: "); 70 fscanf(stdin, "%d", &input); 71 tmp2=(Node*)malloc(sizeof(Node)); 72 tmp2->value=input; 73 tmp->place=1; 74 } 75 start=insert(start, tmp, tmp2); 76 start=display(start); 77 } 78 else if(option==3) 79 { 80 start=display(start); 81 //assinging of values must occur before function execution 82 Node *tmp, *tmp4=NULL; 83 int input, choice; 84 int behind; 85 tmp=start; 86 fprintf(stdout,"Enter what node would you like to append after: "); 87 fscanf(stdin, "%d", &choice); 88 for(behind=0; behind<choice; behind++) 89 { 90 tmp=tmp->next; 91 } 92 //fprintf(stdout,"%d", behind); output tests 93 //start=display(tmp); output tests 94 tmp4=(Node*)malloc(sizeof(Node)); 95 tmp4->place=choice; 96 fprintf(stdout,"Enter value for node to be appended: "); 97 fscanf(stdin, "%d", &input); 98 tmp4->value=input; 99 100 start=display(start); 101 start=append(start, tmp, tmp4); 102 start=display(start); //display acts as test to prove it worked 103 } 104 else if(option==4) 105 { 106 //assinging before functions required 107 108 start=display(start); 109 Node *tmp; 110 tmp=start; 111 Node **tmp3; 112 tmp3=&tmp; 113 int begone, input; 114 fprintf(stdout, "Which node would you like to remove?: "); 115 fscanf(stdin, "%d" , &input); 116 for(begone=0; begone<(input-1);begone++) 117 { 118 tmp=tmp->next; 119 } 120 121 // start=display(tmp); //maybe tmp isn't being assigned right?, sets tmp to one before node to be removed. 122 start=getNode(start, &tmp); 123 start=display(start); //display aids in proving succes of function 124 } 125 else if(option==5) 126 { 127 start=display(start); 128 } 129 else if (option=-1) 130 { 131 fprintf(stdout, "Goodbye\n"); 132 } 133 else 134 { 135 fprintf(stdout, "ERROR, INVALID SELECTION\n"); 136 } 137 } 138 return(0); 139 } 140 141 142 143 Node *build(Node *start) 144 { 145 int input=0; 146 Node *tmp; 147 start=tmp=NULL; 148 149 while(input!=-1) 150 { 151 fprintf(stdout, "Enter a value(-1 to end): "); 152 fscanf(stdin, "%d", &input); 153 154 if(input!=-1) 155 { 156 if(start==NULL) 157 { 158 start=tmp=(Node*)malloc(sizeof(Node)); //creates node, allocate memory to si ze of Node struct, thus saving space. 159 tmp->next=NULL; 160 start->value=input; 161 } 162 else 163 { 164 tmp->next=(Node*)malloc(sizeof(Node)); 165 tmp->next->next=NULL; 166 tmp->next->value=input; 167 tmp=tmp->next; 168 } 169 } 170 } 171 tmp=start; 172 173 return(start); 174 } 175 176 Node *insert(Node *start, Node *tmp, Node *tmp2) 177 { 178 int placing; 179 placing=tmp->place; 180 181 if(placing==0) 182 { 183 tmp2->next=NULL; 184 tmp2->next=tmp; 185 start=tmp2; 186 } 187 else 188 { 189 tmp2->next=NULL; 190 tmp2->next=tmp->next; 191 tmp->next=tmp2; 192 } 193 194 tmp=start; 195 return(start); 196 } 197 198 Node *append(Node *start, Node *tmp, Node *tmp4) 199 { 200 tmp4->next=tmp->next; 201 tmp->next=tmp4; 202 tmp=start; 203 204 return(start); 205 } 206 207 208 Node *getNode(Node *start, Node **tmp3) 209 { 210 Node *tmp2=NULL; 211 Node *tmp4; 212 tmp4=*tmp3;//*tmp3=&tmp (address of tmp, which is 1 before node to be removed 213 tmp2=*tmp3; 214 215 if(start==*tmp3) 216 { 217 tmp4=tmp2->next; //if removing start, set start as 2nd from start, cut that thang of f 218 tmp2->next=NULL; 219 start=tmp4; 220 } 221 222 else 223 { 224 tmp4=tmp2->next; 225 tmp2->next=tmp4->next; // removing other than start, skip around node, cut it off 226 tmp4->next=NULL; 227 } 228 229 tmp4=start; 230 return(start); 231 } 232 233 234 Node *display(Node *start) 235 { 236 int input=0; 237 Node *tmp; 238 tmp=start; 239 240 while(tmp!=NULL) 241 { 242 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 243 tmp=tmp->next; 244 input=input++; 245 } 246 fprintf(stdout, "NULL\n"); 247 248 return(start); 249 }
This program results in the following operation/output:
lab46:~/src/DATA/spring2014$ ./SLL3 |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 1 Enter a value(-1 to end): 1 Enter a value(-1 to end): 2 Enter a value(-1 to end): 3 Enter a value(-1 to end): 4 Enter a value(-1 to end): 5 Enter a value(-1 to end): 6 Enter a value(-1 to end): 7 Enter a value(-1 to end): 8 Enter a value(-1 to end): 9 Enter a value(-1 to end): -1 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 2 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> NULL Which node would you like to insert before?: 4 Enter a value to insert: 12 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 3 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> NULL Enter what node would you like to append after: 9 Enter value for node to be appended: 10 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> NULL [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> [10] 10 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 4 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 12 -> [5] 5 -> [6] 6 -> [7] 7 -> [8] 8 -> [9] 9 -> [10] 10 -> NULL Which node would you like to remove?: 4 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> [9] 10 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: 5 [0] 1 -> [1] 2 -> [2] 3 -> [3] 4 -> [4] 5 -> [5] 6 -> [6] 7 -> [7] 8 -> [8] 9 -> [9] 10 -> NULL |===========Menu==========| | 1.)Build List | | 2.)Insert to List | | 3.)Append to List | | 4.)Remove from List | | 5.)Display the List | | -1.)Quit | |=========================| Please select an option: -1 Goodbye lab46:~/src/DATA/spring2014$