int main() { //test init stack *myStack = malloc(sizeof(stack)); printf("max capacity before init call: %d\n",myStack->cap); stack_init(myStack,10); printf("max capacity after init call: %d\n",myStack->cap); //test size printf("size of stack before pushes: %d\n",stack_size(myStack)); //test push printf("attempting to push these integers in the following order: \n)"); //seeding rand to make it truly 'random' srand(time(NULL)); while(stack_size(myStack)!=myStack->cap) { int temp = rand()%RAND_MAX; printf("%d\n",temp); stack_push(myStack,temp); } printf("size of stack after pushes: %d\n",stack_size(myStack)); //test pop printf("attempting to pop, reading off numbers as they're being popped: \n"); while(stack_size(myStack)>0) { printf("%d\n",stack_pop(myStack)); } printf("size of stack after pops: %d\n",stack_size(myStack)); //test stack pop error catch printf("attempting to pop with empty stack...\n"); stack_pop(myStack); //test deallocate stack_deallocate(myStack); return 0; }
int main() { stack s; int sizeOfStack = 10; int currNum = 0; printf("The initial size of the stack is: %d. \n", sizeOfStack); stack_init(&s, sizeOfStack); printf("Stack before pushing numbers: %d\n", stack_size(&s)); printf("Attempt to pop: \n"); stack_pop(&s); int i; for(i = 0; i < sizeOfStack; i++) { stack_push(&s,i+1); } printf("Stack after pushing %d - %d: %d\n",1,10, stack_size(&s)); printf("Pop off stack: %d.\n", stack_pop(&s)); printf("Pop off stack: %d.\n", stack_pop(&s)); printf("Push %d to the top of stack.\n", 44); stack_push(&s,10); printf("Push %d to the top of stack.\n", 233); stack_push(&s,233); printf("Pop off stack: %d.\n", stack_pop(&s)); printf("Push %d to the top of stack.\n",27); stack_push(&s,27); printf("Push %d to the top of stack, \n", 28); stack_push(&s, 28); printf("Deleting the stack. \n"); stack_deallocate(&s); stack_init(&s,10); printf("Size of stack after reinitialization: %d.\n", stack_size(&s)); }
int main() { struct stack my_stack; stack_init(&my_stack, 2); // Initialize stack printf("Elements: %i\n",stack_size(&my_stack)); // Elements: 0 stack_push(&my_stack,33); // Add 1 element printf("Number of elements should be 1: %i\n",stack_size(&my_stack)); // 1 printf("Pop out a 33: %i\n",stack_pop(&my_stack)); // 33 stack_push(&my_stack,33); stack_push(&my_stack,66); printf("Number of elements should be 2: %i\n",stack_size(&my_stack)); // 2 printf("Pop out a 66: %i\n",stack_pop(&my_stack)); // 66 stack_push(&my_stack,66); // re-push 66 so that there are two elements stack_push(&my_stack,99); // add another element to exceed capacity printf("Number of elements should STILL be 2 since exceeding capcity does nothing: %i\n", stack_size(&my_stack)); //2 stack_deallocate(&my_stack); // deallocate memory // pop the only two elements in stack stack_pop(&my_stack); stack_pop(&my_stack); printf("Number of elements should be 0: %i\n",stack_size(&my_stack)); // 0 // pop an element when there is nothing in the array stack_pop(&my_stack); printf("Number of elements should still be 0: %i\n",stack_size(&my_stack)); puts("Programming in C is cool!"); // Yay return 0; }