示例#1
0
int main(void){
	int i; char buffer[32];

	ArrayList *L1 = createArrayList(10);
	ArrayList *L2 = createArrayList(30);

	FILE *ifp = fopen("names.txt", "rb");

	printf("\nRead Names from file and add them to ArrayList l1\n");
	// Read all names from the file and add them to L1.
	while (fscanf(ifp, "%s", buffer) != EOF)
		put(L1, buffer);
	printf("\nAdd Names to L2 in rev order\n");
	// Add the names to L2 in reverse order.
	for (i = getSize(L1) - 1; i >= 0; i--)
		put(L2, get(L1, i));

	// Print the contents of L1.
	printf("\n-- LIST 1: --\n");
	printArrayList(L1);

	// Print the contents of L2.
	printf("\n-- LIST 2 (First Names): --\n");
	printArrayList(L2);

}
示例#2
0
/* Create FastIntBuffer with initial page size of 1024 ints */
FastIntBuffer *createFastIntBuffer(){
	FastIntBuffer *fib = NULL;
	ArrayList *al= createArrayList();
	if (al==NULL){
		throwException2(out_of_mem,
			"FastIntBuffer allocation failed ");
		return NULL;
	}

	fib = (FastIntBuffer *)malloc(sizeof(FastIntBuffer));
	if (fib==NULL) {
		freeArrayList(al);
		throwException2(out_of_mem,
			"FastIntBuffer allocation failed ");
		return NULL;
	}

	fib->size = 0;
	fib->capacity = 0;
	fib->pageSize = 1<<10;
	fib->exp = 10;
	fib->r = 1023;
	fib->al = al;
	return fib;
}
示例#3
0
/* create FastLongBuffer with page size of (1<<e) longs*/
FastLongBuffer *createFastLongBuffer2(int exp){
	FastLongBuffer *flb = NULL;
	ArrayList *al= createArrayList();
	if (al==NULL){
		throwException2(out_of_mem,
			"FastLongBuffer allocation failed ");
		return NULL;
	}

	flb = (FastLongBuffer *)malloc(sizeof(FastLongBuffer));
	if (flb==NULL) {
		freeArrayList(al); 
		throwException2(out_of_mem,
			"FastLongBuffer allocation failed ");
		return NULL;
	}

	flb->size = 0;
	flb->capacity = 0;
	flb->pageSize = 1<<exp;
	flb->exp = exp;
	flb->r = (1<<exp)-1;
	flb->al = al;

	return flb;
}
示例#4
0
文件: hashMap.c 项目: pallavig/dsa
void createLinkedListInBuckets(HashMap *map){
	int i;
	ArrayList listOfBuckets = createArrayList(map->capacity);
	map->buckets = calloc(1,sizeof(ArrayList));
	*(ArrayList*)map->buckets = listOfBuckets;
	for(i=0;i<map->capacity;i++)
		addInArrayList(map->buckets, create());
};
示例#5
0
int main(void)
{
	ArrayList *L1 = createArrayList(-1);

	// Trim array list. This should reduce its capacity to 0.
	trimArrayList(L1);

	// Trim again. This should produce no output since capacity is already 0.
	trimArrayList(L1);

	// Print the empty list.
	printArrayList(L1);
	printf("Size of list: %d\n", getSize(L1));

	// Destroy array list. Hopefully this won't segfault.
	L1 = destroyArrayList(L1);

	// Print the empty list.
	printArrayList(L1);
	printf("Size of list: %d\n", getSize(L1));

	// Since L1 has been destroyed and is now NULL, the put() function should
	// return NULL
	if (put(L1, "Hello, world!\n") == NULL)
		printf("Awesome!\n");
	else
		printf("Not so awesome!\n");

	// Create new list and add strings to the end. Print out the strings we're
	// adding (to ensure put() is returning pointers to those strings).
	printf("%s\n", put(L1 = createArrayList(-1), "Hello,"));
	printf("%s\n", put(L1, "world!"));

	// Print L1 again.
	printf("Array list contents:\n");
	printArrayList(L1);

	L1 = destroyArrayList(L1);

	return 0;
}
示例#6
0
int main(void){
	int i;
	ArrayList *testarr = createArrayList(10);
	for(i = 0;i<testarr->capacity;i++)
        put(testarr,"hi");
    printArrayList(testarr);
    expandArrayList(testarr,30);
    trimArrayList(testarr);
    printArrayList(testarr);
    printf("MAIN: size is %d\nMAIN: cap is %d\n",testarr->size, testarr->capacity);
	return 0;
}
示例#7
0
char *insertElement(ArrayList *list, int index, char *str){
    int i;
    ArrayList *temparr = createArrayList(list->capacity+1);//create a
    for (i = 0;i<index;i++){
        put(temparr,list->array[i]);
    }
	put(temparr,str);
    for (i = index;i<list->size-1;i++){
        put(temparr,list->array[i]);
    }
    free(list);
    return temparr;
}
示例#8
0
int removeElement(ArrayList *list, int index){
    int i;
    ArrayList *temparr = createArrayList(list->capacity-1);//create a
    for (i = 0;i<index;i++){
        put(temparr,list->array[i]);
    }
    //printf("\n\n\n\print array after first loop\n");
	//printArrayList(temparr);
    for (i = index+1;i<list->size;i++){
        put(temparr,list->array[i]);
    }
    free(list);
    return temparr;
}
示例#9
0
int main(void)
{
	char *str = NULL;

	ArrayList *myList = createArrayList(0);
	ArrayList *list = NULL;

	list = destroyArrayList(NULL);
	nullAssert(list, "destroyArrayList()");

	str = put(myList, NULL);
	nullAssert(str, "put()");

	str = get(NULL, -1);
	nullAssert(str, "get() (1/2)");

	str = get(myList, -1);
	nullAssert(str, "get() (2/2)");

	return 0;
}
示例#10
0
int main(void)
{
	int i; char myString[100], *temp;
	ArrayList *myList = createArrayList(10);

	// add some strings to the array list
	for (i = 0; i < 30; i++)
	{
		// format my string
		sprintf(myString, "(string %02d)", i);

		// add string to end of array list
		temp = put(myList, myString);

		// print a little notification that the string was added
		printf("Added string: %s\n", temp);
	}

	// print out the contents of the array list.
	printArrayList(myList);

	return 0;
}
示例#11
0
int main(void)
{
	int i; char buffer[32];

	ArrayList *L1 = createArrayList(-1);
	ArrayList *L2 = createArrayList(-1);

	FILE *ifp = fopen("names.txt", "rb");

	// Read all names from the file and add them to L1.
	while (fscanf(ifp, "%s", buffer) != EOF)
		put(L1, buffer);

	// Add the names to L2 in reverse order.
	for (i = getSize(L1) - 1; i >= 0; i--)
		put(L2, get(L1, i));

	// Print the contents of L1.
	printf("\n-- LIST 1: --\n");
	printArrayList(L1);

	// Print the contents of L2.
	printf("\n-- LIST 2 (First Names): --\n");
	printArrayList(L2);

	// Swap last names with first names in L2.
	for (i = 0; i < getSize(L2); i++)
	{
		if (strcmp(get(L2, i), "Sean") == 0)
			set(L2, i, "Szumlanski");
		else if (strcmp(get(L2, i), "Adam") == 0)
			set(L2, i, "Bouters");
		else if (strcmp(get(L2, i), "Antoniya") == 0)
			set(L2, i, "Petkova");
		else if (strcmp(get(L2, i), "Ashkan") == 0)
			set(L2, i, "Paya");
		else if (strcmp(get(L2, i), "Grant") == 0)
			set(L2, i, "Van Horn");
	}

	// Print the contents of L2.
	printf("\n-- LIST 2 (Last Names): --\n");
	printArrayList(L2);

	// Print L1 (in reverse order) and L2, to match up first and last names.
	printf("\n-- COMBINED LISTS (First and Last Names): --\n");
	for (i = 0; i < getSize(L2); i++)
		printf("%s %s\n", get(L1, getSize(L1) - 1 - i), get(L2, i));

	// Add elements from L1 to the end of L1 (in reverse order).
	printf("\n");
	for (i = getSize(L1) - 1; i >= 0; i--)
		printf("Adding %s to L1 ...\n", put(L1, get(L1, i)));

	// Print the contents of L1.
	printf("\n-- LIST 1: --\n");
	printArrayList(L1);

	// Insert a string at the beginning of list L1.
	insertElement(L1, 0, "List of Names:");

	// Print the contents of L1.
	printf("\n-- LIST 1: --\n");
	printArrayList(L1);

	// Remove all elements from L1.
	while (getSize(L1))
		removeElement(L1, 0);

	// Print L1, which is now an empty list.
	printf("\n-- LIST 1: --\n");
	printArrayList(L1);

	// Destroy our lists.
	L1 = destroyArrayList(L1);
	L2 = destroyArrayList(L2);

	// Make sure L1 is good and destroyed (and that destroyArrayList doesn't
	// segfault when passed a NULL pointer).
	L1 = destroyArrayList(L1);

	// Print the empty lists one last time.
	printf("\n-- LIST 1: --\n");
	printArrayList(L1);

	printf("\n-- LIST 2: --\n");
	printArrayList(L2);

	return 0;
}
示例#12
0
void initArrays()
{
    tokenArray = malloc(sizeof(Token)*10000);
    codeArray = createArrayList(10);
}
示例#13
0
文件: hashMap.c 项目: KajalJadhav/DSA
void assignBucketsAndSlots(Hashmap* hash,int capacity){
	hash->bucket = createArrayList(capacity);
	assignAllSlots(&hash->bucket);
}