-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_pos_elem.c
54 lines (50 loc) · 1.27 KB
/
max_pos_elem.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
#include "lib/tools.h"
#include "lib/stack.h"
bool removeMaxPosElem(STACK **head)
{
STACK *current = *head,
*previous = NULL,
*maxCurrent = current,
*maxPrevious = NULL;
while (current = current -> next)
{
if (current -> value > 0)
if (maxCurrent -> value <= 0 || current -> value > maxCurrent -> value)
{
maxCurrent = current;
maxPrevious = previous;
}
previous = current;
};
if (maxCurrent -> value > 0)
{
if (maxPrevious)
maxPrevious -> next = maxCurrent -> next;
else
*head = maxCurrent -> next;
free(maxCurrent);
return true;
}
return false;
}
int main(void)
{
STACK *myStack;
FILE *output = openFile("output.txt", "w");
puts("Fill stack:");
myStack = create();
printf("\nOriginal stack: ");
printToFile(myStack, stdout);
if (removeMaxPosElem(&myStack))
{
printf("After deleting maximal positive element: ");
printToFile(myStack, stdout);
printToFile(myStack, output);
}
else
puts("There are no positive elements! Nothing to delete.");
fclose(output);
clear(myStack);
system("pause");
return 0;
}