Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mystack.h"
- #include "logging.h"
- /* The stack is an abstract data type.
- * this means that the internal structures are
- * never exposed to the users of the library.
- */
- /* Note: the stacks have no knowledge of what types
- * they are storing. This is not a concern of the stack
- */
- typedef struct stackObject* pStackObject_t;
- typedef struct stackObject
- {
- void* obj;
- pStackObject_t next;
- } StackObject_t;
- typedef struct stackMEta* pStackMeta_t;
- typedef struct stackMEta
- {
- pStackObject_t stack;
- size_t objsize;
- int numelem;
- int handle;
- pStackMeta_t next;
- } StackMeta_t;
- static pStackMeta_t gStackList = NULL;
- int mystack_create(size_t objsize)
- {
- /* Hint: use gStackList */
- int handle = 1;
- pStackMeta_t currentStack = gStackList;
- if(gStackList != NULL)
- {
- handle++;
- //go to the end of the list
- while(currentStack->next != NULL)
- {
- handle++;
- currentStack = currentStack->next;
- }
- }
- pStackMeta_t newMeta = (pStackMeta_t) malloc (sizeof(StackMeta_t));
- newMeta->stack = NULL;
- newMeta->objsize = objsize;
- newMeta->numelem = 0;
- newMeta->handle = handle;
- newMeta->next = NULL;
- if(gStackList != NULL)
- {
- currentStack->next = newMeta;
- }
- else
- {
- gStackList = newMeta;
- }
- DBG_PRINTF("Created stack with handle: %d and objsize %zu bytes\n", handle, objsize);
- return handle;
- }
- int mystack_push(int handle, void* obj)
- {
- pStackMeta_t currentStack = gStackList;
- if(gStackList == NULL)
- {
- return -1;
- }
- while(currentStack->handle != handle)
- {
- if(currentStack->next != NULL)
- {
- currentStack = currentStack->next;
- }
- else
- {
- return -1;
- }
- }
- pStackObject_t currentObject = currentStack->stack;
- pStackObject_t newObject = (pStackObject_t) malloc (sizeof(StackObject_t));
- newObject->obj = (void*) malloc(currentStack->objsize);
- newObject->next = NULL;
- memcpy(newObject->obj, obj, currentStack->objsize);
- if(currentObject == NULL)
- {
- currentStack->stack = newObject;
- }
- else
- {
- while(currentObject->next != NULL)
- {
- currentObject = currentObject->next;
- }
- currentObject->next = newObject;
- }
- DBG_PRINTF("handle: %d\n, obj: %p\n", handle, obj);
- return 0;
- }
- int mystack_pop(int handle, void* obj)
- {
- pStackMeta_t currentStack = gStackList;
- if(gStackList == NULL)
- {
- return -1;
- }
- while(currentStack->handle != handle)
- {
- if(currentStack->next != NULL)
- {
- currentStack = currentStack->next;
- }
- else
- {
- return -1;
- }
- }
- pStackObject_t currentObject = currentStack->stack;
- if(currentObject == NULL)
- {
- return -1;
- }
- while(currentObject->next != NULL)
- {
- currentObject = currentObject->next;
- }
- memcpy(obj, ¤tObject->obj, currentStack->objsize);
- free(currentObject->obj);
- free(currentObject);
- currentObject = NULL;
- DBG_PRINTF("handle: %d\n, obj: %p\n", handle, obj);
- return 0;
- }
- int mystack_destroy(int handle)
- {
- pStackMeta_t currentStack = gStackList;
- if(currentStack == NULL)
- {
- return -1;
- }
- pStackObject_t currentObject;
- if(currentStack->handle == handle)
- {
- currentObject = currentStack->stack;
- pStackMeta_t newHead = currentStack->next;
- free(currentStack);
- gStackList = newHead;
- }
- else
- {
- while(currentStack->next->handle != handle)
- {
- if(currentStack->next->next != NULL)
- {
- currentStack = currentStack->next;
- }
- else
- {
- return -1;
- }
- }
- currentObject = currentStack->next->stack;
- pStackMeta_t newNextStack = currentStack->next->next;
- free(currentStack->next);
- currentStack->next = newNextStack;
- }
- pStackObject_t next;
- while(currentObject != NULL)
- {
- next = currentObject->next;
- free(currentObject->obj);
- free(currentObject);
- currentObject = next;
- }
- DBG_PRINTF("handle: %d\n", handle);
- return 0;
- }
- int mystack_nofelem(int handle)
- {
- pStackMeta_t currentStack = gStackList;
- if(gStackList == NULL)
- {
- return -1;
- }
- while(currentStack->handle != handle)
- {
- if(currentStack->next != NULL)
- {
- currentStack = currentStack->next;
- }
- else
- {
- return -1;
- }
- }
- int count = 0;
- pStackObject_t currentObject = currentStack->stack;
- while(currentObject != NULL)
- {
- count++;
- currentObject = currentObject->next;
- }
- DBG_PRINTF("handle: %d\n", handle);
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement