Skip to content

Commit

Permalink
finished up the natural number struct and associated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ellgramar committed Mar 25, 2023
1 parent 38a956e commit 2fa4ca1
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
11 changes: 11 additions & 0 deletions main.c
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;
}
99 changes: 99 additions & 0 deletions natural.c
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;
}
29 changes: 29 additions & 0 deletions natural.h
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

0 comments on commit 2fa4ca1

Please sign in to comment.