int main(int argc, char * argv[]) {
    bool pass = true;

    {
        int nums[] = { 1, 3, 2, 1, 2 };
        ListNode * list = ll_create(nums, sizeof(nums) / sizeof(int));
        ListNode * node = list->next;
        delete_middle_node(node);
        ll_print(list);
        ll_delete(list);
    }

    {
        int nums[] = { 1, 3, 2, 1, 2 };
        ListNode * list = ll_create(nums, sizeof(nums) / sizeof(int));
        ListNode * node = list->next->next;
        delete_middle_node(node);
        ll_print(list);
        ll_delete(list);
    }

    {
        int nums[] = { 1, 3, 2, 1, 2 };
        ListNode * list = ll_create(nums, sizeof(nums) / sizeof(int));
        ListNode * node = list->next->next->next;
        delete_middle_node(node);
        ll_print(list);
        ll_delete(list);
    }

    return (pass ? 0 : -1);
}
Example #2
0
File: jlg_hash.c Project: ccbon/ocp
int hash_delete(hash_t **hashpp) {
	if (hashpp && *hashpp) {
		hash_t *hashp = *hashpp;
		
		// go through the double linked list and remove all
		double_linked_list_t *listp = hashp->dlistp;
		while (listp->first) {
			// remove from pair both key and value (and the pair itself)
			hash_pair_t *pairp = (hash_pair_t *) listp->first->valuep;
			free(pairp->key);
			hashp->cfg->free_func(pairp->value);
			free(pairp);
			
			// remove the dll_node
			dll_node_t *nodep = listp->first->nextp;
			free(listp->first);
			listp->first = nodep;
		}
		JLG_FREE(&(hashp->dlistp));
		// go through the array and remove any linked list (without the values which are already freed)
		int i = 0;
		for (i = 0; i < hashp->cfg->key_size; i++) {
			if (hashp->array[i]) {
				ll_delete(&(hashp->array[i]));
			}
		}
		JLG_FREE(&(hashp->array));
		
		// remove the config if special
		hash_config_delete(&(hashp->cfg));
	}
	JLG_FREE(hashpp);
	return 0;
}
Example #3
0
uintptr_t
rfl_alloc_range(rfl_t rfl, unsigned long size)
{
        struct range *range;
        uintptr_t retval;
        rfl_t cur;

        /*
         * Find size entries to allocate 
         */
        if (rfl->next == rfl) {
                return 0;
        }

        cur = rfl->next;
        while (cur != rfl) {
                range = cur->data;
                retval = range->from;
                if (range->from + size - 1 == range->to) {
                        free(range);
                        ll_delete(cur);
                        return retval;
                } else if (range->from + size - 1 < range->to) {
                        range->from += size;
                        return retval;
                } else {
                        cur = cur->next;
                }
        }

        /*
         * Didn't find any valid sized things 
         */
        return 0;
}
Example #4
0
void
module_clear(void)
{
	if (module_list)
	{
		ll_delete(module_list, (ll_delete_t) module_delete);
	}

	return;
}
Example #5
0
void
msgmod_delete(void *data)
{
	msgmod_t *mm = data;

	ll_delete(mm->mm_args, NULL);
	free(mm);

	return;
}
Example #6
0
void
ll_free(struct ll *ll)
{
    struct ll *tmp = ll->next;

    while (tmp != ll) {
        tmp = ll_delete(tmp);
    }
    free(ll);
}
Example #7
0
File: cf.c Project: badzong/mopher
void
cf_set_keylist(var_t *table, ll_t *keys, var_t *v)
{
	char *key;
	var_t *sub;

	if (table == NULL) {
		table = cf_config;
	}

	/*
	 * Last key queued belongs to var_t *v itself.
	 */
	key = LL_DEQUEUE(keys);
	if(keys->ll_size == 0) {

		/*
		 * keys is created in cf_yacc.y and no longer needed.
		 */
		ll_delete(keys, NULL);

		if(v->v_name == NULL) {
			v->v_name = key;
		}

		if(vtable_set(table, v) == -1) {
			log_die(EX_CONFIG, "cf_set: vtable_set failed");
		}

		return;
	}

	if((sub = vtable_lookup(table, key))) {
		cf_set_keylist(sub, keys, v);

		/*
		 * key is strdupd in cf_yacc.y and no longer needed.
		 */
		free(key);

		return;
	}

	if ((sub = vtable_create(key, 0)) == NULL) {
		log_die(EX_CONFIG, "cf_setr: vtable_create failed");
	}

	if(vtable_set(table, sub) == -1) {
		log_die(EX_CONFIG, "cf_set: vtable_set failed");
	}

	cf_set_keylist(sub, keys, v);

	return;
}
Example #8
0
int main(int argc, char** argv) {
    if (argc < (INPUT_ARG + 1)) {
        printf("***Missing input argument\n");
        return EXIT_FAILURE; 
    }

    FILE* file = fopen(argv[INPUT_ARG], FILE_READ_ONLY);
    if (!file) {
        printf("***File \"%s\" could not be found\n", argv[INPUT_ARG]);
        fclose(file);
        return EXIT_FAILURE;
    }

    linked_list* in_order_list = build_list_from_file(file, false);
    linked_list* post_order_list = build_list_from_file(file, true);
    if (in_order_list->size != post_order_list->size ||
        in_order_list->size == 0 || post_order_list->size == 0) {
        ll_delete(in_order_list);
        ll_delete(post_order_list);
        fclose(file);
        printf("***the length of the inorder and post order traversal"
               " in the input must be nonzero and equal.");
        return EXIT_FAILURE;
    }

    printf("inorder: ");
    ll_print(in_order_list);
    printf("postorder: ");
    ll_print(post_order_list);

    binary_tree* btree = new_binary_tree(in_order_list, post_order_list);
    bt_print(btree);
    
    fclose(file);
    ll_delete(in_order_list);
    free(in_order_list);
    ll_delete(post_order_list);
    free(post_order_list);
    bt_delete(btree);
    free(btree);
    return EXIT_SUCCESS;
}
Example #9
0
uintptr_t
rfl_alloc_specific_range(rfl_t rfl, uintptr_t from, unsigned long size)
{
    struct range *range;
    uintptr_t retval;
    rfl_t cur;

    /*
     * Find size entries to allocate 
     */
    if (rfl->next == rfl) {
        return 0;
    }

    cur = rfl->next;
    do {
        range = cur->data;
        retval = range->from;
        if(range->from <= from && range->to > from &&
            range->to >= (from + size - 1)) {
            if(range->from == from && 
                range->from + size - 1 == range->to) {
                free(range);
                ll_delete(cur);
                return retval;
            } else if (range->from == from) {
                range->from += size;
                return retval;
            } else if (from + size - 1 == range->to) {
                range->to -= size;
                return range->to;
            } else {
                struct range *new_range = malloc(sizeof(struct range));

                if(new_range == 0) {
                    return E_RFL_NOMEM; /* Failure! */
                }
                ll_insert_before(cur, new_range);
                new_range->from = range->from;
                new_range->to = from - 1;
                range->from = from + size;
                return from;
            }
        } else {
            cur = cur->next;
        }
    } while (cur != rfl);

    /*
     * Unable to fulfil request
     */
    return 0;
}
Example #10
0
int main( int argc, char** argv){
  int d = 0, quit = 1, commandNum, numRead;
  char *input, command;
  Line l = line_init(stdin);
  Ll ll = ll_init();

  gen_parse_args( argc, argv, &d);

  while( quit){
    printf("Command: ");
    line_read_line( l);
    input = get_line( l);
    sscanf(input, " %c%n", &command, &numRead);
    switch( command){
      case 'q': quit = 0; break;
      case 'i': if( gen_exists_num( input+numRead) ){
                sscanf(input+numRead, "%d", &commandNum);
                ll_insert( ll, commandNum , d);
              }
              else printf("Sorry, you didn't enter a number!\n");
              break;
      case 'd': if( gen_exists_num( input+numRead) ){
                sscanf( input+numRead, "%d", &commandNum);
                ll_delete( ll, commandNum, d);
              }
              else printf("Sorry, you didn't enter a number!\n");
              break;
      case 'c': if( gen_exists_num( input+numRead) ){
                  sscanf( input+numRead, "%d", &commandNum);
                  if( ll_contains( ll, commandNum, d) )
                    printf("LIST DOES CONTAIN %d\n", commandNum);
                  else printf("LIST DOES NOT CONATAIN %d\n", commandNum);
                }
                else printf("Sorry, you didn't enter a number!\n");
                break;
      case 'e': ll_empty( ll, d); break;
      case 'l': ll_print( ll, d); break;
      case 'r': ll_print_rev( ll, d); break;
      case '?': gen_print_help(); break;
      case 'h': gen_print_help(); break;
      case '\n': break;
      default: printf("Invalid Command\n");
    }
    free( input);    
  }

  ll_free( ll, d);
  line_free( l);
  return 0;
}
Example #11
0
void
exp_clear(void)
{
	if (exp_defs)
	{
		sht_delete(exp_defs);
	}

	if (exp_garbage)
	{
		ll_delete(exp_garbage, (ll_delete_t) exp_delete);
	}

	return;
}
Example #12
0
/*
 * free_buf()
 *	Release buffer storage, remove from hash
 */
static void
free_buf(struct buf *b)
{
	ASSERT_DEBUG(b->b_list, "free_buf: null b_list");
	ASSERT_DEBUG(b->b_locks == 0, "free_buf: locks");
	ll_delete(b->b_list);
	(void)hash_delete(bufpool, b->b_start);
	bufsize -= b->b_nsec;
	ASSERT_DEBUG(b->b_data, "free_buf: null b_data");
	free(b->b_data);
	if (b->b_handles) {
		free(b->b_handles);
	}
	free(b);
}
Example #13
0
void
exp_free(var_t *v)
{
	if (v == NULL)
	{
		return;
	}

	if ((v->v_flags & VF_EXP_FREE) == 0)
	{
		return;
	}

	if (v->v_type == VT_LIST && v->v_data != NULL)
	{
		ll_delete(v->v_data, (void *) exp_free);
		v->v_data = NULL;
	}

	var_delete(v);

	return;
}
Example #14
0
void
exp_delete(exp_t *exp)
{
	switch (exp->ex_type)
	{
	case EX_PARENTHESES:
		break;

	case EX_CONSTANT:
		var_delete(exp->ex_data);
		break;

	case EX_FUNCTION:
		exp_function_delete(exp);
		break;

	case EX_LIST:
		ll_delete(exp->ex_data, NULL);
		break;

	case EX_SYMBOL:
	case EX_VARIABLE:
	case EX_OPERATION:
	case EX_TERNARY_COND:
	case EX_MACRO:
		free(exp->ex_data);
		break;

	default:
		log_die(EX_SOFTWARE, "exp_delete: bad type");
	}
		
	free(exp);

	return;
}
Example #15
0
static var_t *
exp_eval_function(exp_t *exp, var_t *mailspec)
{
	exp_function_t *ef = exp->ex_data;
	acl_function_t *af;
	var_t *args = NULL;
	ll_t *single = NULL;
	var_t *v = NULL;

	af = acl_function_lookup(ef->ef_name);
	if (af == NULL)
	{
		log_error("exp_eval_function: unknown function \"%s\"",
		    ef->ef_name);
		goto error;
	}

	/*
	 * Function has arguments
	 */
	if (ef->ef_args)
	{
		args = exp_eval(ef->ef_args, mailspec);
		if (args == NULL)
		{
			log_error("exp_eval_function: exp_eval failed");
			goto error;
		}

		/*
		 * Convert single argument into list.
		 */
		if (args->v_type != VT_LIST)
		{
			single = ll_create();
			if (single == NULL)
			{
				log_error("exp_eval_function: ll_create failed");
				goto error;
			}

			if (LL_INSERT(single, args) == -1)
			{
				log_error("exp_eval_function: LL_INSERT failed");
				goto error;
			}
		}
	}
	else
	{
		/*
		 * Function has no arguments -> empty list
		 */
		single = ll_create();
		if (single == NULL)
		{
			log_error("exp_eval_function: ll_create failed");
			goto error;
		}
	}

	if (af->af_type == AF_SIMPLE)
	{
		v = exp_eval_function_simple(ef->ef_name, af,
		    single ? single : args->v_data);
	}
	else
	{
		v = exp_eval_function_complex(ef->ef_name, af,
		    single ? single : args->v_data);
	}

	if (v == NULL)
	{
		log_error("exp_eval_function: function \"%s\" failed",
		    ef->ef_name);
		goto error;
	}

error:
	if (single)
	{
		ll_delete(single, NULL);
	}

	if (args)
	{
		exp_free(args);
	}

	return v;
}
Example #16
0
/*
 * find_buf()
 *	Given starting sector #, return pointer to buf
 */
struct buf *
find_buf(daddr_t d, uint nsec, int flags)
{
	struct buf *b;

	ASSERT_DEBUG(nsec > 0, "find_buf: zero");
	ASSERT_DEBUG(nsec <= EXTSIZ, "find_buf: too big");

	/*
	 * If we can find it, this is easy
	 */
	b = hash_lookup(bufpool, d);
	if (b) {
		return(b);
	}

	/*
	 * Get a buf struct
	 */
	b = malloc(sizeof(struct buf));
	if (b == 0) {
		return(0);
	}

	/*
	 * Make room in our buffer cache if needed
	 */
	while ((bufsize+nsec) > coresec) {
		age_buf();
	}

	/*
	 * Get the buffer space
	 */
	b->b_data = malloc(stob(nsec));
	if (b->b_data == 0) {
		free(b);
		return(0);
	}

	/*
	 * Add us to pool, and mark us very new
	 */
	b->b_list = ll_insert(&allbufs, b);
	if (b->b_list == 0) {
		free(b->b_data);
		free(b);
		return(0);
	}
	if (hash_insert(bufpool, d, b)) {
		ll_delete(b->b_list);
		free(b->b_data);
		free(b);
		return(0);
	}

	/*
	 * Fill in the rest & return
	 */
	init_lock(&b->b_lock);
	b->b_start = d;
	b->b_nsec = nsec;
	b->b_locks = 0;
	b->b_handles = 0;
	b->b_nhandle = 0;
	if (flags & ABC_FILL) {
		b->b_flags = 0;
	} else {
		b->b_flags = B_SEC0 | B_SECS;
	}
	bufsize += nsec;

	/*
	 * If ABC_BG, initiate fill now
	 */
	if (flags & ABC_BG) {
		qio(b, Q_FILLBUF);
	}

	return(b);
}