-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm_stack_func.c
More file actions
126 lines (115 loc) · 2.57 KB
/
Copy pathm_stack_func.c
File metadata and controls
126 lines (115 loc) · 2.57 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "monty.h"
/**
* print_stack_t - print a list kind of stack_t
* @h: The head of the list.
*
* Return: The length of the list.
*/
size_t print_stack_t(const stack_t *h)
{
size_t len;
if (h == NULL)
return (0);
for (len = 0; h != NULL; h = h->next, len++)
printf("%d\n", h->n);
return (len);
}
/**
* push_stack_elt - adds an element to the begining of a stack
* implements with list type of stack_t.
* @head: The address of the head pointer.
* @n: The value to assign to the new stack element.
*
* Return: The address of the new element (success), NULL (error).
*/
stack_t *push_stack_elt(stack_t **head, const int n)
{
stack_t *new = NULL;
if (head == NULL)
return (NULL);
new = monty_malloc(sizeof(stack_t));
new->n = n;
new->prev = NULL;
new->next = *head;
if (*head != NULL)
(*head)->prev = new;
*head = new;
return (new);
}
/**
* delete_elt_at_index - deletes a node from a list.
* @head: Address of the head pointer of the list.
* @index: The index of the node to be deleted.
*
* Return: 1 (success), -1 (error).
*/
int delete_elt_at_index(stack_t **head, unsigned int index)
{
size_t idx;
stack_t *next = NULL, *prev = NULL, *self = NULL;
if (head == NULL || *head == NULL)
return (-1);
for (self = *head, idx = 0; self != NULL; self = self->next, idx++)
{
if (idx == index)
{
next = self->next;
prev = self->prev;
if (next != NULL)
next->prev = prev;
if (prev != NULL)
prev->next = next;
if (index == 0)
*head = next;
free(self);
return (1);
}
}
return (-1);
}
/**
* add_elt_end - adds a node at the end of a list type of stack_t.
* @head: The address of the pointer that holdes the head of the list.
* @n: The value to assigne to the new nodes element.
*
* Return: The address of the new node (success), NULL (error).
*/
stack_t *add_elt_end(stack_t **head, const int n)
{
stack_t *new = NULL;
stack_t *cnt = NULL;
if (head == NULL)
return (NULL);
new = init_stack_elt(n, NULL, NULL);
for (cnt = *head; ; cnt = cnt->next)
{
if (cnt == NULL)
{
*head = new;
return (new);
}
if (cnt->next == NULL)
{
cnt->next = new;
new->prev = cnt;
return (new);
}
}
}
/**
* get_elt_at_index - gets a node at given index form a given list.
* @head: The head of the list.
* @index: The index of the node.
*
* Return: The address of the node (success), NULL (error).
*/
stack_t *get_elt_at_index(stack_t *head, unsigned int index)
{
size_t idx;
if (head == NULL)
return (NULL);
for (idx = 0; head != NULL; head = head->next, idx++)
if (idx == index)
return (head);
return (NULL);
}