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 */
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "evaluate_rpn.h"
|
||||
|
||||
int evaluate_rpn(struct queue *queue);
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef EVALUATE_RPN_H
|
||||
#define EVALUATE_RPN_H
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
int evaluate_rpn(struct queue *queue);
|
||||
|
||||
#endif /* ! EVALUATE_RPN_H*/
|
||||
@@ -0,0 +1 @@
|
||||
int main(int argc, char** argv);
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "parse_exp.h"
|
||||
|
||||
struct queue *parse_expression(const char *str);
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef PARSE_EXP_H
|
||||
#define PARSE_EXP_H
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
struct queue *parse_expression(const char *str);
|
||||
|
||||
#endif /* ! PARSE_EXP_H */
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "queue.h"
|
||||
|
||||
struct queue *enqueue(struct queue *queue, struct expression_element* element) {
|
||||
struct queue *new_elt = calloc(1,sizeof(struct queue));
|
||||
|
||||
new_elt->data = element;
|
||||
|
||||
if(!queue)
|
||||
return new_elt;
|
||||
|
||||
struct queue* current = queue;
|
||||
|
||||
while(current->next)
|
||||
current = current->next;
|
||||
|
||||
current->next = new_elt;
|
||||
|
||||
return queue;
|
||||
}
|
||||
|
||||
struct expression_element* dequeue(struct queue **queue) {
|
||||
if (!queue || !*queue)
|
||||
return NULL;
|
||||
|
||||
struct queue* removed = *queue;
|
||||
struct expression_element* res = removed->data;
|
||||
|
||||
*queue = removed->next;
|
||||
free(removed);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void queue_destroy(struct queue **queue) {
|
||||
if(!queue)
|
||||
return;
|
||||
while (*queue) {
|
||||
struct expression_element* dequeued = dequeue(queue);
|
||||
free(dequeued);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
union element_type
|
||||
{
|
||||
int operand;
|
||||
char operator;
|
||||
};
|
||||
|
||||
enum element_tag {
|
||||
TOKEN_OPERAND,
|
||||
TOKEN_OPERATOR
|
||||
};
|
||||
|
||||
struct expression_element {
|
||||
enum element_tag tag;
|
||||
union element_type data;
|
||||
};
|
||||
|
||||
struct queue {
|
||||
struct expression_element* data;
|
||||
struct queue *next;
|
||||
};
|
||||
|
||||
struct queue *enqueue(struct queue *queue, struct expression_element* element);
|
||||
struct expression_element* dequeue(struct queue **queue);
|
||||
void queue_destroy(struct queue **queue);
|
||||
|
||||
#endif /* ! QUEUE_H */
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "stack.h"
|
||||
|
||||
struct stack *stack_push(struct stack *s, struct expression_element e)
|
||||
{
|
||||
struct stack *new = malloc(sizeof(struct stack));
|
||||
new->next = NULL;
|
||||
new->data = e;
|
||||
if (s == NULL)
|
||||
return new;
|
||||
new->next = s;
|
||||
return new;
|
||||
}
|
||||
|
||||
struct stack *stack_pop(struct stack *s)
|
||||
{
|
||||
if (!s)
|
||||
return NULL;
|
||||
struct stack *next = s->next;
|
||||
free(s);
|
||||
return next;
|
||||
}
|
||||
|
||||
void stack_destroy(struct stack **s)
|
||||
{
|
||||
if(!s)
|
||||
return;
|
||||
while (*s)
|
||||
{
|
||||
*s = stack_pop(*s);
|
||||
}
|
||||
}
|
||||
|
||||
struct expression_element stack_peek(struct stack *s)
|
||||
{
|
||||
return s->data;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
union element_type
|
||||
{
|
||||
int operand;
|
||||
char operator;
|
||||
};
|
||||
|
||||
enum element_tag {
|
||||
TOKEN_OPERAND,
|
||||
TOKEN_OPERATOR
|
||||
};
|
||||
|
||||
struct expression_element {
|
||||
enum element_tag tag;
|
||||
union element_type data;
|
||||
};
|
||||
|
||||
struct stack {
|
||||
struct expression_element data;
|
||||
struct stack *next;
|
||||
};
|
||||
|
||||
/* Push a new element to the stack and returns the new top */
|
||||
struct stack *stack_push(struct stack *s, struct expression_element element);
|
||||
|
||||
/* Pop from the stack and returns its new top */
|
||||
struct stack *stack_pop(struct stack *s);
|
||||
|
||||
/* Empty and frees the whole stack */
|
||||
void stack_destroy(struct stack **s);
|
||||
|
||||
/* Returns the current top of the stack */
|
||||
struct expression_element stack_peek(struct stack *s);
|
||||
|
||||
|
||||
#endif /* ! STACK_H */
|
||||
Reference in New Issue
Block a user