-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·83 lines (71 loc) · 1.35 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "monty.h"
stack_t *head = NULL;
/**
* main - entry point
* @argc: arguments count
* @argv: list of arguments
* Return: always 0
*/
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
open_file(argv[1]);
free_nodes();
return (0);
}
/**
* create_node - Creates a node.
* @n: Number to go inside the node.
* Return: Upon sucess a pointer to the node. Otherwise NULL.
*/
stack_t *create_node(int n)
{
stack_t *node;
node = malloc(sizeof(stack_t));
if (node == NULL)
err(4);
node->next = NULL;
node->prev = NULL;
node->n = n;
return (node);
}
/**
* free_nodes - Frees nodes in the stack.
*/
void free_nodes(void)
{
stack_t *tmp;
if (head == NULL)
return;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
/**
* add_to_queue - Adds a node to the queue.
* @new_node: Pointer to the new node.
* @ln: line number of the opcode.
*/
void add_to_queue(stack_t **new_node, __attribute__((unused))unsigned int ln)
{
stack_t *tmp;
if (new_node == NULL || *new_node == NULL)
exit(EXIT_FAILURE);
if (head == NULL)
{
head = *new_node;
return;
}
tmp = head;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = *new_node;
(*new_node)->prev = tmp;
}