Beispiel #1
0
void test1(LinkList *L){
	node* p = (node*)calloc(1, sizeof(node));
	insert_first(L, 15);
	insert_first(L, 13);
	insert_first(L, 10);
	insert_first(L, 7);
	insert_first(L, 6);
}
Beispiel #2
0
	int
main(int argc, char *argv[])
{
	int i;

	struct node *h = NULL;
	for(i = 0; i < 10; i++)
		h = insert_last(h, i);

	print(h);

	h = reverse(h, NULL);
	print(h);

	h = reverse2(h);
	print(h);

	h = delete_first(h);
	print(h);

	h = delete_last(h);
	print(h);

	h = insert_first(h, 11);
	print(h);

	h = insert_last(h, 13);
	print(h);

	h = insert_after(h, newnode(14), 13);
	print(h);

	return 0;
}
Beispiel #3
0
int main() 
{
	list_t* head = new_list();

	insert_first(head, 3);
	insert_first(head, 3);
	insert_first(head, 3);
	int i;
	for (i = 0; i < 3; ++i)
	{
		printf("%d\n", head->succ->data);
	}

	free_list(head);

	return 0;
}
Beispiel #4
0
void CHSInterface::LinkExitToRoom(HS_DBREF dbExit, HS_DBREF dbRoom)
{
#ifdef PENNMUSH                 // No change in code between versions
    Exits(dbExit) = dbRoom;
    PUSH(dbExit, Exits(dbRoom));
#endif

#if defined(TM3) || defined(MUX)
    s_Exits(dbRoom, insert_first(Exits(dbRoom), dbExit));
    s_Exits(dbExit, dbRoom);
    s_Location(dbExit, NOTHING);
    link_exit(GOD, dbExit, dbRoom);
#endif

}
Beispiel #5
0
void move_object(dbref thing, dbref dest)
{
	dbref src;

	/*
	 * Remove from the source location 
	 */

	src = Location(thing);
	if(src != NOTHING)
		s_Contents(src, remove_first(Contents(src), thing));

	/*
	 * Special check for HOME 
	 */

	if(dest == HOME)
		dest = Home(thing);

	/*
	 * Add to destination location 
	 */

	if(dest != NOTHING)
		s_Contents(dest, insert_first(Contents(dest), thing));
	else
		s_Next(thing, NOTHING);
	s_Location(thing, dest);

	/*
	 * Look around and do the penny check 
	 */

	look_in(thing, dest, (LK_SHOWEXIT | LK_OBEYTERSE));
	if(isPlayer(thing) && (mudconf.payfind > 0) &&
	   (Pennies(thing) < mudconf.paylimit) && (!Controls(thing, dest)) &&
	   ((random() % mudconf.payfind) == 0)) {
		giveto(thing, 1);
		notify_printf(thing, "You found a %s!", mudconf.one_coin);
	}
}
Beispiel #6
0
/* actual insert: returns true -> insert ok or key found, false -> alloc failure */
bool cbtree_insert(struct CBTree *tree, void *obj)
{
	const void *key, *old_key;
	unsigned newbit, klen, old_klen;
	void *old_obj;

	if (!tree->root)
		return insert_first(tree, obj);

	/* current key */
	klen = get_key(tree, obj, &key);

	/* nearest key in tree */
	old_obj = raw_lookup(tree, key, klen);
	old_klen = get_key(tree, old_obj, &old_key);

	/* first differing bit is the target position */
	newbit = find_crit_bit(key, klen, old_key, old_klen);
	if (newbit == SAME_KEY)
		return true;
	return insert_at(tree, newbit, key, klen, obj);
}
main()
{
int ch;
do
{
printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_first();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("\n\nInvalid choice. Please try again.\n");
}
} while (1);
}
void test_object() {
	printf("%s start.\n", __func__);
	create();
	
	insert(0, &arr_user[0]);
	insert_first(&arr_user[1]);
	insert_last(&arr_user[2]);
	insert(1, &arr_user[3]);
	
	printf("object is_empty=%d\n", is_empty());
	printf("object size=%d\n", size());
	
	int i;
	user *p;
	int len = size();
	for(i=0; i<len; i++) {
		p = (user*)get(i);
		printf("string get(%d)=[%d, %s]\n", i, p->id, p->name);
	}
	
	destory();
	printf("%s finish.\n", __func__);
}
void test_int() {
	printf("%s start.\n", __func__);
	int arr[4] = {1, 2, 3, 4};
	create();
	insert(0, &arr[0]);
	insert_first(&arr[1]);
	insert_last(&arr[2]);
	insert(1, &arr[3]);
	
	printf("int is_empty=%d\n", is_empty());
	printf("int size=%d\n", size());
	
	// 2, 4, 1, 3
	int i;
	int *p;
	int len = size();
	for(i=0; i<len; i++) {
		p = (int*)get(i);
		printf("int get(%d)=%d\n", i, *p);
	}
	destory();
	printf("%s finish.\n", __func__);
}
void test_string() {
	printf("%s start.\n", __func__);
	char* arr = {"one", "two", "three", "four"};
	create();
	insert(0, &arr[0]);
	insert_first(&arr[1]);
	insert_last(&arr[2]);
	insert(1, &arr[3]);
	
	printf("string is_empty=%d\n", is_empty());
	printf("string size=%d\n", size());
	
	// "two", "four", "one", "three"
	int i;
	char *p;
	int len = size();
	for(i=0; i<len; i++) {
		p = (char*)get(i);
		printf("string get(%d)=%s\n", i, p);
	}
	destory();
	printf("%s finish.\n", __func__);
}
static void *coalesce(void *bp) {
    
    dbg_printf("=== Coalesce bp = 0x%lx\n", (size_t)bp);
    
    void *prevbp = PREV_BLKP(bp);
    void *nextbp = NEXT_BLKP(bp);
    
    /*ONLY use the former alloc flag  */
    size_t prev_alloc = GET_PREV_ALLOC(GET_HEADER(bp)); /*GET_ALLOC(GET_FOOTER(prevbp));*/
    size_t next_alloc = GET_ALLOC(GET_HEADER(nextbp));
    size_t bsize = GET_SIZE(GET_HEADER(bp));
    size_t flag = 0;
    
    int class_idx = 0;
    
    /* case 1: make newly freed block to be root */
    if (prev_alloc && next_alloc) {
        
        dbg_printf("Coalesce Case 1\n");
        
        insert_first(bp);
        
        return bp;
        
    }
    /* case 3: next block is free */
    else if (prev_alloc && !next_alloc) {
        
        dbg_printf("Coalesce Case 3\n");
        
        class_idx = get_class_idx_by_size(GET_SIZE(GET_HEADER(nextbp)));
        remove_free_block(nextbp, class_idx);
        
        /* Telling coalesced free block about if bp's previous allocated */
        flag = GET_PREV_ALLOC(GET_HEADER(bp)) ? 0x2 : 0x0;
        
        bsize += GET_SIZE(GET_HEADER(nextbp));
        PUT(GET_HEADER(bp), PACK(bsize, flag));
        PUT(GET_FOOTER(bp), PACK(bsize, flag));
        
        insert_first(bp);
        
        return bp;
        
    }
    /* case 2: prev block is free */
    else if (!prev_alloc && next_alloc) {
        
        dbg_printf("Coalesce Case 2\n");
        
        class_idx = get_class_idx_by_size(GET_SIZE(GET_HEADER(prevbp)));
        
        dbg_printf("class_idx = %d, class_address = 0x%lx\n", class_idx, (size_t)GET_CLASS(class_idx));
        
        remove_free_block(prevbp, class_idx);
        
        /* Telling coalesced free block about if bp's previous's previous allocated */
        flag = GET_PREV_ALLOC(GET_HEADER(prevbp)) ? 0x2 : 0x0;
        
        if (flag == 0) {
            printf("Implies fail coalese: 0x%lx with former\n", (size_t)prevbp);
            exit(2);
        }
        
        bsize += GET_SIZE(GET_HEADER(prevbp));
        PUT(GET_HEADER(prevbp), PACK(bsize, flag));
        PUT(GET_FOOTER(prevbp), PACK(bsize, flag));
        
        insert_first(prevbp);
        
        return prevbp;
    }
    /* case 4: both blocks are free */
    else {
        
        dbg_printf("Coalesce Case 4\n");
        
        class_idx = get_class_idx_by_size(GET_SIZE(GET_HEADER(nextbp)));
        remove_free_block(nextbp, class_idx);
        class_idx = get_class_idx_by_size(GET_SIZE(GET_HEADER(prevbp)));
        remove_free_block(prevbp, class_idx);
        
        /* Telling coalesced free block about if bp's previous's previous allocated */
        flag = GET_PREV_ALLOC(GET_HEADER(prevbp)) ? 0x2 : 0x0;
        
        if (flag == 0) {
            printf("Implies fail coalese: 0x%lx with former\n", (size_t)prevbp);
            exit(2);
        }
        
        bsize += GET_SIZE(GET_HEADER(nextbp));
        bsize += GET_SIZE(GET_FOOTER(prevbp));
        PUT(GET_HEADER(prevbp), PACK(bsize, flag));
        PUT(GET_FOOTER(nextbp), PACK(bsize, flag));
        
        insert_first(prevbp);
        
        return prevbp;
    }
    
    dbg_printf("Unable to coalesce!\n");
    return NULL;
}
/* Place an ADJUSTED sized block in heap */
static void place(void *bp, size_t asize) {
    
#if DEBUG
    if (NEXT_BLKP(bp)) {
        if (GET_PREV_ALLOC(GET_HEADER(NEXT_BLKP(bp)))) {
            dbg_printf("0x%lx: Fail to inform next block when free\n", (size_t)bp);
            exit(2);
        }
    }
#endif
    
    dbg_printf("=== Place, bp = 0x%lx, adjusted size = %ld \n", (size_t)bp, asize);
    
    /* block free size */
    size_t csize = GET_SIZE(GET_HEADER(bp));
    char *nextbp = NULL;
    int class_idx = 0;
    size_t flag = 0;
    
    /* Split, say, minimum block size set to 1 WSIZE = 8 byte */
    if ((csize - asize) >= (4 * WSIZE)) {
        
        class_idx = get_class_idx_by_size(GET_SIZE(GET_HEADER(bp)));
        
        /* Include previous block's information */
        flag = GET_PREV_ALLOC(GET_HEADER(bp)) ? 0x3 : 0x1;
        
        PUT(GET_HEADER(bp), PACK(asize, flag));
        PUT(GET_FOOTER(bp), PACK(asize, flag));
        
        nextbp = NEXT_BLKP(bp);
        
        PUT(GET_HEADER(nextbp), PACK((csize - asize), 0));
        PUT(GET_FOOTER(nextbp), PACK((csize - asize), 0));
        
        /* Inform the next block that this block is allocated */
        flag = GET(GET_HEADER(nextbp));
        flag |= 0x2;
        PUT(GET_HEADER(nextbp), flag);
        PUT(GET_FOOTER(nextbp), flag);
        
        split_free_block(bp, nextbp);
        remove_free_block(bp, class_idx);
        
        remove_free_block(nextbp, class_idx);
        insert_first(nextbp);
        
        mm_checkheap(CHECK_HEAP);
        
    }
    else {
        /* Include previous block's information */
        flag = GET_PREV_ALLOC(GET_HEADER(bp)) ? 0x3 : 0x1;
        
        PUT(GET_HEADER(bp), PACK(csize, flag));
        PUT(GET_FOOTER(bp), PACK(csize, flag));
        
        /* Inform the next block that this block is allocated */
        if ((size_t)bp == 0x800004980) {
            dbg_printf("bp size = %ld\n",GET_SIZE(GET_HEADER(bp)));
            dbg_printf("NEXT_BLKP(bp); 0x%lx\n",(size_t)NEXT_BLKP(bp));
        }
        nextbp = NEXT_BLKP(bp);
        if (nextbp) {
            flag = GET(GET_HEADER(nextbp));
            flag |= 0x2;
            PUT(GET_HEADER(nextbp), flag);
            /* Only put footer when next block is free */
            if (!GET_ALLOC(GET_HEADER(nextbp))) {
                PUT(GET_FOOTER(nextbp), flag);
            }
            
        }
        
        remove_free_block(bp, get_class_idx_by_size(csize));
    }
}
Beispiel #13
0
void test(LinkList *L){
	node* p = (node*)calloc(1, sizeof(node));
	for(int i=5;i>0;i--){
		insert_first(L, i);
	}
}