diff --git a/a.out b/a.out new file mode 100755 index 0000000..5395bb8 Binary files /dev/null and b/a.out differ diff --git a/assignment9 b/assignment9 new file mode 100644 index 0000000..e69de29 diff --git a/assignment9.txt b/assignment9.txt new file mode 100644 index 0000000..5e9ccf5 --- /dev/null +++ b/assignment9.txt @@ -0,0 +1,10 @@ +Priyanka Musti +1. +malloc() takes 1 argument and its stored in bytes. calloc() takes 2 argument: array size and element counts. +2. +You can find the size of allocated memory by using realloc to set the size of the array + +3. +You can free memory on the heap but not on the stack because the heap is static but the heap is dynamically allocated. You cannot change how much memory you are using on the stack but in te heap you can. +4. +You need a pointer allocated to memory on success or NULL on failure. diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 0000000..aeb0204 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,126 @@ +#include +#include + +typedef struct node +{ + int data; + struct node *next; +}node; + +void printList(node*head) +{ + node *temp=head; + while(temp->next !=NULL) + { + printf("%d\n", temp-> data); + temp=temp->next; + } +} +node* add(node *head, int data) +{ + node *temp=(node *)malloc(sizeof(node)); + temp-> data=data; + temp -> next=head; + head=temp; + return head; +} +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); +} +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); +} +int smallestLength(node *list_1, node *list_2) +{ + int counter_L1=0; + int counter_L2=0; + + while(list_1->next !=NULL) + { + list_1=list_1->next; + counter_L1++; + } + while(list_2->next!=NULL) + + { + list_2=list_2->next; + 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"); +} +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 value is %d\n", list2size); + } + else printf("Thetare 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; +} + +