-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_stack_func.c
123 lines (103 loc) · 1.95 KB
/
basic_stack_func.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "monty.h"
int data;
/**
* pushStack - Free the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void pushStack(stack_t **stack, unsigned int line_number)
{
stack_t *new = malloc(sizeof(stack_t));
(void)line_number;
if (new == NULL)
return;
new->n = data;
new->prev = NULL;
new->next = NULL;
if (*stack != NULL)
{
new->next = *stack;
(*stack)->prev = new;
}
*stack = new;
}
/**
* printStack - Free the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void printStack(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
if (*stack == NULL)
return;
(void)line_number;
while (temp != NULL)
{
printf("%d\n", temp->n);
temp = temp->next;
}
}
/**
* topStack - Free the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void topStack(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
if (*stack == NULL)
{
fprintf(stderr, "L%u: can't pint, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
printf("%d\n", temp->n);
}
/**
* popStack - Free the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void popStack(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
if (*stack == NULL)
{
fprintf(stderr, "L%u: can't pop an empty stack\n", line_number);
exit(EXIT_FAILURE);
}
temp = (*stack)->next;
free(*stack);
*stack = temp;
if (*stack != NULL)
temp->prev = NULL;
}
/**
* swapStack - Free the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void swapStack(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
int top;
if (*stack == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%u: can't swap, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
temp = *stack;
top = temp->n;
(*stack)->n = temp->next->n;
temp->next->n = top;
}