/ Published in: C
stiva
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <stdio.h> #include <malloc.h> #include <stdlib.h> //implementare stiva struct numar{ int valoare; struct numar *next; }; struct numar *prim = NULL; int nr_elem; struct numar *creare(int val){ if(NULL == ptr){ return NULL; } ptr->valoare = val; ptr->next = NULL; prim = ptr; nr_elem = 1; return ptr; } //functie afisare stiva void afisare_coada(){ struct numar *ptr = prim; while(ptr != NULL){ ptr = ptr->next; } } //functie adaugare in stiva push struct numar *push(val){ ptr->valoare = val; ptr->next = NULL; if(NULL == prim){ creare(val); }else{ ptr->next = prim; prim = ptr; nr_elem++; } afisare_coada(); return ptr; } //functie pop int pop(){ struct numar *pop = prim; struct numar *temp = NULL; if(pop != NULL){ prim = prim->next; } pop = NULL; afisare_coada(); return 1; } //functie empty int empty(){ struct numar *ptr = prim; while(ptr != NULL){ pop(); afisare_coada(); } return 1; } int main(){ int i; for (i = 5; i>0; i--){ push(i); } pop(); }
URL: http://ssm-iscri.ro