Kellen Hoose's spring2014 Opus
Here's to passing!
I'm Kellen Hoose and my pursuits are to pass this class and get my degree this spring. I was too busy and unfocused last semester to sufficiently complete this course and while I'm even more busy this semester I wish to do my best and finish what I couldn't before.
This first week was spent, reviewing old concepts I had lost touch with over break.
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
1 #include<stdio.h> 2 #include<stdlib.h> 3 /* 4 Author:Kellen Hoose 5 Date:1/22/2014 6 Purpose:This program displays the variable size ranges for signed char, 7 unsigned char, signed short, unsigned short, signed int, unsigned int, 8 signed long, unsigned long, signed long long, unsigned long long and float varia bles. 9 */ 10 int main() 11 { 12 signed char sc=0; 13 unsigned char uc=0; 14 signed short ssi=0; 15 unsigned short usi=0; 16 signed int si=0; 17 unsigned int ui=0; 18 signed long sl=0; 19 unsigned long ul=0; 20 signed long long sll=0; 21 unsigned long long ull=0; 22 float f=0; 23 24 25 printf(" "); 26 printf("a signed char is %hhd bytes\n", sizeof(sc)); 27 printf("lower bound is: %hhd\n", ((unsigned char)(sc-1)/2)+1); 28 printf("Upper bound is: %hhd\n", ((unsigned char)(sc-1)/2)); 29 printf("--------\n"); 30 printf("an unsigned char is %hhu bytes \n", sizeof(uc)); 31 printf("lower bound is: %hhu\n", uc); 32 printf("Upper bound is: %hhu\n", uc-1); 33 printf("--------\n"); 34 printf("a signed short int is %hd bytes\n", sizeof(ssi)); 35 printf("lower bound is: %hd\n", ((unsigned short)(ssi-1)/2)+1); 36 printf("Upper bound is: %hd\n", ((unsigned short)(ssi-1)/2)); 37 printf("--------\n"); 38 printf("an unsigned short int is %hu bytes \n", sizeof(usi)); 39 printf("lower bound is: %hu\n", usi); 40 printf("Upper bound is: %hu\n", usi-1); 41 printf("--------\n"); 42 printf("a signed int is %d bytes\n", sizeof(si)); 43 printf("lower bound is: %d\n", ((unsigned int)(si-1)/2)+1); 44 printf("Upper bound is: %d\n", ((unsigned int)(si-1)/2)); 45 printf("--------\n"); 46 printf("an unsigned int is %u bytes \n", sizeof(ui)); 47 printf("lower bound is: %u\n", ui); 48 printf("Upper bound is: %u\n", ui-1); 49 printf("--------\n"); 50 printf("a signed long int is %ld bytes\n", sizeof(sl)); 51 printf("lower bound is: %ld\n", ((unsigned long int)(sl-1)/2)+1); 52 printf("Upper bound is: %ld\n", ((unsigned long int)(sl-1)/2)); 53 printf("--------\n"); 54 printf("an unsigned long int is %lu bytes \n", sizeof(ul)); 55 printf("lower bound is: %lu\n", ul); 56 printf("Upper bound is: %lu\n", ul-1); 57 printf("--------\n"); 58 printf("a signed float is %f bytes\n", sizeof(f)); 59 printf("lower bound is: %f\n", (( float)(f-1)/2)+1); 60 printf("Upper bound is: %f\n", ((float)(f-1)/2)); 61 printf("--------\n"); 62 printf("an unsigned float is %f bytes \n", sizeof(f)); 63 printf("lower bound is: %f\n", f); 64 printf("Upper bound is: %f\n", f-1); 65 printf("--------\n"); 66 67 return(0); 68 }
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 --------
This week we covered the use of nodes, generating them, assigning values, along with creating tmp files to allow the continuation of the list. The list is at first empty, then we create a node and assign it a value, then we continue until we enter -1, the list is then read back to the user. The program is as shown below.
1 #include<stdio.h> 2 #include<stdlib.h> 3 /* 4 Author:Kellen D Hoose 5 Filename: node.c 6 Date:2.09.2014 7 Purpose:Allow the creation of a list of numbers, then read the list back to user 8 */ 9 struct node { 10 signed char value; //by setting the variable as a signed char 11 struct node *next; // we limit ourselves to 256 characters, from -127 to 128 12 }; 13 14 typedef struct node Node; 15 16 int main() 17 { 18 int input; 19 Node *list, *tmp; 20 21 list = tmp = NULL; 22 printf("\n\nWARNING: numbers above 127 or below -128 will be recorded incorrectly\n\n"); // it's best if the users are warned of number limits 23 while (input != -1) 24 { 25 printf("Enter a value(-1 to quit): "); 26 scanf("%d", &input); 27 if (input != -1) 28 { 29 if (list == NULL) //this says "if list doesn't exist, make node for list and 30 //input value 31 { 32 list = tmp = (Node *) malloc(sizeof(Node)); 33 tmp->next = NULL; 34 list->value = input; 35 } 36 else //after there is SOMETHING in list, add onto list using this section 37 { 38 tmp->next = (Node *) malloc(sizeof(Node)); 39 tmp->next->next = NULL; 40 tmp->next->value = input; 41 tmp = tmp->next; 42 } 43 } 44 } 45 tmp = list; 46 printf("\n\nList: "); 47 while (tmp != NULL) //goes through a loop, until tmp=NULL, at the end of input values. 48 { 49 printf("%d ->", tmp->value); 50 tmp = tmp->next; 51 } 52 printf("NULL\n"); 53 return (0); 54 }
To compile this program we input:
lab46:~/src/DATA/spring2014$ gcc -o node node.c
The execution of the compiled program results in:
lab46:~/src/DATA/spring2014$ ./node WARNING: numbers above 127 or below -128 will be recorded incorrectly Enter a value(-1 to quit): 1 Enter a value(-1 to quit): 2 Enter a value(-1 to quit): 3 Enter a value(-1 to quit): 4 Enter a value(-1 to quit): -1 List: 1 ->2 ->3 ->4 ->NULL lab46:~/src/DATA/spring2014$
The following code needs tweaking before working properly for the SLL project code.
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 10 typedef struct node Node; 11 12 struct list { 13 struct node *start; 14 struct node *end; 15 }; 16 17 typedef struct list List; 18 19 List *build(); 20 void displayf(List*); 21 void displayb(List*); 22 List *insert(List *myList, Node *place, Node *newNode); 23 List *getNode(List *myList, Node **place); 24 List *append(List *myList, Node *place, Node *newNode); 25 List *sort(List *myList); 26 List *clear(List *myList); 27 28 int main() 29 { 30 List *myList; 31 int option=0; 32 Node *tmp=NULL; 33 while(option!=-1) 34 35 { 36 fprintf(stdout, "|=============Menu=============|\n"); 37 fprintf(stdout, "| 1.)Build list |\n"); 38 fprintf(stdout, "| 2.)Insert to list |\n"); 39 fprintf(stdout, "| 3.)Append to list |\n"); 40 fprintf(stdout, "| 4.)Remove from list |\n"); 41 fprintf(stdout, "| 5.)Display the list forward |\n"); 42 fprintf(stdout, "| 6.)Display the list backwards|\n"); 43 fprintf(stdout, "|-1.)Quit |\n"); 44 fprintf(stdout, "|==============================|\n"); 45 fprintf(stdout, "Please select an option: "); 46 fscanf(stdin, "%d", &option); 47 48 if(option==1) 49 { 50 myList=build(); 51 } 52 else if(option==2) 53 { 54 55 tmp=myList->start; 56 fprintf(stdout, "Which node would you like to insert to? "); 57 fscanf(stdin, "%d", &choice); 58 59 // seeker loop 60 for(seeker=0; seeker<(choice-1); seeker++) 61 { 62 tmp=tmp->next; 63 } 64 65 // make new node (tmp2) 66 // put choice in new node's value 67 Node * tmp2 = (Node *)malloc(sizeof(Node)); 68 69 fprintf(stdout, "What value would you like to insert: "); 70 fscanf(stdin, "%d", &choice); 71 72 tmp2->value=choice; 73 74 myList = insert(myList, tmp, tmp2); 75 } 76 } 77 else if(option==4) 78 { 79 list=removenode(list); 80 } 81 else if(option==5) 82 { 83 displayf(myList); 84 } 85 else if(option==6) 86 { 87 displayb(myList); 88 } 89 else if(option=-1) 90 { 91 fprintf(stdout, "Goodbye\n"); 92 } 93 else 94 { 95 fprintf(stdout, "Error, invalid input\n"); 96 } 97 } 98 99 return(0); 100 101 } 102 103 List *build() 104 { 105 Node *tmp=NULL; 106 List *myList=(List*)malloc(sizeof(List)); 107 myList->start=myList->end=NULL; 108 int input = 0; 109 fprintf(stdout, "Enter a value (-1 to quit): "); 110 fscanf(stdin, "%d", &input); 111 112 while(input !=-1) 113 { 114 if(myList->start==NULL) 115 { 116 myList->start=myList->end=(Node*)malloc(sizeof(Node)); 117 myList->start->value=input; 118 myList->end->prev=myList->start->next=NULL; 119 } 120 else 121 { 122 myList->end->next=(Node*)malloc(sizeof(Node)); 123 myList->end->next->value=input; 124 myList->end->next->next=NULL; 125 myList->end->next->prev=myList->end; 126 myList->end=mylist->end->next; 127 } 128 fprintf(stdout, "Enter another value(-1 to quit): "); 129 fscanf(stdin, "%d", &input); 130 } 131 132 return(myList); 133 } 134 135 void displayf(List *myList); 136 { 137 Node *tmp=myList->start; 138 int input=0; 139 140 while(tmp !=NULL) 141 { 142 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 143 tmp=tmp->next; 144 input=input++; 145 } 146 fprintf(stdout, "NULL\n";) 147 } 148 149 void displayb(List *myList); 150 { 151 Node *tmp=myList->end; 152 int input=0; 153 154 while(tmp !=NULL) 155 { 156 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 157 tmp=tmp->prev; 158 input=input++; 159 } 160 fprintf(stdout, "NULL\n"); 161 } 162 163 List *insert(List *myList, Node *place, Node *newNode); 164 { 165 if(place==myList->start) 166 { 167 newNode->next=place; 168 place->prev=newNode; 169 newNode->prev=NULL; 170 myList->start=newNode; 171 } 172 else 173 { 174 newNode->next=place; 175 place->prev->next=newNode; 176 newNode->prev=place->prev; 177 place->prev=newNode; 178 } 179 180 return(myList); 181 } 182 183 List *getNode(List *myList, Node **place); 184 { 185 if(myList->start->next==NULL) 186 { 187 myList->start=NULL; 188 } 189 190 else if(*place==myList->start) 191 { 192 myList->start=myList->start->next; 193 myList->start->prev=NULL; 194 (*place)->next=NULL; 195 } 196 else if(*place==myList->end) 197 { 198 myList->end=myList->end->prev; 199 myList->end->next=NULL; 200 (*place)->prev=NULL; 201 } 202 else 203 { 204 (*place)->prev->next=(*place)->next; 205 (*place)->next->prev=(*place)->prev; 206 (*place)->prev=(*place)->next=NULL; 207 } 208 209 return(myList); 210 } 211 212 List *append(List *myList, Node *place, Node *newNode); 213 { 214 if(place==myList->end) 215 { 216 newNode->prev=place; 217 place->next=newNode; 218 newNode->next=NULL; 219 myList->end=newNode; 220 } 221 else 222 { 223 newNode->prev=place; 224 place->next->prev=newNode; 225 newNode->next=place->next; 226 place->next=newNode; 227 } 228 229 return(myList); 230 } 231 232 List *sort(List *myList); 233 { 234 Node *tmp; 235 Node *tmp2; 236 tmp = myList->start; 237 Node *highest; 238 int behind; 239 int count = 0; 240 while(tmp != NULL) 241 { 242 tmp = tmp->next; 243 count++; 244 } 245 int first = count; 246 for(; first>1; first--) 247 { 248 tmp = myList->start; 249 tmp2 = myList->start; 250 highest = tmp; 251 for(; count>0; count--) 252 { 253 if(highest->value < tmp2->value) 254 { 255 highest = tmp2; 256 } 257 tmp2 = tmp2->next; 258 } 259 myList = getNode(myList, &highest); 260 261 tmp = myList->start; 262 for(behind = 0; behind<(first-2); behind++) 263 { 264 tmp = tmp->next; 265 } 266 myList = append(myList, tmp, highest); 267 } 268 return(myList); 269 } 270 271 List *clear(List *myList); 272 { 273 Node *tmp = myList->start; 274 Node *tmp2; 275 int count; 276 277 while(tmp != NULL) 278 { 279 tmp = tmp->next; 280 } 281 282 tmp = myList->start; 283 284 for(; count>0; count--) 285 { 286 if(tmp == myList->end) 287 { 288 myList->end = NULL; 289 myList->start = NULL; 290 free(tmp); 291 } 292 else 293 { 294 tmp2 = tmp->next; 295 tmp->next = NULL; 296 tmp->prev = NULL; 297 tmp2->prev = NULL; 298 myList->start = tmp2; 299 free(tmp); 300 tmp = tmp2; 301 } 302 } 303 myList->qty = 0; 304 305 return(myList); 306 }
1 /*uthor: Kellen Hoose 2 Date: 9/26/2013 3 Purpose: Prototype solution for Singly Linked List project. 4 Capable of making list, insert, append, and removing. 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 struct node{ 11 int value; 12 struct node*next; 13 }; 14 15 typedef struct node Node; 16 17 int main() 18 { 19 int input=0; 20 Node*list, *tmp; 21 list=tmp=NULL; 22 while(input!=-1) 23 { 24 fprintf(stdout, "Enter a value (-1 to end): "); 25 fscanf(stdin, "%d", &input); 26 27 if(input!=-1) 28 { 29 if(list==NULL) 30 { 31 list=tmp=(Node*)malloc(sizeof(Node)); 32 tmp->next=NULL; 33 list->value=input; 34 } 35 else 36 { 37 tmp->next=(Node*)malloc(sizeof(Node)); 38 tmp->next->next=NULL; 39 tmp->next->value=input; 40 tmp=tmp->next; 41 } 42 } 43 } 44 tmp=list; 45 input=0; 46 while(tmp!=NULL) 47 { 48 fprintf(stdout, "[%d] %d ->", input, tmp->value); 49 tmp=tmp->next; 50 input=input++; 51 } 52 fprintf(stdout, "NULL\n"); 53 fprintf(stdout, "Which node would you like to insert before? "); 54 fscanf(stdin, "%d", &input); 55 int seeker; 56 57 tmp=list; 58 Node *tmp2=NULL; 59 60 for(seeker=0; seeker<(input-1); seeker++) 61 { 62 tmp=tmp->next; 63 } 64 if (input == 0) 65 { 66 printf("Enter value for new node: "); 67 scanf("%d", &input); 68 tmp2 = (Node *) malloc(sizeof(Node)); 69 tmp2->value = input; 70 tmp2->next = NULL; 71 tmp2->next = tmp; 72 list =tmp2; 73 input = 0; 74 tmp=list; 75 while (tmp != NULL) 76 { 77 printf("[%d] %d ->", input, tmp->value); 78 tmp = tmp->next; 79 input = input++; 80 } 81 printf("NULL\n"); 82 } 83 else 84 { 85 printf("Enter a value to insert: "); 86 scanf("%d", &input); 87 tmp2 = (Node *) malloc(sizeof(Node)); 88 tmp2->value = input; 89 tmp2->next = tmp->next; 90 tmp->next = tmp2; 91 input = 0; 92 tmp=list; 93 while (tmp != NULL) 94 { 95 printf("[%d] %d ->", input, tmp->value); 96 tmp= tmp->next; 97 input = input + 1; 98 } 99 printf("NULL\n"); 100 } 101 102 fprintf(stdout, "Enter what node you would like to append to: "); 103 fscanf(stdin, "%d", &input); 104 int behind; 105 tmp=list; 106 Node*tmp3=NULL; 107 for(behind=0; behind<input; behind++) 108 { 109 tmp=tmp->next; 110 } 111 112 fprintf(stdout, "What value would you like to append: "); 113 fscanf(stdin, "%d", &input); 114 tmp3=(Node*)malloc(sizeof(Node)); 115 tmp3->value=input; 116 tmp3->next=tmp->next; 117 tmp->next=tmp3; 118 119 tmp=list; 120 input=0; 121 122 while(tmp!=NULL) 123 { 124 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 125 tmp=tmp->next; 126 input=input++; 127 } 128 129 fprintf(stdout, "NULL\n"); 130 131 tmp=list; 132 int remove; 133 fprintf(stdout, "Which node would you like to remove?: "); 134 fscanf(stdin, "%d", &input); 135 136 for(remove=0; remove<(input-1);remove++) 137 { 138 tmp=tmp->next; 139 } 140 if(input==0) 141 { 142 tmp2=tmp->next; 143 tmp->next=NULL; 144 list=tmp2; 145 } 146 else 147 { 148 tmp2=tmp->next; 149 tmp->next=tmp2->next; 150 tmp2->next=NULL; 151 } 152 tmp=list; 153 input=0; 154 while(tmp!=NULL) 155 { 156 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 157 tmp=tmp->next; 158 input=input++; 159 } 160 161 fprintf(stdout, "NULL\n"); 162 163 return(0); 164 }
This is the displayed output of the above code.
lab46:~/src/DATA/finished$ ./SLL 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): -1 [0] 1 ->[1] 2 ->[2] 3 ->[3] 4 ->[4] 5 ->[5] 6 ->NULL Which node would you like to insert before? 2 Enter a value to insert: 9 [0] 1 ->[1] 2 ->[2] 9 ->[3] 3 ->[4] 4 ->[5] 5 ->[6] 6 ->NULL Enter what node you would like to append to: 2 What value would you like to append: 8 [0] 1 -> [1] 2 -> [2] 9 -> [3] 8 -> [4] 3 -> [5] 4 -> [6] 5 -> [7] 6 -> NULL Which node would you like to remove?: 2 [0] 1 -> [1] 2 -> [2] 8 -> [3] 3 -> [4] 4 -> [5] 5 -> [6] 6 -> NULL lab46:~/src/DATA/finished$
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$