Skip to content

Commit 92fc79e

Browse files
authored
Add files via upload
1 parent 9f0fb89 commit 92fc79e

File tree

1 file changed

+19
-75
lines changed

1 file changed

+19
-75
lines changed

main.c

Lines changed: 19 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,28 @@
11
#include <stdio.h>
2-
#include <stdlib.h>
3-
#define MAX 100
42

5-
typedef struct {
6-
int top;
7-
int Stack[MAX];
8-
}stack;
9-
10-
void initStack(stack *s){
11-
s->top = 0;
12-
}
13-
14-
int isFull (stack *s) {
15-
if (s->top == MAX)
16-
return 1;
17-
else
18-
return 0;
19-
}
20-
21-
int isEmpty (stack *s) {
22-
if (s->top == 0)
23-
return 1;
24-
else
25-
return 0;
26-
}
27-
28-
int pop (stack *s, int *x) {
29-
if (isEmpty(s))
30-
return 0;
31-
else {
32-
*x = s->Stack[--s->top];
33-
return 1;
34-
}
35-
}
36-
37-
int push (stack *s, int x) {
38-
if (isFull(s))
39-
return 0;
40-
else {
41-
s->Stack[s->top++] = x;
42-
return 1;
43-
}
44-
}
45-
46-
int peek (stack *s, int *x) {
47-
if (isEmpty(s))
48-
return 0;
49-
else {
50-
int peaky = s->top - 1;
51-
*x = s->Stack[peaky];
52-
return 1;
53-
}
54-
}
55-
56-
void printStack(stack *s) {
57-
if (isEmpty(s))
58-
return;
59-
else {
60-
int i = s->top-1;
61-
while( i >= 0) {
62-
printf("%d ", s->Stack[i]);
63-
i--;
64-
} printf("\n");
3+
void binarySearch(int wanted, int array[], int n)
4+
{
5+
int first, last, mid;
6+
first = 0;
7+
last = n;
8+
mid = (first + last)/2;
9+
while ( (last > first) && (array[mid] != wanted) )
10+
{
11+
if (array[mid] < wanted)
12+
first = mid + 1;
13+
else
14+
last = mid - 1;
15+
16+
mid = (last + first) / 2;
6517
}
18+
if (array[mid] == wanted)
19+
printf("Aranan kan bulundu indis : %d \n", mid);
6620
}
6721

6822
int main(int argc, char **argv)
6923
{
70-
stack s;
71-
int x;
72-
initStack(&s);
73-
push(&s, 5);
74-
push(&s, 7);
75-
push(&s, 15);
76-
printStack(&s);
77-
pop(&s, &x);
78-
printf("poped value = %d \n", x);
79-
printStack(&s);
80-
peek(&s, &x);
81-
printf("peeked value = %d\n", x);
82-
printStack(&s);
24+
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
25+
int n = 10;
26+
binarySearch(8, array, n);
8327
return 0;
8428
}

0 commit comments

Comments
 (0)