Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------ MAIN.C ------------------------------ //
- #include <stdio.h>
- #include "stack.h"
- int main()
- {
- struct Stack s1;
- struct Stack s2;
- int i = 0;
- init(&s1); // inicjalizacja dwoch stosow
- init(&s2);
- push(&s1, 11);
- push(&s1, 12);
- push(&s1, 13);
- push(&s2, 21);
- push(&s2, 22);
- push(&s1, 15);
- push(&s1, 16);
- push(&s1, 16);
- push(&s2, 24);
- push(&s2, 25);
- push(&s1, 17);
- push(&s1, 18);
- push(&s1, 19);
- push(&s2, 27);
- push(&s2, 28);
- for (i = 0; !isempty(&s1) && !isempty(&s2); i++)
- {
- printf("%d %d\n", pop(&s1), pop(&s2));
- }
- destroy(&s1);
- destroy(&s2);
- return 0;
- }
- // ------------------------------ STACK.H ------------------------------ //
- #include <stdbool.h>
- struct Stack{
- int top; // gora stosu
- int *dane;
- int size;
- };
- void init (struct Stack* s);
- void destroy (struct Stack* s);
- void push (struct Stack* s, int element);
- int pop (struct Stack* s);
- void clear (struct Stack* s);
- int isempty (struct Stack* s);
- // ------------------------------ STACK.C ------------------------------ //
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <malloc.h>
- #include "stack.h"
- void init(struct Stack* s){
- s->top = 0;
- s->size = 1;
- s->dane = (int*)malloc(sizeof(int) * s->size);
- }
- void destroy(struct Stack* s){
- free(s->dane);
- }
- void clear(struct Stack* s){
- s->top = 0;
- }
- void push(struct Stack* s, int element){
- if (s->size == s->top)
- s->dane = (int*)realloc(s->dane, sizeof(int) * (s->size*= 2));
- assert(s->top < s->size);
- s->dane[s->top++] = element;
- }
- int pop(struct Stack* s){
- assert(s->top>0);
- return s->dane[--s->top];
- }
- int isempty(struct Stack* s){
- return s->top == 0 ? 1 : 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement