Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Declaration of struct "stack"
- //INV: FILO - "First in - last out"
- typedef struct {
- int* arrayOfIntegers;
- int size;
- } stack;
- //Creating instance of struct "stack"
- stack* createStack(size_t size) {
- stack *newStack = calloc(1, sizeof(stack));
- newStack -> arrayOfIntegers = (int*)calloc(size, sizeof(int));
- newStack -> size = 0;
- return newStack;
- }
- void ensureCapacityOfStack(stack* stack) {
- int* newArray = (int*)calloc(2 * (size_t)stack -> size, sizeof(int));
- memcpy(newArray, stack -> arrayOfIntegers, stack -> size);
- stack -> arrayOfIntegers = newArray;
- }
- void ensureCapacity(char* expression, size_t size) {
- char* newLine = (char*)calloc(2 * size + 1, sizeof(char));
- memcpy(newLine, expression, size);
- expression = newLine;
- }
- //Add the element to stack Time: O(1)
- void push(stack* stack, int value) {
- stack -> arrayOfIntegers[stack -> size++] = value;
- if (stack -> size % 1000 == 0) {
- ensureCapacityOfStack(stack);
- }
- }
- //Returns the element on the peek of stack Time: O(1)
- int peek(stack* stack) {
- return stack -> arrayOfIntegers[stack -> size - 1];
- }
- //Delete the peek element from stack Time: O(1)
- void pop(stack* stack) {
- if (stack -> size == 0) {
- printf("bad input");
- exit(1);
- }
- stack -> arrayOfIntegers[stack -> size--] = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement