-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks1.c
88 lines (74 loc) · 1.67 KB
/
stacks1.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
#include "monty.h"
/**
* addnodeTos - Adds a node to the stack.
*
* @new_node: Pointer to the new node.
*
* @ln: Interger representing the line number of of the opcode.
*/
void addnodeTos(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;
head = *new_node;
head->next = temp;
temp->prev = head;
}
/**
* pall - This function prints the elements of the stack
*
* @stack: Pointer to a pointer pointing to top node of the stack.
*
* @line_number: line number of the opcode.
*/
void pall(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
(void) line_number;
if (stack == NULL)
exit(EXIT_FAILURE);
temp = *stack;
while (temp != NULL)
{
printf("%d\n", temp->n);
temp = temp->next;
}
}
/**
* popTop - this function will remove the top element of a stack
*
* @stack: Pointer to a pointer pointing to top node of the stack.
*
* @line_number: Interger representing the line number of of the opcode.
*/
void popTop(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
if (stack == NULL || *stack == NULL)
AdvErrors(7, line_number);
temp = *stack;
*stack = temp->next;
if (*stack != NULL)
(*stack)->prev = NULL;
free(temp);
}
/**
* printTop - Prints the top nodes of the stack
*
* @stack: Pointer to a pointer pointing to top node of the stack.
*
* @line_number: Interger representing the line number of of the opcode.
*/
void printTop(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || *stack == NULL)
AdvErrors(6, line_number);
printf("%d\n", (*stack)->n);
}