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"
- #include "task2a.c"
- #include <assert.h>
- int main( int argc, char const *argv[])
- {
- Stack stack;
- init(&stack);
- assert(isEmpty(&stack)==1);
- push(&stack, 12);
- push(&stack, 14);
- push(&stack, 16);
- display(&stack);
- printf("\n");
- Stack newStack;
- initAndCopy(&newStack, &stack);
- assert(equal(&stack, &newStack));
- display(&newStack);
- assert(pop(&newStack)==16);
- push(&newStack, 5);
- push(&newStack, 11);
- push(&newStack, 10);
- push(&newStack, 70);
- assert(pop(&newStack)==70);
- printf("\n");
- display(&newStack);
- printf("\n");
- assert(isEmpty(&stack)==0);
- printf("\n");
- assign(&newStack, &stack);
- assert(equal(&stack, &newStack));
- display(&newStack);
- printf("\n");
- display(&stack);
- printf("\n");
- destroy(&newStack);
- destroy(&stack);
- return 0;
- }
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define SIZE 5
- 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 copy(Stack* s, Stack* source);
- void assign (Stack *s, Stack *source);
- void display(Stack *s);
- void testIsEmpty(Stack* s);
- bool equal(Stack*s, Stack*s1);
- #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();
- }
- s->top = source->top;
- s->size = source->size;
- copy(s, source);
- }
- void copy(Stack* s, Stack* source)
- {
- 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;
- copy(s, source);
- }
- void display(Stack *s)
- {
- int i;
- for(i = 0; i <= s->top; i++) printf("%d ", s->data[i]);
- }
- void testIsEmpty(Stack* s)
- {
- if(isEmpty(s))
- {
- printf("\nStack is empty\n");
- } else
- {
- printf("\nStack is not empty\n");
- }
- }
- bool equal(Stack*s, Stack*s1)
- {
- int i;
- if(s->top != s1->top)
- {
- destroy(s);
- destroy(s1);
- return false;
- }
- for(i=0; i<s->top; i++)
- {
- if(s->data[i] != s1->data[i])
- {
- destroy(s);
- destroy(s1);
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment