diff --git a/Assignment9.txt b/Assignment9.txt new file mode 100644 index 0000000..b9f9028 --- /dev/null +++ b/Assignment9.txt @@ -0,0 +1,17 @@ +1. Throughly compare and contrast malloc() and calloc(). Make sure to include details about parameters, return values, etc. + malloc: format = (castType) malloc (sizeInBytes); + takes only one argument + requires no inilization + use memset () to set memory to 0 + calloc: format = (castType) calloc (blocks, sizeOfBlocks); + takes two arguments + initializes allocates memory to 0 + +2. How would you determine the size of an allocated portion of memory? + We could use sizeof and our pointer /* ?? */ + +3. Why do you have to free memory on the heap but not on the stack? + You have to free memory on the heap because if you dont, it will cause a memory leak. Memory on the stack is automatically freed when the function terminates. + +4. Why do you need to test the return value from malloc? + To test if memory has been successfully allocated diff --git a/linkedLists.c b/linkedLists.c new file mode 100644 index 0000000..81b619d --- /dev/null +++ b/linkedLists.c @@ -0,0 +1,131 @@ +#include +#include + +typedef struct node { + int data; + struct node *next; +} node; + +node* add(node *head, int num) { + node *temp = (node *)malloc(sizeof(node)); + temp->data = num; + temp->next =head; + head = temp; + return head; + + } +void printList (node *curr) { + while (curr->next !=NULL) + { + printf("%d\n", curr -> data); + curr = curr->next; + } +} +void delete (node *curr, int rem) { + while (curr->next != NULL && (curr->next)-> data !=rem) { + curr= curr -> next; + } + if (curr ->next ==NULL) { + printf("The number %d is not in the list\n", rem); + return; + } + node *temp = curr -> next; + curr ->next = temp ->next; + free(temp); + return; +} +node* clear (node *curr) { + node *head=curr; + while (head -> next != NULL) + { + head -> next = curr; + } + while (curr -> next != NULL) + { + curr -> next = head; + } + free (curr); +} + +void smallestLength (node*l1, node *l2) { + int countL1=0; + int countL2=0; + + while (l1 -> next != NULL) { + l1 ->next = l1; + countL1++; + } + while (l2 -> next != NULL) { + l2 ->next = l2; + countL2++; + } + if (countL1> countL2) { + printf("List 1 is bigger\n"); + } + if (countL2> countL1) { + printf("List 2 is bigger\n"); + } + if (countL1== countL2) { + printf("Lists are the same size\n"); + } +} +void smallestSum (node *l1, node *l2) { + int sumL1 = 0; + int sumL2 = 0; + while (l1->next!=NULL) { + l1 ->next = l1; + sumL1+=l1 -> data; + } + while (l2->next!=NULL) { + l2 ->next = l2; + sumL2+=l2 -> data; + } + if (sumL1>sumL2) { + printf("List one has a bigger sum\n"); + return; + } + if (sumL2>sumL1) { + printf("List two has a bigger sum\n"); + return; + } + + if (sumL1==sumL2) { + printf("The lists have equal sums\n"); + return; + } +} +int main () { + struct node * head = (node*) malloc(sizeof(node)); + head -> next = NULL; + node *curr=head; + head =add(head, 23); + head = add(head, 5); + head = add(head, 8); + head = add(head, 12); + head = add(head, 17); + printList (head); + delete (head, 12); + delete (head, 4); + node *l1; + node *l2; + l1 -> next= NULL; + l2 -> next = NULL; + l1 = add (l1, 34); + l1 = add (l1, 1); + l1 = add (l1, 2); + l1 = add (l1, 43); + l1 = add (l1, 12); + + l2 = add (l2, 4); + l2 = add (l2, 9); + l2 = add (l2, 23); + l2 = add (l2, 12); + +// smallestSum (l1, l2); +// smallestLength (l1, l2); + printf("test1\n"); + head = clear (head); + printf("test2\n"); +// printList (head); + return 0; +}