-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished up the natural number struct and associated functions
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// Created by Elliot Marshall on 3/20/23. | ||
// | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include "natural.h" | ||
int main (int argc, char** argv[]){ | ||
int naturalnum = sizeof(natural); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// Created by Elliot Marshall on 3/20/23. | ||
// | ||
|
||
#include "natural.h" | ||
|
||
// constructor for a natural number | ||
struct natural newNatural(){ | ||
struct natural * n = calloc(1,sizeof(natural)); | ||
n.val = NULL; | ||
n.prev = NULL; | ||
n.next = NULL; | ||
return n; | ||
} | ||
// convert a long to a natural | ||
struct natural longToNatural(long number){ | ||
natural n = newNatural(); | ||
n.val = (uint64_t)number; | ||
return n; | ||
} | ||
// frees the natural number in a list | ||
void freeNatural(struct natural n){ | ||
// set n to the last node | ||
n = getTail(n); | ||
// iterate from the last node and free each one until the first node | ||
while (n.prev != NULL){ | ||
n.val = NULL | ||
n = n.prev; | ||
n.next.prev = NULL; | ||
free(n.next); | ||
n.next = NULL; | ||
} | ||
// now free the first node | ||
n.val = NULL; | ||
n.prev = NULL; | ||
n.next = NULL; | ||
free(n); | ||
return; | ||
} | ||
|
||
// convenience functions | ||
// get the head of the LL of the number | ||
struct natural getHead(struct natural n){ | ||
// while n is not the first one, reset n to be the previous one | ||
while (n.prev != NULL){ | ||
n = n.prev; | ||
} | ||
return n; | ||
} | ||
// get the tail of the linked list of the natural | ||
struct natural getTail(struct natural n){ | ||
// while n is not the lst one, reset n to be the next one | ||
while (n.next != NULL){ | ||
n = n.next; | ||
} | ||
return n; | ||
} | ||
// append a number to the tail of the list | ||
struct natural append(struct natural old, struct natural addition){ | ||
old = getTail(old); | ||
old.next = addition; | ||
additon.prev = old; | ||
old = getHead(old); | ||
return old; | ||
} | ||
// append a list to a natural | ||
struct natural prepend(struct natuarl old, struct natural addition){ | ||
old = getHead(old); | ||
old.prev = addition; | ||
addition.next = old; | ||
old = getHead(old); | ||
return old; | ||
} | ||
// deletes a specific value in a LL if it exists, only the first value, so be careful | ||
void delByValue(long value, struct natural n){ | ||
n = getHead(n); | ||
while (n.val != value){ | ||
n = n.next; | ||
} | ||
if ((n.val == value) && (n.prev != NULL)){ | ||
n = n.prev; | ||
free(n.next); | ||
} else if ((n.val == value) && (n.next != NULL)){ | ||
n = n.next; | ||
free(n.prev); | ||
} else{ | ||
free(n); | ||
} | ||
return; | ||
} | ||
// deletes a natural in a list by index | ||
struct natural delByIndex(int index, struct natural n){ | ||
n = getHead(n); | ||
while ((index > 0) && n.next != NULL){ | ||
n = n.next; | ||
} | ||
free(n); | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// Created by Elliot Marshall on 3/20/23. | ||
// | ||
#ifndef NATURAL_H | ||
#define NATURAL_H | ||
// includes | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
// struct | ||
typedef struct natural { | ||
uint64_t val; | ||
struct natural * prev; | ||
struct natural * next; | ||
}natural; | ||
// functions | ||
struct natural newNatural(); | ||
struct natural longToNatural(long number); | ||
void freeNatural(struct natural n); | ||
|
||
|
||
// convenience functions | ||
struct natural getHead(struct natural n); | ||
struct natural getTail(struct natural n); | ||
struct natural append(struct natural old, struct natural addition); | ||
struct natural prepend(struct natuarl old, struct natural addition); | ||
struct natural insertByIndex(uint64_t index, struct natural n); | ||
void delByValue(long value, struct natuarl n); | ||
void delByIndex(int index, struct natuarl n); | ||
#endif // NATURAL_H |