init
This commit is contained in:
@@ -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