Advertisement
tomur

myCListLibrary

Jan 3rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int* createList();
  5. int* addToList(int* list, int addMe);
  6. void printList(int* list);
  7. void destroyList(int* list);
  8. int* removeFromList(int* list, int index);
  9. int findIndex(int* list, int search);
  10.  
  11.  
  12. int main() {
  13.     printf("Hello, World!\n");
  14.     int *list = createList();
  15. //    int *list2 = createList();
  16. //    addToList(list2,456);
  17.     printList(list);
  18.     list=addToList(list,5);
  19.     list=addToList(list,17231);
  20.     list=addToList(list,2);
  21.     printList(list);
  22. //    printList(list2);
  23.     destroyList(list);
  24.     return 0;
  25. }
  26.  
  27. int findIndex(int* list, int search)
  28. {
  29.     int i;
  30.     for(i=0;i<list[0];i++)
  31.     {
  32.         if(list[i]==search)
  33.         {
  34.             return i;
  35.         }
  36.     }
  37. }
  38.  
  39.  
  40. int* createList()
  41. {
  42.     int* list = (int*) calloc(2, sizeof(int));
  43.     list[0] = 1;
  44.     return list;
  45. }
  46.  
  47. int* addToList(int* list, int addMe)
  48. {
  49.     list = (int*)realloc(list, list[0] * sizeof(int) + sizeof(int));
  50.     list[0]+=1;
  51.     list[list[0]-1]=addMe;
  52.     return list;
  53. }
  54.  
  55. void printList(int* list)
  56. {
  57.     int i;
  58.     for(i=0;i<list[0];i++)
  59.     {
  60.         printf("%d: %d\n",i,list[i]);
  61.     }
  62. }
  63.  
  64. int* removeFromList(int* list, int index)
  65. {
  66.     list = (int*)realloc(list, list[0] * sizeof(int) - sizeof(int));
  67.     list[0]-=1;
  68.     list[index]=0;
  69.     int i;
  70.     for(i=0;i<list[0]-index;i++)
  71.     {
  72.         list[index+i]=list[index+i+1];
  73.     }
  74.     return list;
  75. }
  76. void destroyList(int* list)
  77. {
  78.     free(list);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement