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