-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal towers program with stack.c
122 lines (89 loc) · 2.29 KB
/
signal towers program with stack.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
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct StackNode
{
int data;
struct StackNode* next;
};
struct StackNode* newNode(int data)
{
struct StackNode* stackNode = (struct StackNode*) malloc(sizeof(struct StackNode));
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}
int isEmpty(struct StackNode *root)
{
return !root;
}
void push(struct StackNode** root, int data)
{
struct StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
}
int pop(struct StackNode** root)
{
if (isEmpty(*root))
return INT_MIN;
struct StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);
return popped;
}
int peek(struct StackNode* root)
{
if (isEmpty(root))
return INT_MIN;
return root->data;
}
int main()
{
struct StackNode* root = NULL;
struct StackNode* temp2 = NULL;
struct StackNode* temp3=NULL;
struct StackNode* temp4=NULL;
int test,kapasite,i=0,j=0,x,y,eleman,gecici1,gecici2,say=0,gecici3,a=0,gecici4;
printf("\nWelcome :)\n\n");
printf("Give the test number "); scanf("%d",&test);
while(i<test){
printf("How many tower to be "); scanf("%d",&kapasite);
while(j<kapasite){
printf("length of towers "); scanf("%d",&eleman);
push(&root,eleman);
push(&temp2,eleman);
j++; }
for(x=0;x<kapasite-1;x++){
gecici1=peek(root);
pop(&root);
for(y=0;y<kapasite-x-1;y++){
gecici2=peek(temp2);
if(gecici1>=gecici2){ say++;}
pop(&temp2);
gecici3=peek(temp2);
push(&temp3,gecici3);
}
while(a<kapasite-x-1){
gecici4=peek(temp3);
push(&temp4,gecici4);
pop(&temp3);
a++;}
a=0;
if(gecici1>=gecici3){say++;}
temp2=temp4;
puts("");
puts("*******************************************");
printf("sign made from tower %d = %d\n",gecici1,say);
puts("*******************************************");
say=0;
}
i++;
}
puts("");
puts("*******************************************");
printf("sign maded from tower %d = 1\n",peek(root));
puts("*******************************************");
return 0;
}