Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct tacka{
- int x;
- int y;
- int z;
- } TIP;
- typedef struct cvor_st
- {
- TIP inf;
- struct cvor_st *sledeci;
- } SCVOR;
- void ispisi_stek(SCVOR *vrh)
- {
- if (vrh != NULL)
- {
- printf("element: (%i,%i,%i)\n", vrh->inf.x,vrh->inf.y,vrh->inf.z);
- ispisi_stek(vrh->sledeci);
- }
- }
- void obrisi_stek(SCVOR *vrh)
- {
- if (vrh!= NULL)
- {
- printf("brisemo element: (%i,%i,%i)\n", vrh->inf.x,vrh->inf.y,vrh->inf.z);
- obrisi_stek(vrh->sledeci);
- free(vrh);
- }
- }
- void push(TIP inf, SCVOR **vrh)
- {
- SCVOR *novi;
- novi = (SCVOR *)malloc(sizeof(SCVOR));
- novi->inf = inf;
- novi->sledeci = *vrh;
- *vrh = novi;
- }
- TIP pop(SCVOR **vrh)
- {
- SCVOR *tmp;
- TIP pod,x;
- x.x=-1; x.y=-1; x.z=-1;
- if (*vrh == NULL)
- return x;
- /* pokupimo informaciju sa vrha steka */
- pod = (*vrh)->inf;
- /* zapamtimo element sa vrha steka da bismo
- ga obrisali nakon prevezivanja */
- tmp = *vrh;
- /* prevezemo (preskocimo) element sa vrha */
- *vrh = tmp->sledeci;
- /* obrisemo element sa vrha */
- free(tmp);
- return pod;
- }
- TIP top(SCVOR *vrh)
- {
- TIP x;
- x.x=-1; x.y=-1; x.z=-1;
- if (vrh == NULL)
- return x;
- return vrh->inf;
- }
- int main(void)
- {
- /* vrh steka je lokalna promenljiva */
- SCVOR *vrh;
- TIP i;
- int odg;
- /* inicijalizacija praznog steka*/
- vrh = NULL;
- do {
- printf("\n Opcije:\n");
- printf("\t1. Smestanje na stek - push.\n");
- printf("\t2. Skidanje sa steka - pop.\n");
- printf("\t3. Pristup prvom elementu - top.\n");
- printf("\t4. Prikaz sadrzaja celog steka.\n");
- printf("\t5. Kraj.\n");
- printf("\n\t>> ");
- scanf("%d", &odg);
- switch( odg ) {
- case 1: printf("Unesite x: ");
- scanf("%d", &i.x);
- printf("Unesite y: ");
- scanf("%d", &i.y);
- printf("Unesite z: ");
- scanf("%d", &i.z);
- push(i, &vrh);
- break;
- case 2: i = pop(&vrh); /* pokupimo jedan element sa steka */
- printf("Skinuli sa steka: (%i,%i,%i)\n", i.x,i.y,i.z);
- break;
- case 3: printf("Na vrhu steka je (%i,%i,%i)\n", top(vrh).x,top(vrh).y,top(vrh).z); /* pogledamo sta je na vrhu steka */
- break;
- case 4: ispisi_stek(vrh); /* ispisemo sadrzaj stabla*/
- break;
- }
- } while ( odg!=5 );
- /* obrisemo stek */
- obrisi_stek(vrh);
- vrh = NULL;
- return 0;
- }
Add Comment
Please, Sign In to add comment