Beispiel #1
0
int main(int argc, char* argv[]){
  if(argc==2)
    {
      struct bag* b = (struct bag*)malloc(sizeof(struct bag));/*Create new bag*/
      initBag(b);/*Initialize*/
      char* s=argv[1];
      int n = atoi(s);/*number of elements to add*/
      int i;
      for( i = 1; i < n; ++i)
    {
	    addFrontList(b->l, (TYPE)(i));/*Add elements*/
    }
        addToBag(b, (TYPE)9);
        addToBag(b, (TYPE)3);
        addToBag(b, (TYPE)9);
     double t1 = getMilliseconds();/*Time before contains()*/
      printf("Found? %d\n", bagContains(b, (TYPE)20));
      double t2 = getMilliseconds();/*Time after contains()*/
        printf("Size: %d\n", b->l->size);
        removeFromBag(b, (TYPE)9);
        printf("Size: %d\n", b->l->size);
        addFrontList(b->l, 10.0);
        printf("Front: %f\n", frontList(b->l));
      printf("%d %g\n", n, t2-t1);
        freeBag(b);
    }
  else
  {
      printf("Please enter the number of elements to add.\n");
                }

  return 0;

}
Beispiel #2
0
int main(int argc, char* argv[]) {
    int returned = -1;
    struct linkedList* q = createLinkedList(3);
    printf("Checking if the list is empty: %d\n", isEmptyList(q));
    
    printf("Attempting to add to front \n");
    addFrontList(q, 1);
    removeList(q, 1);
    printf("Checking if the list is empty: %d\n", isEmptyList(q));
    addFrontList(q, 2);
    addFrontList(q, 3);
    _printList(q);
    
    printf("Attempting to add to back \n");
    addBackList(q, 4);
    addBackList(q, 5);
    addBackList(q, 6);
    _printList(q);
    
    printf("Attempting to print front \n");
    printf("%d\n", frontList(q));
    
    printf("Attempting to print back \n");
    printf("%d\n", backList(q));
    
    printf("Attempting to remove front \n");
    removeFrontList(q);
    _printList(q);
    
    printf("Attempting to remove back \n");
    removeBackList(q);
    _printList(q);
    
    printf("Attempting to delete 4 \n");
    removeList(q, 4);
    _printList(q);
    
    printf("Attempting to print list \n");
    _printList(q);
    
    printf("Attempting to confirm contains 5 \n");
    returned = containsList(q, 5);
    if (returned == 1)
        printf("true \n");
    else
        printf("false \n");
    
    return 0;
}
Beispiel #3
0
/* 
	Add an item to the bag
	param: 	lst		pointer to the bag
	param: 	v		value to be added
	pre:	lst is not null
	post:	a link storing val is added to the bag
 */
void addList(struct linkedList *lst, TYPE v)
{
	addFrontList(lst, v); 

	/* FIXME: you must write this */

}
Beispiel #4
0
int main(){

	struct bag* b = (struct bag*)malloc(sizeof(struct bag));/*Create new bag*/
	initBag(b);/*Initialize*/

    printf("size of list = %d \n", b->lst->size);
    printf("\n");

    addFrontList(b->lst, 3);
    addFrontList(b->lst, 2);
    addBackList(b->lst, 4);
    addBackList(b->lst, 5);
    addBackList(b->lst, 6);
    addFrontList(b->lst, 1);

    int i;
    struct DLink *currentlink = b->lst->head->next;
    for(i = 1; i <= b->lst->size; i++){
            printf("index %d = %d \n", i, currentlink->value);
            currentlink = currentlink->next;
    }
    printf("\n");
    printf("front = %d \n", frontList(b->lst));
    printf("back = %d \n", backList(b->lst));
    printf("is list empty? = %d \n", isEmptyList(b->lst));
    printf("size of list = %d \n", b->lst->size);
    printf("list contains = %d \n", listContains(b->lst, 12));

    printf("\n");
    addToBag(b, 10);
    removeFromBag(b, 1);
    struct DLink *link = b->lst->head->next;
    for(i = 1; i <= b->lst->size; i++){
            printf("index %d = %d \n", i, link->value);
            link = link->next;
    }
    printf("list contains = %d \n", bagContains(b, 100));

    return 0;

}
int main(int argc, char* argv[]) {
    printf("implement testLinkedList.c!\n");
    
    struct linkedList *lst;
    lst = createLinkedList();
    
    printf("isEmptyList?%i\n", isEmptyList(lst));
    
    printf("\nTesting add links (3),5,6,9,8\n");
    addList(lst, 5);
    printf("addList 5:%i\n", frontList(lst));
    addBackList(lst, 6);
    printf("backList 6:%i\n", backList(lst));
    addBackList(lst, 9);
    printf("backList 9:%i\n", backList(lst));
    
    addFrontList(lst,3);
    printf("frontList 3:%i\n", frontList(lst));
    
    addBackList(lst, 8);
    printf("backList 8:%i\n\n", backList(lst));
    
    printf("frontList 3:%i\n", frontList(lst));
    printf("backList 8:%i\n\n", backList(lst));
    
    printf("remove the front(3) and back(8)...\n");
    removeFrontList(lst);
    printf("frontList should now be 5:%i\n", frontList(lst));
    removeBackList(lst);
    printf("backList should now be 9:%i\n", backList(lst));
    
    printf("contains 5?%i\n",containsList(lst,5));
    printf("removing 5...");
    removeFrontList(lst);
    printf("contains 5?%i\n",containsList(lst,5));
    
    printf("removing 6...");
    removeList(lst,6);
    printf("frontList? 9:%i", frontList(lst));
    printf("isEmptyList?%i\n", isEmptyList(lst));
    
    return 0;
}
Beispiel #6
0
/* 
	Add an item to the bag
	param: 	lst		pointer to the bag
	param: 	v		value to be added
	pre:	lst is not null
	post:	a link storing val is added to the bag
 */
void addList(struct linkedList *lst, TYPE v)
{
  assert(lst != NULL);
  addFrontList(lst,v); // wasn't clear if it should be front or back
}
Beispiel #7
0
/*
	addToBag: Function to add an element to the bag
	param: b the bag
	param: val the value added to the bag
	pre: b is not null
	post: val is added to the bag (list implementing the bag), size is incremented
*/
void addToBag(struct bag* b, TYPE val) {
 	addFrontList(b->lst->head, val);
 	++b->size;
}
Beispiel #8
0
/* 
	Add an item to the bag
	param: 	lst		pointer to the bag
	param: 	v		value to be added
	pre:	lst is not null
	post:	a link storing val is added to the bag
 */
void addList(struct linkedList *lst, TYPE v)
{
	addFrontList(lst, v);

}
Beispiel #9
0
void addList(struct linkedList *lst, TYPE v)
{
	assert(lst != NULL);
	addFrontList(lst, v);
}
Beispiel #10
0
/* Function to add an element to the bag:
	Pre: b is not null
	Post: val is added to the bag ie the list implementing the bag*/
void addToBag(struct bag* b, TYPE val){
	assert(b != NULL);
 	addFrontList(b->l, val);
}