diff --git a/bytecodes/47.m b/bytecodes/47.m new file mode 100644 index 0000000..b1f1d86 --- /dev/null +++ b/bytecodes/47.m @@ -0,0 +1,16 @@ +queue +push 1 +push 2 +push 3 +pall +stack +push 4 +push 5 +push 6 +pall +add +pall +queue +push 11111 +add +pall diff --git a/functions.c b/functions.c index a24d96b..e266a53 100644 --- a/functions.c +++ b/functions.c @@ -8,7 +8,7 @@ */ int add_node(stack_t **head, int value) { - stack_t *node; + stack_t *node, *temp; if (!head) return (-1); @@ -21,36 +21,33 @@ int add_node(stack_t **head, int value) } node->n = value; - node->prev = NULL; - node->next = *head; - if (*head != NULL) - (*head)->prev = node; - *head = node; - return (1); -} - -/** - * delete_node - removes the top of the stack - * @head: pointer to the head node - * Return: 0 on success, -1 on failure - */ -int delete_node(stack_t **head) -{ - stack_t *node; - - if (!head) - return (-1); - - if (*head == NULL) - return (0); - - node = *head; - *head = (*head)->next; - (*head)->prev = NULL; - free(node); + if (mode == 0) + { + node->next = *head; + node->prev = NULL; + if (*head != NULL) + (*head)->prev = node; + *head = node; + } + else + { + temp = *head; + if (!temp) + { + node->prev = NULL; + node->next = NULL; + *head = node; + return (1); + } + while (temp->next) + temp = temp->next; + node->next = NULL; + node->prev = temp; + temp->next = node; + } - return (0); + return (1); } /** diff --git a/get_operation.c b/get_operation.c index 2601e27..f61b609 100644 --- a/get_operation.c +++ b/get_operation.c @@ -23,6 +23,8 @@ void (*get_op(char *name))(stack_t **, unsigned int) {"pstr", pstr}, {"rotl", rotl}, {"rotr", rotr}, + {"stack", stack}, + {"queue", queue}, {NULL, NULL} }; diff --git a/misc_operations.c b/misc_operations.c index 4e95974..a18947e 100644 --- a/misc_operations.c +++ b/misc_operations.c @@ -57,6 +57,8 @@ void _mod(stack_t **stack, unsigned int line_number) */ void pchar(stack_t **stack, unsigned int line_number) { + stack_t *node; + if (!stack || !*stack) { fprintf(stderr, "L%u: can't pchar, stack empty\n", line_number); @@ -69,7 +71,8 @@ void pchar(stack_t **stack, unsigned int line_number) exit(EXIT_FAILURE); } - printf("%c\n", (*stack)->n); + node = *stack; + printf("%c\n", node->n); } /** diff --git a/monty b/monty index ab58d7c..018e7e3 100755 Binary files a/monty and b/monty differ diff --git a/monty.h b/monty.h index a4b62c2..6d9c539 100644 --- a/monty.h +++ b/monty.h @@ -11,6 +11,7 @@ #include #include +extern int mode; /** * struct stack_s - doubly linked list representation of a stack (or queue) * @n: integer @@ -54,7 +55,7 @@ void pint(stack_t **stack, unsigned int line_number); void pop(stack_t **stack, unsigned int line_number); void swap(stack_t **stack, unsigned int line_number); void _add(stack_t **stack, unsigned int line_number); -void nop(stack_t **stack, unsigned int line_number); +void nop(stack_t **stack __attribute__((unused)), unsigned int line_number __attribute__((unused))); void _sub(stack_t **stack, unsigned int line_number); void _div(stack_t **stack, unsigned int line_number); void _mul(stack_t **stack, unsigned int line_number); @@ -63,6 +64,8 @@ void pchar(stack_t **stack, unsigned int line_number); void pstr(stack_t **stack, unsigned int line_number); void rotl(stack_t **stack, unsigned int line_number); void rotr(stack_t **stack, unsigned int line_number); +void stack(stack_t **stack __attribute__((unused)), unsigned int line_number __attribute__((unused))); +void queue(stack_t **stack __attribute__((unused)), unsigned int line_number __attribute__((unused))); /* Miscellaneous */ void (*get_op(char *name))(stack_t **stack, unsigned int line_number); diff --git a/more_operations.c b/more_operations.c index 7c8b593..a0b03a4 100644 --- a/more_operations.c +++ b/more_operations.c @@ -50,14 +50,9 @@ void _add(stack_t **stack, unsigned int line_number) * @stack: stack * @line_number: line number */ -void nop(stack_t **stack, unsigned int line_number) +void nop(stack_t **stack __attribute__((unused)), unsigned int line_number __attribute__((unused))) { - int i = 0; - stack_t *node = *stack; - - (void) line_number; - if (i == 1) - printf("%d\n", node->n); + return; } /** diff --git a/operations.c b/operations.c index c28495c..35d1717 100644 --- a/operations.c +++ b/operations.c @@ -59,6 +59,7 @@ void pall(stack_t **stack, unsigned int line_number) (void) line_number; node = *stack; + while (node) { printf("%d\n", node->n); @@ -98,6 +99,7 @@ void pop(stack_t **stack, unsigned int line_number) } node = *stack; + *stack = (*stack)->next; free(node); } diff --git a/rotate.c b/rotate.c index 547bed1..354f879 100644 --- a/rotate.c +++ b/rotate.c @@ -1,5 +1,7 @@ #include "monty.h" +int mode = 0; + /** * rotl - rotates the stack * @stack: pointer to stack @@ -39,7 +41,6 @@ void rotr(stack_t **stack, unsigned int line_number) (void) line_number; temp = *stack; - while (temp->next) temp = temp->next; temp->prev->next = NULL; @@ -47,3 +48,23 @@ void rotr(stack_t **stack, unsigned int line_number) temp->prev = NULL; *stack = temp; } + +/** + * stack - sets mode to stack + * @stack: pointer to stack + * @line_number: line number + */ +void stack(stack_t **stack __attribute__((unused)), unsigned int line_number __attribute__((unused))) +{ + mode = 0; +} + +/** + * queue - sets mode to stack + * @stack: pointer to stack + * @line_number: line number + */ +void queue(stack_t **stack __attribute__((unused)), unsigned int line_number __attribute__((unused))) +{ + mode = 1; +}