From 5ef9a02a82374027c43ecd5858eacaa691f8249a Mon Sep 17 00:00:00 2001 From: "uityh.uityh" Date: Fri, 5 Aug 2016 09:19:24 -0400 Subject: [PATCH 1/4] Matthew Kellerman --- assignment9.txt | 25 +++++++ linkedlist.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 assignment9.txt create mode 100644 linkedlist.c diff --git a/assignment9.txt b/assignment9.txt new file mode 100644 index 0000000..fec550d --- /dev/null +++ b/assignment9.txt @@ -0,0 +1,25 @@ +Matthew Kellerman + +1. +malloc and calloc are both functions that are used to allocate bytes of memory to specifc variables. + +malloc() is written as dataType *malloc(size_t numBytes) + +calloc() is written as dataType *calloc(size_t numElements, size_t size) + +malloc() allocates the paramenter numBytes in that number of bytes in heap + +The elements are not initialized and returns a pointer to the allocated memory on success. If it fails, NULL is returned. + +calloc() allocates the size(in bytes) times the number of elements in memory. + +Its elements are set to zero and like malloc, returns a pointer(or NULL) to the allocated memory on success or failure. + +2. +I would determine the size of an allocated portion of memory by multiplying the amount of items that are stored by the number of bytes they each take up. For example, let's say I have a char array with three characters. I can multiple the three by one (char == 1 byte) to find the size of the allocated memory. + +3. +You have free memory on the heap but not the on the stack because the stack's memory is freed automatically,(after your program runs), but since the heap can store values from multiple programs, you have to free the memory manually. + +4. +You need to test the return value from malloc because you need to check if you the memory was allocated successfully. If it wasn't, you don't have enough memory left in your heap which could cause you to not be able to use the heap at all. diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 0000000..6fc77c4 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,180 @@ +//Matthew Kellerman +//code manipulates linked lists to print them out, add a node with an integer to them, remove a node with a specific intger, find which of two linked list has the smallest length/size + +#include +#include + + +typedef struct node { + int data; + struct node *next; +}node; + +//allows me to create nodes for data structure +//when you create a new node, it will go the beginnig of the list +node* create(node *pointer,int data) + { +node *newPtr =(struct node*)malloc(sizeof(struct node)); +newPtr->next = pointer; +newPtr->data = data; +return newPtr; + + } +//prints numbers in linked list +void printList(node *print) +{ +while(print->next!=NULL) +{//prints the value of the node + printf("%d\n",(*print).data); + print = print->next; +//points to another node +} +} +//allows me to remove node +void remove1(node *pointer, int data) +{ +//while the pointer doesn't point to NULL and the pointer doesn't point to the value you want to delete +while(pointer->next!=NULL &&(pointer->next)->data!=data) +{ + +pointer = pointer ->next; +} +if(pointer->next==NULL) +{//if the number can't be found +printf("%d does not exist\n",data); +return; +} +node *tempPoint = pointer->next;//temp points to the node which has to be removed + +pointer->next = tempPoint->next;//removes node next to pointer +free(tempPoint); +//frees temp +printf("%d was successfully removed\n",data); +return; +} +void clear(node*pr) +{ +node *temp; +while(pr->next!=NULL) +{ + //does same thing as remove1 function except does it for all the nodes except the head one +temp = pr; +free(pr); +pr = temp; +pr = pr->next; +} +} + +int smallestLength(node*p, node*p2) +{ +int counter = 0; +int flag = 0; +while(p->next!=NULL) + //while loop checks to see how many nodes there are +{ +counter++; +p= p->next; +} + +while(p2->next!=NULL) +{ + flag++; + p2 = p2->next; +} +if(flag>counter){ + +return counter; +} +else {if(flag==counter){ + +printf("Both linked lists are the same size.\n"); +return flag; +} +else{ + +printf("The first linked list is longer.\n"); +return flag; +} +} +} +void smallestSum(node*ptr,node*ptr2) +{ +int counter = 0; +int flag =0; + +while(ptr->next!=NULL) +{ + counter+=ptr->data; + ptr = ptr->next; +} +while(ptr2->next!=NULL) +{ + flag+=ptr2->data; + ptr2 = ptr2->next; +} +// flag and counter represent the size of the linked lists +if(counter>flag) +{ + printf("The second list has the smallest sum.\n"); +} +else{ +if(flag>counter){ + printf("The first list has the smallest sum.\n"); + +} +else{ +printf("The lists have an equal sum.\n"); +} +} + +} +int main() +{ +struct node *head; +//allocates memory for new data structure + +head = (struct node*)malloc(sizeof(struct node)); + +head->next= NULL; + +head = create(head,37); +head = create(head,17); +head = create(head,9); +head = create(head,2); + +printList(head); + +printf("\n"); + +remove1(head,17); +remove1(head,16); +//since 17 is removed, first linked list has only three nodes +struct node*head2; +//allocates memory for new data structure +head2= (struct node *)malloc(sizeof(struct node)); +head2->next=NULL; + +head2 = create(head2,34); +head2 = create(head2,56); +head2 = create(head2,1); +head2 = create(head2,5); + +printf("The second linked list is:\n"); +printList(head2); +printf("\n"); + +printf("%d is the size of the smaller list\n",smallestLength(head,head2)); + +printf("\n"); + head2= create(head2,-10); + printList(head2); + +printf("\n"); +smallestSum(head,head2); +clear(head); +clear(head2); + +free(head); +free(head2); +return 0; +} From 60d0457563f960f24816e2d448a0c0901b126fb7 Mon Sep 17 00:00:00 2001 From: "uityh.uityh" Date: Fri, 5 Aug 2016 09:35:56 -0400 Subject: [PATCH 2/4] Matthew Kellerman --- linkedlist.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/linkedlist.c b/linkedlist.c index 6fc77c4..02960b4 100644 --- a/linkedlist.c +++ b/linkedlist.c @@ -12,7 +12,7 @@ typedef struct node { //allows me to create nodes for data structure //when you create a new node, it will go the beginnig of the list -node* create(node *pointer,int data) +node* add(node *pointer,int data) { node *newPtr =(struct node*)malloc(sizeof(struct node)); newPtr->next = pointer; @@ -58,11 +58,13 @@ node *temp; while(pr->next!=NULL) { //does same thing as remove1 function except does it for all the nodes except the head one + temp = pr; free(pr); pr = temp; pr = pr->next; } +free(pr); } int smallestLength(node*p, node*p2) @@ -137,10 +139,10 @@ head = (struct node*)malloc(sizeof(struct node)); head->next= NULL; -head = create(head,37); -head = create(head,17); -head = create(head,9); -head = create(head,2); +head = add(head,37); +head = add(head,17); +head = add(head,9); +head = add(head,2); printList(head); @@ -154,10 +156,10 @@ struct node*head2; head2= (struct node *)malloc(sizeof(struct node)); head2->next=NULL; -head2 = create(head2,34); -head2 = create(head2,56); -head2 = create(head2,1); -head2 = create(head2,5); +head2 = add(head2,34); +head2 = add(head2,56); +head2 = add(head2,1); +head2 = add(head2,5); printf("The second linked list is:\n"); printList(head2); @@ -166,7 +168,7 @@ printf("\n"); printf("%d is the size of the smaller list\n",smallestLength(head,head2)); printf("\n"); - head2= create(head2,-10); + head2= add(head2,-10); printList(head2); printf("\n"); From 1759ac63259894a47dad8e805125a5fbf7232c32 Mon Sep 17 00:00:00 2001 From: Matthew Kellerman Date: Fri, 5 Aug 2016 09:37:15 -0400 Subject: [PATCH 3/4] Update assignment9.txt --- assignment9.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignment9.txt b/assignment9.txt index fec550d..a9ad7dc 100644 --- a/assignment9.txt +++ b/assignment9.txt @@ -19,7 +19,7 @@ Its elements are set to zero and like malloc, returns a pointer(or NULL) to the I would determine the size of an allocated portion of memory by multiplying the amount of items that are stored by the number of bytes they each take up. For example, let's say I have a char array with three characters. I can multiple the three by one (char == 1 byte) to find the size of the allocated memory. 3. -You have free memory on the heap but not the on the stack because the stack's memory is freed automatically,(after your program runs), but since the heap can store values from multiple programs, you have to free the memory manually. +You have free memory on the heap but not the on the stack because the stack's memory is freed automatically,(after your program ends), but since the heap can store values from multiple programs, you have to free the memory manually. 4. You need to test the return value from malloc because you need to check if you the memory was allocated successfully. If it wasn't, you don't have enough memory left in your heap which could cause you to not be able to use the heap at all. From 4f26f5859bbc02b55c2d618adf1c6f8ee03948ed Mon Sep 17 00:00:00 2001 From: Matthew Kellerman Date: Fri, 5 Aug 2016 09:43:53 -0400 Subject: [PATCH 4/4] Update linkedlist.c --- linkedlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linkedlist.c b/linkedlist.c index 02960b4..d4dc291 100644 --- a/linkedlist.c +++ b/linkedlist.c @@ -57,7 +57,7 @@ void clear(node*pr) node *temp; while(pr->next!=NULL) { - //does same thing as remove1 function except does it for all the nodes except the head one + //does same thing as remove1 function except does it for all the nodes except the head one and frees them temp = pr; free(pr);