Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- typedef struct{
- int* data;
- int top;
- int size;
- } Stack;
- void init(Stack* s);
- void destroy(Stack* s);
- void push(Stack *s, int element);
- int pop(Stack* s);
- bool isEmpty(Stack* s);
- void initAndCopy(Stack* s, Stack* source);
- void assign(Stack* s, Stack* source);
- void copy(Stack* s, Stack* source);
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include "stack1a.h"
- void init(Stack* s)
- {
- s->top = -1;
- s->size = 5;
- s->data = (int*)malloc(s->size * sizeof(int));
- if(s->data == NULL)
- {
- printf("Memory not allocated");
- free(s->data);
- exit(1);
- }
- }
- void initAndCopy(Stack* s, Stack* source)
- {
- init(s);
- copy(s, source);
- }
- void assign(Stack* s, Stack* source)
- {
- if(s->size == source->size)
- {
- s->top = source->top;
- copy(s, source);
- } else {
- s->size = source->size;
- int *temp = realloc(s->data, s->size);
- if(temp == NULL)
- {
- printf("Reallocation problem. Aborting\n");
- abort();
- }
- s->data = temp;
- s->top = source->top;
- printf("size of new stack is: %d\n", s->size);
- copy(s, source);
- }
- }
- void copy(Stack* s, Stack* source)
- {
- int counter = 0;
- while(counter <= source->top)
- {
- s->data[counter] = source->data[counter];
- //push(s, source->data[counter]);
- counter++;
- }
- }
- void push(Stack *s, int element)
- {
- if(s->top == s->size -1)
- {
- int *temp = realloc(s->data, s->size*2*sizeof(int));
- if(temp == NULL)
- {
- printf("Reallocation problem. Aborting\n");
- abort();
- } else
- {
- s->data = temp;
- }
- s->size *= 2;
- }
- s->data[++s->top] = element;
- }
- int pop(Stack* s)
- {
- if(isEmpty(s))
- {
- printf("Stack is empty\n");
- //destroy(s); pozorne rozwiazanie, tylko na potrzeby valgrind
- exit(1);
- }
- 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);
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- #include "stack1a.h"
- //#include "stack1a.c"
- void testIsEmpty(Stack* s)
- {
- if(isEmpty(s))
- {
- printf("\nStack is empty\n");
- } else {
- printf("\nStack is not empty\n");
- }
- }
- int main( int argc, char const *argv[])
- {
- //TESTING initandcopy function
- Stack s1;
- init(&s1);
- int counter;
- for(counter = 0; counter < 25; counter++)
- {
- push(&s1, counter);
- }
- Stack s2;
- initAndCopy(&s2, &s1);
- while(s2.top > -1)
- {
- assert(pop(&s2) == pop(&s1));
- }
- destroy(&s2);
- destroy(&s1);
- //TESTING THE ASSIGN FUNCTION
- /*
- Stack s1;
- init(&s1);
- testIsEmpty(&s1);
- Stack s2;
- init(&s2);
- int counter;
- for(counter = 0; counter < 25; counter++)
- {
- push(&s1, counter);
- push(&s2, counter%3);
- }
- testIsEmpty(&s1);
- assign(&s2, &s1);
- while(s1.top > -1)
- {
- assert(pop(&s2) == pop(&s1));
- //printf("Value from stack\ns1: %d\n", pop(&s1));
- }
- testIsEmpty(&s1);
- destroy(&s2);
- destroy(&s1);
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement