Skip to content

Commit

Permalink
need to fix tfree()
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbui904 committed Feb 14, 2022
1 parent 6138c5c commit 1317321
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 50 deletions.
Binary file modified assignment11-starter/linkedlist
Binary file not shown.
32 changes: 10 additions & 22 deletions assignment11-starter/linkedlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,23 @@ void display(Value *list)
}
}

// Return a new list that is the reverse of the one that is passed in. All
// content within the list should be duplicated; there should be no shared
// memory whatsoever between the original list and the new one. Use your
// cons(), car(), and cdr() functions from above -- but be sure that you
// don't end up pointing to memory used by the old list! Hint: this means
// that you'll need to make copies of the Value structs that serve as car
// values for the cons cells in the original list; more specifically, you'll
// want to talloc new space for them on the heap. In the case of a string,
// the strlen() function will come in handy, in addition to strcpy(); note
// that strlen() will not include the null terminator in its count. To use
// these functions, you'll need to include <string.h> above.

// FAQ: What if there are nested lists inside that list?

// ANS: There won't be for this assignment. There will be later, but that will
// be after we've set up an easier way of managing memory.
// Return a new list that is the reverse of the one that is passed in. None of
// the values in the original linked list should be copied this time. Instead,
// create a new linked list of CONS_TYPE nodes whose car values point to the
// corresponding car values in the original list.
Value *reverse(Value *list)
{
// copy over the current values into heap
Value *head = list;
Value *new_head = makeNull();
Value *current = list;
Value *reverse_list = makeNull();

// at the last node of the list
while (!isNull(head))
while (!isNull(current))
{
new_head = cons(car(head), new_head);
head = cdr(head);
reverse_list = cons(car(current), reverse_list);
current = cdr(current);
}
return new_head;
return reverse_list;
}

// Return the length of the given list, i.e., the number of cons cells.
Expand Down
Binary file modified assignment11-starter/linkedlist.o
Binary file not shown.
32 changes: 4 additions & 28 deletions assignment11-starter/talloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,15 @@ void *talloc(size_t size)
// helper function to make freeing all associated memories of talloc easy
void cleanup(Value *head)
{
if (head->type == CONS_TYPE)
if (head->type == NULL_TYPE)
{
cleanup(head->c.car);
cleanup(head->c.cdr);
free(head);
}
else
{
// need to free up the NODES too
switch (head->type)
{
// go inside the address stored at the ptr, and free those as well
case PTR_TYPE:
cleanup(head->p);
free(head);
break;
case INT_TYPE:
free(head);
break;

case DOUBLE_TYPE:
free(head);
break;
case STR_TYPE:
free(head->s);
free(head);
break;
case NULL_TYPE:
free(head);
break;
default:
printf("what the fuck\n");
}
free(head->c.car);
free(head->p);
cleanup(head->c.cdr);
}
}

Expand Down
Binary file modified assignment11-starter/talloc.o
Binary file not shown.

0 comments on commit 1317321

Please sign in to comment.