Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define STACK_SIZE 100
- int *adressen[STACK_SIZE];
- //Store value at end of stack
- int push(int value) {
- if (endofstack() <= STACK_SIZE)
- {
- adressen[endofstack()] = malloc(sizeof(int));
- *adressen[endofstack() - 1] = value;
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //return the first value of the stack and shift all to the left
- int pop() {
- int value = *adressen[0];
- for (int i = 0; i < STACK_SIZE; i++)
- {
- adressen[i] = adressen[i + 1];
- }
- adressen[STACK_SIZE] = NULL;
- return value;
- }
- //return the position of the end of the stack (empty adresses filled with NULL)
- int endofstack() {
- int i = 0;
- while (adressen[i] != NULL) {
- i++;
- }
- return i;
- }
- int main() {
- for (int i = 0; i < STACK_SIZE; i++)
- {
- adressen[i] = NULL;
- }
- push(123);
- printf("%d\n", pop());
- for (int i = 0; endofstack() < STACK_SIZE; i++)
- {
- push(i);
- }
- while (endofstack() > 0) {
- printf("%d\n", pop());
- }
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement