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
9 changes: 9 additions & 0 deletions assignment9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Drew French

1) malloc() takes size_t & number of bytes of space and allocates that amount of bytes; you then need to fill the space. Calloc takes size_t, number of bytes per element, and number of elements and allocates [bytes per element] for each element. This space is filled by these elements.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the space is actually initialized with 0s in calloc!


2) Maybe iterate through it to fill it with integers and multiply the number of integers by 4 to get the amount of bytes?

3) Heap resets only when the computer is restarted; stack resets when the program ends.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right that the stack resets for each program, but you're not quite right about the heap!


4) If it's NULL, there is no space.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right! so you should test for this to make sure it's been properly allocated.

64 changes: 64 additions & 0 deletions linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Drew French */

/* Does stuff with
* linked lists.
*/

/* Did not finish; trying
* to figure out why what
* there is so far is
* getting errors.
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int n;
struct node *next;
} nodeType;

void printList(nodeType *head)
{
nodeType *current = head;
while(current != NULL)
{
printf("%d\n", current->n)
current = current->next;
}
}

void clear(nodeType *head)
{
nodeType *current = head;
while(current != NULL)
{
free(*current);
}
}

int main()
{
nodeType *head = NULL;

head = malloc(sizeof(nodeType));
head->n = 3;
head->next = malloc(sizeof(nodeType));
head->next->n = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these steps should have been done through the insert() function! see how you have to rewrite the code each time? That's why we use functions!

head->next->next = malloc(sizeof(nodeType));
head->next->next->n = 4;
head->next->next->next = malloc(sizeof(nodeType));
head->next->next->next->n = 1;
head->next->next->next->next = malloc(sizeof(nodeType));
head->next->next->next->next->n = 5;
head->next->next->next->next->next = malloc(sizeof(nodeType));
head->next->next->next->next->next->n = 9;
head->next->next->next->next->next->next = NULL;

printList(*head);

clear(*head);

return 0;
}