init
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
#include "basic_tree.h"
|
||||
|
||||
struct node *insert(struct node *root, int value);
|
||||
|
||||
void delete_leaf(struct node *root, int value);
|
||||
|
||||
struct node *delete_node(struct node *root, int value);
|
||||
|
||||
void pretty_print(struct node *root);
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef BASIC_TREE_H
|
||||
#define BASIC_TREE_H
|
||||
|
||||
struct queue {
|
||||
struct node *node;
|
||||
struct queue *next;
|
||||
};
|
||||
|
||||
struct queue *enqueue(struct queue *queue, struct node *node);
|
||||
struct node *dequeue(struct queue **queue);
|
||||
void queue_destroy(struct queue *queue);
|
||||
|
||||
|
||||
struct node {
|
||||
int value;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
};
|
||||
|
||||
struct node *insert(struct node *root, int value);
|
||||
void delete_leaf(struct node *root, int value);
|
||||
struct node *delete_node(struct node *root, int value);
|
||||
void pretty_print(struct node *root);
|
||||
|
||||
#endif /* ! BASIC_TREE_H */
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "basic_tree.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct queue *enqueue(struct queue *queue, struct node *node) {
|
||||
struct queue *new_elt = malloc(sizeof(struct queue));
|
||||
if (!new_elt)
|
||||
return queue;
|
||||
new_elt->node = node;
|
||||
new_elt->next = NULL;
|
||||
if (!queue) {
|
||||
return new_elt;
|
||||
}
|
||||
struct queue *it = queue;
|
||||
while (it->next) {
|
||||
it = it->next;
|
||||
}
|
||||
it->next = new_elt;
|
||||
return queue;
|
||||
}
|
||||
|
||||
struct node *dequeue(struct queue **queue) {
|
||||
if (!queue || !*queue)
|
||||
return NULL;
|
||||
struct node *res = (*queue)->node;
|
||||
struct queue *temp = *queue;
|
||||
*queue = (*queue)->next;
|
||||
free(temp);
|
||||
return res;
|
||||
}
|
||||
|
||||
void queue_destroy(struct queue *queue) {
|
||||
while (queue) {
|
||||
dequeue(&queue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "expansion.h"
|
||||
|
||||
char *weird_copy(const char *input);
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef EXPANSION_H
|
||||
#define EXPANSION_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct var {
|
||||
char *key;
|
||||
char *value;
|
||||
struct var *next;
|
||||
};
|
||||
|
||||
struct var *var_add(struct var *list, const char *key, const char *value);
|
||||
const char *var_get(struct var *list, const char *key);
|
||||
size_t var_size(struct var *list);
|
||||
int var_update(struct var *list, const char *key, const char *value);
|
||||
void var_free(struct var *list);
|
||||
|
||||
char *weird_copy(const char *input);
|
||||
|
||||
#endif /* ! EXPANSION_H */
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "expansion.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct var *var_add(struct var *list, const char *key, const char *value) {
|
||||
struct var *node = malloc(sizeof(struct var));
|
||||
if (!node)
|
||||
return NULL;
|
||||
node->key = strdup(key);
|
||||
node->value = strdup(value);
|
||||
if (!node->key || !node->value) {
|
||||
free(node->key);
|
||||
free(node->value);
|
||||
free(node);
|
||||
return NULL;
|
||||
}
|
||||
node->next = list;
|
||||
return node;
|
||||
}
|
||||
|
||||
const char *var_get(struct var *list, const char *key) {
|
||||
while (list)
|
||||
{
|
||||
if (strcmp(list->key, key) == 0)
|
||||
return list->value;
|
||||
list = list->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t var_size(struct var *list) {
|
||||
size_t n = 0;
|
||||
while (list)
|
||||
{
|
||||
n++;
|
||||
list = list->next;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int var_update(struct var *list, const char *key, const char *value) {
|
||||
while (list)
|
||||
{
|
||||
if (strcmp(list->key, key) == 0) {
|
||||
char *new_val = strdup(value);
|
||||
if (!new_val)
|
||||
return -1;
|
||||
free(list->value);
|
||||
list->value = new_val;
|
||||
return 0;
|
||||
}
|
||||
list = list->next;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void var_free(struct var *list) {
|
||||
while (list) {
|
||||
struct var *next = list->next;
|
||||
free(list->key);
|
||||
free(list->value);
|
||||
free(list);
|
||||
list = next;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "parsing.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
struct node *create_node(const char *line);
|
||||
|
||||
struct node *parse_file(const char *filename);
|
||||
|
||||
void execute_tree(struct node *node, int value);
|
||||
|
||||
int main(int argc, char *argv[]);
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef PARSING_H
|
||||
#define PARSING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
enum node_type {
|
||||
EMPTY,
|
||||
PLUS,
|
||||
MINUS,
|
||||
PRINT
|
||||
};
|
||||
|
||||
enum child {
|
||||
LEFT,
|
||||
RIGHT
|
||||
};
|
||||
|
||||
enum condition_type {
|
||||
INT,
|
||||
CHILD
|
||||
};
|
||||
|
||||
union condition {
|
||||
int n;
|
||||
enum child child;
|
||||
};
|
||||
|
||||
struct node {
|
||||
enum node_type node_type;
|
||||
union condition condition;
|
||||
enum condition_type condition_type;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
};
|
||||
|
||||
struct queue {
|
||||
struct node *node;
|
||||
struct queue *next;
|
||||
};
|
||||
|
||||
struct queue *enqueue(struct queue *queue, struct node *node);
|
||||
struct node *dequeue(struct queue **queue);
|
||||
void queue_destroy(struct queue *queue);
|
||||
|
||||
struct node *create_node(const char *line);
|
||||
struct node *parse_file(const char *filename);
|
||||
void execute_tree(struct node *node, int value);
|
||||
|
||||
#endif /* ! PARSING_H */
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "parsing.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct queue *enqueue(struct queue *queue, struct node *node) {
|
||||
struct queue *new_elt = malloc(sizeof(struct queue));
|
||||
if (!new_elt)
|
||||
return queue;
|
||||
new_elt->node = node;
|
||||
new_elt->next = NULL;
|
||||
if (!queue) {
|
||||
return new_elt;
|
||||
}
|
||||
struct queue *it = queue;
|
||||
while (it->next) {
|
||||
it = it->next;
|
||||
}
|
||||
it->next = new_elt;
|
||||
return queue;
|
||||
}
|
||||
|
||||
struct node *dequeue(struct queue **queue) {
|
||||
if (!queue || !*queue)
|
||||
return NULL;
|
||||
struct node *res = (*queue)->node;
|
||||
struct queue *temp = *queue;
|
||||
*queue = (*queue)->next;
|
||||
free(temp);
|
||||
return res;
|
||||
}
|
||||
|
||||
void queue_destroy(struct queue *queue) {
|
||||
while (queue) {
|
||||
dequeue(&queue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "vector_tree.h"
|
||||
|
||||
struct vector *insert(struct vector *tree, int value);
|
||||
|
||||
void delete_node(struct vector *tree, int value);
|
||||
|
||||
void pretty_print(struct vector *tree);
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef VECTOR_TREE
|
||||
#define VECTOR_TREE
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct vector {
|
||||
int *values;
|
||||
size_t size;
|
||||
size_t nb_elements;
|
||||
};
|
||||
|
||||
struct vector *insert(struct vector *tree, int value);
|
||||
void delete_node(struct vector *tree, int value);
|
||||
void pretty_print(struct vector *tree);
|
||||
|
||||
#endif /* ! VECTOR_TREE */
|
||||
Reference in New Issue
Block a user