-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.c
109 lines (96 loc) · 1.84 KB
/
node.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
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "monty.h"
int num;
/**
* newnode - creates a new node
* @i: the value to add in the node
* Return: pointer to the new node
*/
stack_t *newnode(int i)
{
stack_t *new;
new = malloc(sizeof(stack_t));
if (new == NULL)
{
dprintf(2, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
new->n = i;
new->prev = NULL;
new->next = NULL;
return (new);
}
/**
* push - adds a node into stack
* @stack: pointer to the top of stack
* @line_number: the number of line a file
* Return: void
*/
void push(stack_t **stack, unsigned int line_number)
{
stack_t *temp = NULL;
(void) line_number;
temp = newnode(num);
temp->next = *stack;
if (*stack != NULL)
{
(*stack)->prev = temp;
}
*stack = temp;
}
/**
* pall - prints all the stack value
* @stack: the pointer to the top of stack
* @line_number: unsigned integer
* Return: void
*/
void pall(stack_t **stack, unsigned int line_number)
{
stack_t *node;
(void) line_number;
node = *stack;
while (node != NULL)
{
dprintf(1, "%d\n", node->n);
node = node->next;
}
}
/**
* pint - prints the value at the top of the stack
* @stack: double pointer to the head of the stack
* @line_number: line number
*/
void pint(stack_t **stack, unsigned int line_number)
{
if (*stack == NULL || stack == NULL)
{
dprintf(2, "L%d: can't pint, stack empty\n", line_number);
clear_stack(stack);
exit(EXIT_FAILURE);
}
else
dprintf(1, "%d\n", (*stack)->n);
}
/**
* pop - removes the top element from the stack
* @stack: pointer to the top of stack
* @line_number: line number
* Return: void
*/
void pop(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
if (!*stack)
{
dprintf(2, "L%u: can't pop an empty stack\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
else
{
temp = (*stack)->next;
free(*stack);
if (temp)
temp->prev = NULL;
*stack = temp;
}
}