From 6f000cd2790629420f4bbd775bdb9660547e83c6 Mon Sep 17 00:00:00 2001 From: Eli Lichtblau Date: Thu, 4 Aug 2016 19:50:26 -0400 Subject: [PATCH 1/2] does not take userinput --- linkedlist.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 linkedlist.c diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 0000000..974de00 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,202 @@ +#include +#include + + +typedef struct node +{ + int data; + struct node *next; + +}node; + + +/*Print a give linked list + + parameter(list) + */ +void printList(node *head) +{ + node *temp = head; + + while(temp-> next != NULL) + { + printf("%d\n", temp -> data); + temp = temp ->next; + } +} + +/* adds an interger and a node to the beggining of a list + + parameter(linked list, interger) + */ +node* add(node *head, int data) +{ + + node *temp = (node *)malloc(sizeof(node)); + temp-> data = data; + temp -> next = head; + head = temp; + + return head; + + + +} + + + +/* given a linked and an interger, remove the first instance of the interger prints an error if that number doesnt exist + + parameter(linked list, interger) + */ +void delete(node *ptr,int data) +{ + + while(ptr->next != NULL && (ptr->next)->data!=data) + { + ptr = ptr-> next; + + } + if(ptr-> next == NULL) + { + printf("%d does not exist in the list", data); + return; + } + node *temp = ptr ->next; + ptr ->next = temp->next; + free(temp); +} + +/*given a linked list it deletes everything + + parameter(linked list) + */ +void clear(node *ptr) +{ + while(ptr -> next != NULL) + { + node *temp = ptr -> next; + ptr -> next = temp->next; + free(temp); + + } + node *temp = ptr -> next; + ptr -> next = temp -> next; + free(temp); +} + +/*given two linked lisets of intergers, find length of smaller list + + paramter(linked list, linked list) + */ +int smallestLength(node *list_1, node *list_2) +{ + int counter_L1 = 0; + int counter_L2 = 0; +/* fprintf(stderr,"%d\n",list_2->next==NULL);*/ + + while(list_1->next != NULL) + { + list_1 = list_1-> next; + counter_L1++; + // printf("%d\n", counter_L1); + } +// fprintf(stderr,"finished list 1 - starting list 2\n"); + while(list_2->next != NULL) + { + list_2 = list_2 -> next; + counter_L2++; + // printf("%d\n", counter_L2); + } + + if(counter_L1 > counter_L2) + { + printf("the first list is larger\n"); + return counter_L1; + } + + else if(counter_L2 > counter_L1) + { + printf("The second one is larger\n"); + return counter_L2; + } + + else printf("They are the same size\n"); + + + +} + +/*Given two linked list of integers, write a funciton to find the list with smallest sum + + paramters(linked list, linked list) + */ +void smallestSum(node *list1, node*list2) +{ + int list1size = 0; + int list2size = 0; + + while(list1->next != NULL) + { + list1size += list1->data; + list1 = list1->next; + } + + while(list2->next !=NULL) + { + + list2size += list2->data; + list2 = list2->next; + } + + + if(list2size>list1size) + { + printf("The first list is smaller, the value is %d\n", list1size); + } + else if(list1size>list2size) + { + printf("The second list is smaller, the vaule is %d\n", list2size); + + } + + else printf("They are the same size"); + + } + + +int main() +{ + +struct node *head = (struct node *) malloc (sizeof(struct node)); +struct node *headTwo = (struct node *) malloc (sizeof(struct node)); + +head = add(head, 4); +head = add(head, 5); +head = add(head, 6); +head = add(head, 5); +headTwo = add(headTwo, 40); +headTwo = add(headTwo, 40); +delete(head, 5); + printList(headTwo); + printList(head); + smallestLength(head, headTwo); + smallestSum(head, headTwo); + return 0; +} + + + + + + + + + + + + + + + + From d5fa36b58984e8fb21ebbccec5647094e7d00b3c Mon Sep 17 00:00:00 2001 From: Eli Lichtblau Date: Thu, 4 Aug 2016 19:52:42 -0400 Subject: [PATCH 2/2] Assignment9.txt --- assignment9.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 assignment9.txt diff --git a/assignment9.txt b/assignment9.txt new file mode 100644 index 0000000..2be062a --- /dev/null +++ b/assignment9.txt @@ -0,0 +1,12 @@ +1.malloc takes 1 argument, memory required in bytes, calloc however takes 2 arguments, the amunt of elements and the size of each element + + malloc doesnt initialize while calloc initializes to 0 - this means that malloc goes with memset + + however they both return a pointer on success and NULL on failure + +2. There is no direct way to determine the size of allocated memory, (as far as I know), but you can either check the code or use realloc to set the size of allocated memory and then you would know + + +3. I would guess you have to free memory from the heap and not the stack b/c the heap is dynamically allocated, and memory in the stack is static, you cannot change how much you are using so it wouldnt make sense to be able to free it, however in the heap the amount of memory being used can change so when you change it you need to tell the computer that it can use that memory again b/c it is not being used. + +4. You need to test the return value of malloc b/c it returns NULL on failure and a pointer on success so if it returns NULL you know that it failed