-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_monty.c
96 lines (80 loc) · 1.35 KB
/
main_monty.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
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "monty.h"
stack_t *head = NULL;
/**
* main - This is the 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);
}
openew_file(argv[1]);
freeNodes();
return (0);
}
/**
* createNode - This function will create a new node
*
* @n: Number to go inside the node.
*
* Return: Upon sucess a pointer to the node. Otherwise NULL.
*
*/
stack_t *createNode(int n)
{
stack_t *node;
node = malloc(sizeof(stack_t));
if (node == NULL)
error_mesg(4);
node->next = NULL;
node->prev = NULL;
node->n = n;
return (node);
}
/**
* freeNodes - Frees nodes in the stack
*
*/
void freeNodes(void)
{
stack_t *temp;
if (head == NULL)
return;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
/**
* adddToqueue - Adds a new node to the queue.
*
* @new_node: Pointer to the new node.
*
* @ln: line number of the opcode.
*/
void adddToqueue(stack_t **new_node, __attribute__((unused))unsigned int ln)
{
stack_t *temp;
if (new_node == NULL || *new_node == NULL)
exit(EXIT_FAILURE);
if (head == NULL)
{
head = *new_node;
return;
}
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = *new_node;
(*new_node)->prev = temp;
}