0% found this document useful (0 votes)
27 views3 pages

Reverse Ömer Faruk Tunç

The document defines functions for creating, manipulating, and printing two stacks (Stack1 and Stack2). The key functions are: - Creative allocates memory for a stack - Push adds an element to a stack - Print displays a stack - Reverse transfers all elements from one stack to the other - IsFull checks if a stack is full and increases its size if needed The main function demonstrates using these functions by adding elements to each stack and then reversing the contents of Stack1 and Stack2.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Reverse Ömer Faruk Tunç

The document defines functions for creating, manipulating, and printing two stacks (Stack1 and Stack2). The key functions are: - Creative allocates memory for a stack - Push adds an element to a stack - Print displays a stack - Reverse transfers all elements from one stack to the other - IsFull checks if a stack is full and increases its size if needed The main function demonstrates using these functions by adding elements to each stack and then reversing the contents of Stack1 and Stack2.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ömer Faruk TUNÇ

2107231022
1. #include<stdio.h>
2. #include<stdlib.h>
3.
4. struct Array
5. {
6. int head;
7. int SIZE;
8. int *d;
9.
10. }Stack1,Stack2;
11.
12. int creative(int olcu){
13. int stack=calloc(olcu,sizeof(int));
14. return stack;
15. }
16. void push(int *Array,int temp){
17. int Control= isFull(Array);
18. if(Control==0){
19. if (Array==Stack1.d)
20. {
21. Stack1.head++;
22. Stack1.d[Stack1.head]=temp;
23. printf("Stack'1 e %d eklendi\n",temp);
24. }
25. else
26. {
27. Stack2.head++;
28. Stack2.d[Stack2.head]=temp;
29. printf("Stack'2 e %d eklendi\n",temp);
30. }}}
31. int pop(int *Array){
32. if (Array==Stack1.head)
33. {
34. printf("%d, Stack1'den cikartildi",Stack1.d[Stack1.head]);
35. Stack1.d[Stack1.head]=NULL;
36. }
37. else
38. {
39. printf("%d, Stack2'den cikartildi",Stack2.d[Stack2.head]);
40. Stack2.d[Stack2.head]=NULL;
41. }
42.
43.
44. }
45.
46.
47. void print(int *Array){
48. if (Array==Stack1.d)
49. {
50. int i;
51. printf("Stack 1: \n");
52. for(i=(Stack1.SIZE-1);i>=0;i--){
53. printf(" | %2d |\n",Array[i]);
54. }
55. printf(" --------\n");
56. }
57. else if (Array==Stack2.d)
58. {
59. int i;
60. printf("Stack 2: \n");
61. for(i=(Stack2.SIZE-1);i>=0;i--){
62. printf(" | %2d |\n",Array[i]);
63. }
64. printf(" --------\n");
65. }
66. }
67. int delcontrole(int *Array1,int *Array2){
68. if (Array1==Stack1.d && Array2==Stack2.d)
69. {
70. return 0;
71. }
72. else if (Array2==Stack1.d && Array1==Stack2.d)
73. {
74. return 1;
75. }
76.

Reverse Fonksiyonu
Ömer Faruk TUNÇ
2107231022
77.
78. }
79. int push_increase(int *Leyla){
80. if(Leyla==Stack1.d){
81. Leyla=(int*)malloc((Stack1.SIZE + Stack1.SIZE)*sizeof(int));
82. return Leyla;}
83. else if(Leyla==Stack2.d){
84. Leyla=(int*)malloc((Stack2.SIZE + Stack2.SIZE)*sizeof(int));
85. return Leyla;}
86.
87. }
88. void reverse(int *Array1,int *Array2){
89. int Control=delcontrole(Array1,Array2);
90. switch (Control)
91. {
92. case 0:
93. if(Stack2.head<Stack1.SIZE && Stack2.head==Stack2.SIZE-1){
94. Stack2.SIZE=Stack1.head+Stack2.SIZE+1;
95. }
96. for (int i = Stack1.head; i >=0; i--)
97. {
98.
99. if(Stack1.d[i]!=0){
100. Stack2.d[Stack2.head+1]=Stack1.d[i];
101. Stack1.d[i]=0;
102. Stack2.head++;}
103. }
104.
105. break;
106. case 1:
107. if(Stack1.head<Stack2.SIZE && Stack1.head==Stack1.SIZE-1){
108. Stack1.SIZE=Stack2.head+Stack1.SIZE+1;
109. }
110. for (int i = Stack2.head; i >=0; i--)
111. {
112.
113. if(Stack2.d[i]!=0){
114. Stack1.d[Stack1.head+1]=Stack2.d[i];
115. Stack2.d[i]=0;
116. Stack1.head++;}
117. }
118.
119. break;
120.
121. default:
122. break;
123. }}
124. int isFull(int *Array){
125. if (Array==Stack1.d)
126. {
127. if(Stack1.head == Stack1.SIZE - 1){
128. printf("Hata.. Yigin Tasmasi\n");
129. printf("yuzde 100 arttirildi\n");
130. Stack1.SIZE+=Stack1.SIZE/2;
131. push_increase(&Array);
132. return 1;
133. }
134. else
135. {
136. return 0;
137. }
138. }
139. else if(Array==Stack2.d)
140. {
141. if(Stack2.head == Stack2.SIZE - 1){
142. printf("Hata.. Yigin Tasmasi\n");
143. printf("yuzde 100 arttirildi\n");
144. Stack2.SIZE+=Stack2.SIZE/2;
145. push_increase(&Array);
146. return 1;
147. }
148. else
149. {
150. return 0;
151. }
152. }

Reverse Fonksiyonu
Ömer Faruk TUNÇ
2107231022
153.
154.
155. }
156. void main(){
157. Stack1.head=-1;
158. Stack2.head=-1;
159. //----------------------------
160. Stack1.SIZE= 8; //
161. Stack2.SIZE= 2; //
162. //----------------------------
163. Stack1.d=creative(Stack1.SIZE);
164. Stack2.d=creative(Stack2.SIZE);
165.
166. //----------------------------
167. push(Stack2.d,8);
168. push(Stack2.d,4);
169.
170. print(Stack2.d);
171.
172.
173. push(Stack1.d,9);
174. push(Stack1.d,10);
175. print(Stack1.d);
176. reverse(Stack1.d,Stack2.d);
177. print(Stack2.d);
178.
179. }
180.

Fonskiyonların tanımı:
Creative: Stackleri ayarlamak istediğimiz boyuta ayarlar →creative(boyut)
Push: Stacklere eklemek istediğimiz değeri eklememizi sağlar →push(Yığınadı,sayı)
Pop: Stackteki peek değeri çıkartır→ pop(Yığınadı)
Print:İstenilen stack’i yazdırmayı sağlar → print(Yığınadı)
Delcontrole: reverse yapılacak yığın kontrolünü sağlar eğer reverse edilmeye çalışılan Stackler aynı
ise atlar ve reverse yapmaz.
Push_increase: Bellek arttırma fonksiyonudur.
Reverse: Yığınlar arasında aktarımı sağlar
isFull: Yığının dolu olup olmadığı kontrolünü sağlar dolu olması durumunda push_increase
fonksiyonuna başvurur.

Boyutlar için değiştirilmesi gereken alanlar 160 & 161. Satırlardaki ölçülerdir.

Reverse Fonksiyonu

You might also like