This repository has been archived by the owner on Jul 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathastack.c
151 lines (142 loc) · 3.23 KB
/
astack.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* @Project: IFJ14
-----------------------------------------------------
@Author: Marek Bielik [email protected]
@Author: Filip Gulan [email protected]
@Author: Filip Ježovica [email protected]
@Author: Luboš Matuška [email protected]
@Author: Eduard Rybár [email protected]
-----------------------------------------------------
*/
/*
*Nainkludovanie hackoveho suboru
*/
#include "astack.h"
#include "garbage.h"
/*
*Premenne definicie
*/
astack aS; //zasobnik
/*
*Funkcia na inicializaciu zasobnika
*Parameter: Zasobnik
*/
void astack_init(astack *aS)
{
aS->First = NULL;
aS->Second = NULL;
}
/*
*Funkcia ktora ulozi na zasobnik hodnotu
*Parameter: Zasobnik a vkladana adresa
*/
void myaPush(astack *aS , void *adresa)
{
if(aS->First == NULL) //ak sa pushuje prvy prvok
{
astack_element pomocna = mymalloc(sizeof(struct astack_elementS));
pomocna->adresa = adresa;
pomocna->ptr_next = aS->First;
aS->First = pomocna;
if(aS->First != NULL)
{
aS->Second = aS->First->ptr_next; //tuto nasmerueme este aj Second
}
}
else //ak sa pushuje nty prvok
{
astack_element pomocna = mymalloc(sizeof(struct astack_elementS));
pomocna->adresa = adresa;
pomocna->ptr_next = aS->First;
aS->First = pomocna;
aS->Second = aS->First->ptr_next; //tuto nasmerueme este aj Second
}
}
/*
*Funkcia na popnutie vrcholu zasobniku
*Parameter: Zasobnik
*/
void myaPop(astack *aS)
{
if((aS->First != NULL) && (aS->Second != NULL)) //ak je tam aj First aj Second polozka
{
astack_element pomocna = aS->First;
aS->First = aS->First->ptr_next;
aS->Second = aS->First->ptr_next;
myfree(pomocna);
}
else if((aS->First != NULL) && (aS->Second == NULL)) //iba first polozka
{
astack_element pomocna = aS->First;
aS->First = aS->First->ptr_next;
myfree(pomocna);
}
}
/*
*Funkcia vrati adresu, ktora sa nachadza na vrchole zasobniku
*Parameter: Zasobnik
*Vracia: void adresu, ktora sa nachadza na zasobniku na tope
*/
void *myaTop(astack *aS)
{
if(aS->First != NULL)
{
return aS->First->adresa;
}
return NULL;
}
/*
*Funkcia vrati adresu, ktora sa nachadza tesne pod vrcholom zasobniku
*Parameter: Zasobnik
*Vracia: void adresu, ktora sa nachadza na zasobniku na druhom mieste
*/
void *myaSecTop(astack *aS)
{
if(aS->Second != NULL)
{
return aS->Second->adresa;
}
return NULL;
}
/*
*Zrusi cely Zasobnik
*Parameter: Zasobnik
*/
void destroyaStack(astack *aS)
{
astack_element pomocna;
while(aS->First != NULL)
{
pomocna = aS->First;
aS->First = aS->First->ptr_next;
myfree(pomocna);
}
aS->Second = NULL;
}
/*
int main()
{
int *a = malloc(sizeof(int));
*a = 5;
int *b = malloc(sizeof(int));
*b = 10;
int *c = malloc(sizeof(int));
*c = 15;
int *d = malloc(sizeof(int));
*d = 20;
astack_init(&aS);
myaPush(&aS, a);
printf("%d\n", (*(int*)(myaTop(&aS)))); //nejak takto ked chceme dostat hodnotu zo zasobnikovej adresy!!!!!!!
myaPush(&aS, b);
printf("%d\n", (*(int*)(myaSecTop(&aS))));
myaPush(&aS, c);
printf("%d\n", (*(int*)(myaSecTop(&aS))));
myaPush(&aS, d);
printf("%d\n", (*(int*)(myaSecTop(&aS))));
myaPop(&aS);
printf("%d\n", (*(int*)(myaTop(&aS))));
myaPop(&aS);
myaPop(&aS);
myaPop(&aS);
destroyaStack(&aS);
return 0;
}*/