Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- struct stos
- {
- int liczba;
- struct stos *n;
- };
- void init(struct stos *(*head));
- void destroy(struct stos *(*head));
- struct stos* push (struct stos *head, int x);
- struct stos* pop (struct stos *head);
- int top (struct stos *head);
- int empty (struct stos *head);
- int full (struct stos *head);
- int main()
- {
- struct stos *HEAD;
- init(&HEAD);
- int ilosc, liczba;
- srand(time(NULL));
- ilosc=rand()%10;
- for(int i=0; i<ilosc; i++)
- {
- liczba=rand()%10;
- HEAD=push(HEAD, liczba);
- }
- while(empty(HEAD))
- {
- printf("Liczba z wierzchu: %d\n", top(HEAD));
- HEAD=pop(HEAD);
- }
- destroy(&HEAD);
- return 0;
- }
- void init(struct stos *(*head))
- {
- *head=NULL;
- }
- void destroy(struct stos *(*head))
- {
- if((*head)!=NULL)
- {
- struct stos *tmp;
- while(*head)
- {
- tmp=(*head)->n;
- free(*head);
- *head=tmp;
- }
- }
- }
- struct stos* push (struct stos *head, int x)
- {
- struct stos *tmp;
- tmp=malloc(sizeof(struct stos));
- tmp->liczba=x;
- tmp->n=head;
- return tmp;
- }
- struct stos* pop (struct stos *head)
- {
- struct stos *tmp;
- tmp=head->n;
- free(head);
- return tmp;
- }
- int top (struct stos *head)
- {
- if (head) return head->liczba;
- else return NULL;
- }
- int empty (struct stos *head)
- {
- if(head==NULL) return 0; //stos pusty
- else return 1; //stos nie jest pusty
- }
- int full (struct stos *head)
- {
- return 0; //lista nie jest pe³na
- }
Advertisement
Add Comment
Please, Sign In to add comment