-
Notifications
You must be signed in to change notification settings - Fork 2
/
NGE.c
89 lines (87 loc) · 1.62 KB
/
NGE.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
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define STACKSIZE 100
struct stack
{
int top;
int arr[STACKSIZE];
};
void push(struct stack *ps, int x)
{
if (ps->top == STACKSIZE - 1)
{
printf("overflow");
return;
}
else
{
ps->arr[++ps->top] = x;
}
}
int pop(struct stack *ps)
{
int temp;
if (ps->top == -1)
{
printf("underflow");
return -1;
}
else
{
temp = ps->arr[ps->top];
ps->top--;
return temp;
}
}
// bool isempty(struct stack *ps)
// {
// return (ps->top == -1) ? true : false;
// }
int isempty(struct stack *ps)
{
return (ps->top == -1) ? 1 : 0;
}
void printNGE(int arr[], int n)
{
struct stack s;
int element, next;
s.top = -1;
push(&s, arr[0]);
for (int i = 1; i < n; i++)
{
next = arr[i];
if (isempty(&s)!= 1)
{
element = pop(&s);
while (element < next)
{
printf("the next greater element of %d is %d\n", element, next);
if (isempty (&s)!= 1)
{
element = pop(&s);
}
else
break;
}
if (element > next)
{
push(&s, element);
}
}
push(&s, next);
}
while (isempty(&s) == 0)
{
element=pop(&s);
next=-1;
printf("the next greater element of %d is %d\n",element,next);
}
}
int main()
{
int arr[]={4,5,2,25};
int n=sizeof(arr)/sizeof(arr[0]);
printNGE(arr,n);
return 0;
}