Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Samuel Vedernikoff
- A12906756
- CS12xeq
- Assignment 3*/
- #include <malloc.h>
- #include <stdio.h>
- #include "mylib.h"
- #include "stack.h"
- #define STACK_POINTER_INDEX (-1) /* Index of last used space */
- #define STACK_SIZE_INDEX (-2) /* Index of size of the stack */
- #define STACK_COUNT_INDEX (-3) /* Index of which stack allocated */
- #define STACK_OFFSET 3 /* offset from allocation to where user info begins */
- /* catastrophic error messages */
- static const char DELETE_NONEXIST[] = "Deleting a non-existent stack!!!\n";
- static const char EMPTY_NONEXIST[] = "Emptying a non-existent stack!!!\n";
- static const char INCOMING_NONEXIST[] =
- "Incoming parameter does not exist!!!\n";
- static const char ISEMPTY_NONEXIST[] =
- "Isempty check from a non-existent stack!!!\n";
- static const char ISFULL_NONEXIST[] =
- "Isfull check from a non-existent stack!!!\n";
- static const char NUM_NONEXIST[] =
- "Num_elements check from a non-existent stack!!!\n";
- static const char POP_NONEXIST[] = "Popping from a non-existent stack!!!\n";
- static const char POP_EMPTY[] = "Popping from an empty stack!!!\n";
- static const char PUSH_NONEXIST[] = "Pushing to a non-existent stack!!!\n";
- static const char PUSH_FULL[] = "Pushing to a full stack!!!\n";
- static const char TOP_NONEXIST[] = "Topping from a non-existent stack!!!\n";
- static const char TOP_EMPTY[] = "Topping from an empty stack!!!\n";
- static const char WRITE_NONEXIST_FILE[] =
- "Attempt to write using non-existent file pointer!!!\n";
- static const char WRITE_NONEXIST_STACK[] =
- "Attempt to write to a non-existent stack!!!\n";
- /* Debug messages. HEX messages used for negative numbers on the stack. */
- static const char ALLOCATED[] = "[Stack %ld has been allocated]\n";
- static const char DEALLOCATE[] = "[Stack %ld has been deallocated]\n";
- static const char HEXPOP[] = "[Stack %ld - Popping 0x%lx]\n";
- static const char HEXPUSH[] = "[Stack %ld - Pushing 0x%lx]\n";
- static const char HEXTOP[] = "[Stack %ld - Topping 0x%lx]\n";
- static const char POP[] = "[Stack %ld - Popping %ld]\n";
- static const char PUSH[] = "[Stack %ld - Pushing %ld]\n";
- static const char TOP[] = "[Stack %ld - Topping %ld]\n";
- /* static variable allocation */
- static int debug = FALSE; /* allocation of debug flag */
- static int stack_counter = 0; /* number of stacks allocated so far */
- /* Debug state methods */
- void debug_off (void) {
- debug = FALSE;
- }
- void debug_on (void) {
- debug = TRUE;
- }
- /* function deallocates all the memory in the
- Stack and sets the stack pointer to NULL*/
- void delete_Stack (Stack ** spp) {
- /*assigns the pointer to the memory
- * to null thus deleting the stack*/
- *spp = NULL;
- /*deallocates the memory originally allocated to Stack*/
- free(*spp);
- }
- /* functions empties the stack of all its elements and sets the stack pointer to 0;
- * checks whether there are stacks to manipulate*/
- void empty_Stack (Stack * this_Stack) {
- if (this_Stack[-3] > 0)
- {
- this_Stack = (Stack *)-1;
- }
- }
- /*function checks whether the stack is empty*/
- long isempty_Stack (Stack * this_Stack) {
- if (this_Stack[-1] == -1 )
- {
- return 1;
- }
- else {
- return 0;
- }
- }
- /* function checks whether stack is full
- * makes sure that the stack_ counter is greater than 0
- * makes sure that there are elements in the stack*/
- long isfull_Stack (Stack * this_Stack) { //Step 2
- if (this_Stack[-3] > 0 && this_Stack[-2] >= 0)
- {
- if ( (this_Stack[-2] -1) != 0)
- {
- return 1;
- }
- else {
- return 0;
- }
- }
- else
- {
- printf ("%s\n", ISFULL_NONEXIST);
- }
- }
- /* function: allocates memory and intializes the a new Stack Object.
- * allocates memory to input number of longs, initializes the stack
- * infrastructure,
- * and returns a pointer to the first storage space in the stack.*/
- Stack * new_Stack (unsigned long stacksize) {
- Stack *memory = malloc((stacksize + STACK_OFFSET) * sizeof(long));
- Stack * this_Stack = (Stack*)memory + 3;
- /* Counter of stacks*/
- this_Stack[-3] = stack_counter;
- /*size of the stack*/
- this_Stack[-2] = stacksize;
- /*last used space*/
- this_Stack[-1] = 0;
- stack_counter++;
- return this_Stack;
- }
- /*function returns the size of the stack
- checks whether the stack counter is greater than 0*/
- long num_elements (Stack * this_Stack) { //Step 4
- if (this_Stack[-3] > 0)
- {
- return this_Stack[-2];
- }
- else {
- return 0;
- }
- }
- /* function takes the last element entered into the stack and removes it.
- * Then it assignes the last used space to the previous element. */
- long pop (Stack * this_Stack, long * item) {
- item = (long *)this_Stack[this_Stack[-1]];
- this_Stack[-1] = this_Stack[-1] - 1;
- }
- /* function takes a long passed into the parameter
- * and adds that value to the top of the stack
- * the function first checks if the stack is full
- * and whether there are stacks to manipulate*/
- long push (Stack * this_Stack, long item) {
- if (!isfull_Stack(this_Stack) && this_Stack[-3] > 0)
- {
- this_Stack[-1] = this_Stack[-1] + 1;
- this_Stack[this_Stack[-1]] = item;
- return 1;
- }
- else{
- return 0;
- }
- }
- /* function takes a stack pointer and a pointer to a long
- *it will store the last element in the stack to the long pointer*/
- long top (Stack * this_Stack, long * item) {
- if (this_Stack[-3] > 0 && this_Stack[-2] >= 0)
- {
- item[0] = this_Stack[-1];
- }
- }
- FILE * write_Stack (Stack * this_Stack, FILE * stream) {
- /* index into the stack */
- long index = 0;
- if (this_Stack == NULL) {
- fprintf (stderr, WRITE_NONEXIST_STACK);
- return stream;
- }
- if (stream == NULL) {
- fprintf (stderr, WRITE_NONEXIST_FILE);
- return stream;
- }
- if (stream == stderr)
- fprintf (stream, "Stack has %ld items in it.\n",
- num_elements (this_Stack));
- for (index = STACK_COUNT_INDEX + STACK_OFFSET;
- index < num_elements (this_Stack); index++) {
- if (stream == stderr)
- fprintf (stream, "Value on stack is |0x%lx|\n",
- this_Stack[index]);
- else {
- if (this_Stack[index] < 0)
- fprintf (stream, "%c ",
- (char) this_Stack[index]);
- else
- fprintf (stream, "%ld ", this_Stack[index]);
- }
- }
- return stream;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement