Skip to content

Commit

Permalink
feat: stack and queue
Browse files Browse the repository at this point in the history
  • Loading branch information
horpeazy committed Oct 7, 2022
1 parent fe4571b commit 4373b14
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 39 deletions.
16 changes: 16 additions & 0 deletions bytecodes/47.m
Original file line number Diff line number Diff line change
@@ -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
55 changes: 26 additions & 29 deletions functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
int add_node(stack_t **head, int value)
{
stack_t *node;
stack_t *node, *temp;

if (!head)
return (-1);
Expand All @@ -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);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions get_operation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};

Expand Down
5 changes: 4 additions & 1 deletion misc_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
Binary file modified monty
Binary file not shown.
5 changes: 4 additions & 1 deletion monty.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <unistd.h>
#include <string.h>

extern int mode;
/**
* struct stack_s - doubly linked list representation of a stack (or queue)
* @n: integer
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 2 additions & 7 deletions more_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -98,6 +99,7 @@ void pop(stack_t **stack, unsigned int line_number)
}

node = *stack;

*stack = (*stack)->next;
free(node);
}
23 changes: 22 additions & 1 deletion rotate.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "monty.h"

int mode = 0;

/**
* rotl - rotates the stack
* @stack: pointer to stack
Expand Down Expand Up @@ -39,11 +41,30 @@ void rotr(stack_t **stack, unsigned int line_number)

(void) line_number;
temp = *stack;

while (temp->next)
temp = temp->next;
temp->prev->next = NULL;
temp->next = *stack;
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;
}

0 comments on commit 4373b14

Please sign in to comment.