-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker.c
67 lines (64 loc) · 873 Bytes
/
linker.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
#include "monty.h"
/**
* print_prev - link list in reverse
* @stack: pointer
* Return: nodes
*/
size_t print_prev(stack_t *stack)
{
size_t rev = 0;
while (stack->next)
{
stack = stack->next;
}
while (stack)
{
stack = stack->prev;
rev++;
}
return (rev);
}
/**
* delete: delete node at index
* @stack: pointer
* @index: unsigned int
* Return: -1
*/
int delete(stack_t **stack, unsigned int index)
{
stack_t *c;
unsigned int ptr = 0;
if (!stack)
return (-1);
c = *stack;
if (index == 0)
{
if (c->next)
{
c->next->prev = NULL;
*stack = c->next;
}
else
*stack = NULL;
free(c);
return (1);
}
while (c)
{
if (ptr == index)
{
if (c->next)
{
c->prev->next = c->next;
c->next->prev = c->prev;
}
else
c->prev->next = NULL;
free(c);
return (1);
}
c = c->next;
ptr++;
}
return (-1);
}