-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-node_ops.c
More file actions
99 lines (90 loc) · 1.71 KB
/
0-node_ops.c
File metadata and controls
99 lines (90 loc) · 1.71 KB
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
#include "monty.h"
int vals;
/**
* create_node - function that create node
* @n:data to store
* Return:new_node
*/
stack_t *create_node(int n)
{
stack_t *new_node = malloc(sizeof(stack_t));
if (!new_node)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
new_node->n = n;
new_node->prev = NULL;
new_node->next = NULL;
return (new_node);
}
/**
* push - function that push value onto the stack
* @head:head stack
* @line_num:line number
*/
void push(stack_t **head, unsigned int line_num)
{
stack_t *new_node = NULL;
(void)line_num;
new_node = create_node(vals);
new_node->next = *head;
if (*head != NULL)
(*head)->prev = new_node;
*head = new_node;
}
/**
* pall - function to print all values in the stack
* @head: head stack
* @line_num:line number
*/
void pall(stack_t **head, unsigned int line_num)
{
stack_t *temp = NULL;
(void)line_num;
temp = *head;
while (temp != NULL)
{
fprintf(stdout, "%d\n", temp->n);
temp = temp->next;
}
}
/**
* pint - function that print the top value of the stack
* @head: head stack
* @line_num: line number
*/
void pint (stack_t **head, unsigned int line_num)
{
if (!*head || !head)
{
fprintf(stderr, "L%d: can't pint, stack empty\n", line_num);
close_stack(head);
exit(EXIT_FAILURE);
}
else
fprintf(stdout, "%d\n", (*head)->n);
}
/**
* pop - function that pop a value from the stack
* @head:head stack
* @line_num: line number
*/
void pop(stack_t **head, unsigned int line_num)
{
stack_t *temp;
if (!*head)
{
fprintf(stderr, "L%u: can't pop an empty stack\n", line_num);
free_stack(*head);
exit(EXIT_FAILURE);
}
else
{
temp = (*head)->next;
free(*head);
if (temp)
temp->prev = NULL;
*head = temp;
}
}