Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int* createList();
- int* addToList(int* list, int addMe);
- void printList(int* list);
- void destroyList(int* list);
- int* removeFromList(int* list, int index);
- int findIndex(int* list, int search);
- int main() {
- printf("Hello, World!\n");
- int *list = createList();
- // int *list2 = createList();
- // addToList(list2,456);
- printList(list);
- list=addToList(list,5);
- list=addToList(list,17231);
- list=addToList(list,2);
- printList(list);
- // printList(list2);
- destroyList(list);
- return 0;
- }
- int findIndex(int* list, int search)
- {
- int i;
- for(i=0;i<list[0];i++)
- {
- if(list[i]==search)
- {
- return i;
- }
- }
- }
- int* createList()
- {
- int* list = (int*) calloc(2, sizeof(int));
- list[0] = 1;
- return list;
- }
- int* addToList(int* list, int addMe)
- {
- list = (int*)realloc(list, list[0] * sizeof(int) + sizeof(int));
- list[0]+=1;
- list[list[0]-1]=addMe;
- return list;
- }
- void printList(int* list)
- {
- int i;
- for(i=0;i<list[0];i++)
- {
- printf("%d: %d\n",i,list[i]);
- }
- }
- int* removeFromList(int* list, int index)
- {
- list = (int*)realloc(list, list[0] * sizeof(int) - sizeof(int));
- list[0]-=1;
- list[index]=0;
- int i;
- for(i=0;i<list[0]-index;i++)
- {
- list[index+i]=list[index+i+1];
- }
- return list;
- }
- void destroyList(int* list)
- {
- free(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement