Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions assignment9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*Rebecca Hale Assignment 9 Part 2*/
1. malloc() and calloc() are both functions that are used to allocate
bytes of memory to specific variables. However, calloc() automatically
initializes the memory to 0 while malloc does not have jurisdiction
to access the memory and save values within it. malloc() is only passed
the variables size in bytes while calloc() is passed the number of items
and the size of each variable in bytes. Both functions return a void
pointer.

2. The size of an allocated portion of memory can be determined by
multiplying the amount of items that are stored in that portion of
memory by the amount of bytes used by the variables stored in each portion
of memory. For instance, if I wanted to determine the amount of memory
used to store an integer array of length 5, I would multiply the amount of
elements, 5, by the amount of bytes used in each element, 4, to find out
the total amount of memory allocated to the array.

3. Memory must be freed in heap but not in stack because memory saved in
stack is automatically freed while memory in heap that is not freed will
result in memory leaks, which means places of memory will be quarantined
and will not be able to store any new data.

4. The return value of malloc() must be saved because memory in malloc() isstored in heap and memory that is not freed in heap can result in memory
leaks. If malloc() fails and returns NULL and the return value is not
checked, the program could crash because not enough memory space could
be available on the machine.
154 changes: 154 additions & 0 deletions linkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *next;
} node;

void printList(node *n){/*prints out linked list*/
if(n->next==NULL){
printf("\n%d\n", n->data);
return;
}
n=n->next;
do{
printf("%d\n", n->data);
n=n->next;
}while(n->next!=NULL);
printf("%d\n", n->data);/*prints out data of last node*/
}

void add(node *n, int i){
node *new = (node *) malloc(sizeof(node));/*allocated space*/
new->data=i;/*sets data to i*/
new->next=n->next;/*sets node pointer of new to first node*/
n->next=new;/*sets head pointer to new pointer*/
}

void remove1(node *n, int i){
int index=0;
node *tempHead=n;
while(n->next!=NULL && n->data!=i){/*finds location of int*/
index++;
n=n->next; /*increments to next location in list*/
}
if(n->next==NULL && n->data!=i)/*prints error if no int found*/
fprintf(stderr,"\nError, integer not found in list.\n");
else {
node *before;
node *after;
node *temp;
temp=n;
before=tempHead;/*starts at beginning of list*/
after=n->next;
int j;
for(j=0; j<index-1; j++)
before=before->next;/*sets before pointer*/
before->next=after;/*moves before pointer to after*/
free(temp);

}
}
int lastNode(node *n){/*returns last node data integer*/
while(n->next!=NULL){
n=n->next;
}

return n->data;
}
int sizeOfList(node *n){/*returns size of list*/
int i=0;
while(n->next!=NULL){
i++;
n=n->next;
}

return i;
}
void clear(node *n){
node *temphead = n;
int last = lastNode(n);
int size = sizeOfList(n);
while(size>0){
remove1(temphead, last);/*removes last element*/
last = lastNode(temphead);
size = sizeOfList(temphead);
}
free(temphead);/*frees head node*/
}

int smallestLength(node *one, node *two){
int oneSize = sizeOfList(one);
int twoSize = sizeOfList(two);
if(oneSize<twoSize){
fprintf(stderr, "\nThe first list given is smallest\n");
return oneSize;
}
fprintf(stderr, "\nThe second list given is smallest\n");
return twoSize;
}

void smallestSum(node *one, node *two){
int oneSize = sizeOfList(one);/*gets size of list*/
int twoSize = sizeOfList(two);/*gets size of list*/
int count1=0;
int count2=0;
one=one->next;/*sets to node past header*/
two=two->next;/*sets to node past header*/
for(int i=0; i<oneSize; i++){
count1+=one->data;
one=one->next;
}
for(int j=0; j<twoSize; j++){
count2+=two->data;
two=two->next;
}
if(count1>count2){
fprintf(stderr, "The sum of the second list is smaller.");
return;
}
fprintf(stderr, "The sum of the first list is smaller.");
}

int main(){
node *head = (node *) malloc(sizeof(node));/*creates head node*/
node *one = (node *) malloc(sizeof(node));
node *two = (node *) malloc(sizeof(node));
node *three = (node *) malloc(sizeof(node));
node *four = (node *) malloc(sizeof(node));/*creates pointers*/

head->next=one;
one->next=two;
two->next=three;
three->next=four;/*sets next pointer for all nodes*/
four->next=NULL;

head->data=-1;
one->data=1;
two->data=2;
three->data=3;
four->data=4;/*sets data for all nodes*/

node *head2 = (node *) malloc(sizeof(node));
node *one2 = (node *) malloc(sizeof(node));
node *two2 = (node *) malloc(sizeof(node));

head2->next=one2;
one2->next=two2;
two2->next=NULL;

head2->data=-1;
one2->data=111;
two2->data=222;

free(head);
free(head2);
free(one);
free(one2);
free(two);
free(two2);
free(three);
free(four);/*frees all data*/
return 0;
}