Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include "task2a.h"
- void init(Stack* s)
- {
- s->top = -1;
- s->size = SIZE;
- s->data = (int*)malloc(SIZE * sizeof(int));
- if(s->data == NULL)
- {
- printf("error");
- free(s->data);
- abort();
- }
- }
- void push(Stack *s, int element)
- {
- if(s->top == s->size -1)
- {
- s->data = realloc(s->data, s->size*2*sizeof(int));
- if (!s->data)
- {
- free(s->data);
- perror("realloc");
- }
- s->size *= 2;
- }
- s->data[++s->top] = element;
- }
- int pop(Stack* s)
- {
- if(isEmpty(s))
- {
- printf("Stack is empty");
- abort();
- }
- else
- {
- return s->data[s->top--];
- }
- }
- bool isEmpty(Stack *s)
- {
- if (s->top == -1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void destroy (Stack *s)
- {
- free(s->data);
- }
- void initAndCopy(Stack* s, Stack* source)
- {
- s->data = (int*)malloc(sizeof(int) * source->size);
- // if(s->data == NULL)
- // {
- // printf("unable to allocate memory");
- // abort();
- // }
- assert(s->data != NULL);
- s->top = source->top;
- s->size = source->size;
- for(int i = s->top; i>=0;i--)
- {
- s->data[i] = source-> data[i];
- }
- }
- void assign (Stack *s, Stack *source)
- {
- if(s->size != source->size)
- {
- s->data = realloc(s->data, sizeof(int) * (source->size));
- }
- s->top = source->top;
- s->size = source->size;
- for(int i = source->top; i>=0; i--)
- {
- s->data[i] = source-> data[i];
- }
- }
- void display(Stack *s)
- {
- int i;
- for(i = 0; i <= s->top; i++) printf("%d ", s->data[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement