Advertisement
Guest User

broken linked list

a guest
Jan 21st, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1. #include "mystack.h"
  2. #include "logging.h"
  3. /* The stack is an abstract data type.
  4.  * this means that the internal structures are
  5.  * never exposed to the users of the library.
  6.  */
  7.  
  8. /* Note: the stacks have no knowledge of what types
  9.  * they are storing. This is not a concern of the stack
  10.  */
  11.  
  12. typedef struct stackObject* pStackObject_t;
  13. typedef struct stackObject
  14. {
  15.     void* obj;
  16.     pStackObject_t next;
  17. } StackObject_t;
  18.  
  19. typedef struct stackMEta* pStackMeta_t;
  20. typedef struct stackMEta
  21. {
  22.     pStackObject_t stack;
  23.     size_t objsize;
  24.     int numelem;
  25.     int handle;
  26.     pStackMeta_t next;
  27. } StackMeta_t;
  28.  
  29. static pStackMeta_t gStackList = NULL;
  30.  
  31.  int mystack_create(size_t objsize)
  32.  {
  33.         /* Hint: use gStackList */
  34.         int handle = 1;
  35.  
  36.         pStackMeta_t currentStack = gStackList;
  37.  
  38.         if(gStackList != NULL)
  39.         {
  40.             handle++;
  41.  
  42.             //go to the end of the list
  43.             while(currentStack->next != NULL)
  44.             {
  45.                 handle++;
  46.                 currentStack = currentStack->next;
  47.             }
  48.         }
  49.  
  50.         pStackMeta_t newMeta = (pStackMeta_t) malloc (sizeof(StackMeta_t));
  51.         newMeta->stack = NULL;
  52.         newMeta->objsize = objsize;
  53.         newMeta->numelem = 0;
  54.         newMeta->handle = handle;
  55.         newMeta->next = NULL;
  56.  
  57.         if(gStackList != NULL)
  58.         {
  59.             currentStack->next = newMeta;
  60.         }
  61.         else
  62.         {
  63.             gStackList = newMeta;
  64.         }
  65.  
  66.         DBG_PRINTF("Created stack with handle: %d and objsize %zu bytes\n", handle, objsize);
  67.         return handle;
  68.  }
  69.  
  70.  int mystack_push(int handle, void* obj)
  71.  {
  72.         pStackMeta_t currentStack = gStackList;
  73.  
  74.         if(gStackList == NULL)
  75.         {
  76.             return -1;
  77.         }
  78.  
  79.         while(currentStack->handle != handle)
  80.         {
  81.             if(currentStack->next != NULL)
  82.             {
  83.                 currentStack = currentStack->next;
  84.             }
  85.             else
  86.             {
  87.                 return -1;
  88.             }
  89.         }
  90.  
  91.         pStackObject_t currentObject = currentStack->stack;
  92.         pStackObject_t newObject = (pStackObject_t) malloc (sizeof(StackObject_t));
  93.         newObject->obj = (void*) malloc(currentStack->objsize);
  94.         newObject->next = NULL;
  95.         memcpy(newObject->obj, obj, currentStack->objsize);
  96.  
  97.         if(currentObject == NULL)
  98.         {
  99.             currentStack->stack = newObject;
  100.         }
  101.         else
  102.         {
  103.             while(currentObject->next != NULL)
  104.             {
  105.                 currentObject = currentObject->next;
  106.             }
  107.             currentObject->next = newObject;
  108.         }
  109.     DBG_PRINTF("handle: %d\n, obj: %p\n", handle, obj);
  110.  
  111.         return 0;
  112.  }
  113.  
  114.  int mystack_pop(int handle, void* obj)
  115.  {
  116.      pStackMeta_t currentStack = gStackList;
  117.  
  118.      if(gStackList == NULL)
  119.      {
  120.          return -1;
  121.      }
  122.  
  123.      while(currentStack->handle != handle)
  124.      {
  125.          if(currentStack->next != NULL)
  126.          {
  127.              currentStack = currentStack->next;
  128.          }
  129.          else
  130.          {
  131.              return -1;
  132.          }
  133.      }
  134.  
  135.      pStackObject_t currentObject = currentStack->stack;
  136.  
  137.      if(currentObject == NULL)
  138.      {
  139.          return -1;
  140.      }
  141.  
  142.      while(currentObject->next != NULL)
  143.      {
  144.          currentObject = currentObject->next;
  145.      }
  146.  
  147.      memcpy(obj, &currentObject->obj, currentStack->objsize);
  148.      free(currentObject->obj);
  149.      free(currentObject);
  150.      currentObject = NULL;
  151.  
  152.     DBG_PRINTF("handle: %d\n, obj: %p\n", handle, obj);
  153.     return 0;
  154.  }
  155.  
  156.  int mystack_destroy(int handle)
  157.  {
  158.      pStackMeta_t currentStack = gStackList;
  159.  
  160.      if(currentStack == NULL)
  161.      {
  162.          return -1;
  163.      }
  164.  
  165.      pStackObject_t currentObject;
  166.  
  167.      if(currentStack->handle == handle)
  168.      {
  169.          currentObject = currentStack->stack;
  170.          pStackMeta_t newHead = currentStack->next;
  171.          free(currentStack);
  172.          gStackList = newHead;
  173.      }
  174.  
  175.      else
  176.      {
  177.          while(currentStack->next->handle != handle)
  178.          {
  179.              if(currentStack->next->next != NULL)
  180.              {
  181.                  currentStack = currentStack->next;
  182.              }
  183.              else
  184.              {
  185.                  return -1;
  186.              }
  187.          }
  188.  
  189.          currentObject = currentStack->next->stack;
  190.          pStackMeta_t newNextStack = currentStack->next->next;
  191.          free(currentStack->next);
  192.          currentStack->next = newNextStack;
  193.      }
  194.  
  195.      pStackObject_t next;
  196.  
  197.      while(currentObject != NULL)
  198.      {
  199.          next = currentObject->next;
  200.          free(currentObject->obj);
  201.          free(currentObject);
  202.          currentObject = next;
  203.      }
  204.  
  205.     DBG_PRINTF("handle: %d\n", handle);
  206.     return 0;
  207.  }
  208.  
  209.  int mystack_nofelem(int handle)
  210.  {
  211.      pStackMeta_t currentStack = gStackList;
  212.  
  213.      if(gStackList == NULL)
  214.      {
  215.          return -1;
  216.      }
  217.  
  218.      while(currentStack->handle != handle)
  219.      {
  220.          if(currentStack->next != NULL)
  221.          {
  222.              currentStack = currentStack->next;
  223.          }
  224.          else
  225.          {
  226.              return -1;
  227.          }
  228.      }
  229.  
  230.      int count = 0;
  231.  
  232.      pStackObject_t currentObject = currentStack->stack;
  233.  
  234.      while(currentObject != NULL)
  235.      {
  236.          count++;
  237.          currentObject = currentObject->next;
  238.      }
  239.  
  240.     DBG_PRINTF("handle: %d\n", handle);
  241.     return count;
  242.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement