User Tools

Site Tools


opus:spring2014:khoose2:journal

Data Structures Journal

January 22, 2014

This first week was spent, reviewing old concepts I had lost touch with over break.

MONTH Day, YEAR

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:

  • What action or concept of significance, as related to the course, did you experience on this date?
  • Why was this significant?
  • What concepts are you dealing with that may not make perfect sense?
  • What challenges are you facing with respect to the course?
varsize
  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
--------

February 5th,2014

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.

node.c
  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$

February 20th,2014

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 }

February 21,2014

SLL.c
  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$
opus/spring2014/khoose2/journal.txt · Last modified: 2014/02/23 01:57 by khoose2