Пример #1
0
int main(int argc, const char * argv[]) {
    Teacher teacher1 = {11, "Mr Wang", 'M'};
    Teacher teacher2 = {22, "Mr Jiang", 'M'};
    Teacher teacher3 = {33, "Mr Huang", 'M'};
    Teacher teacher4 = {44, "Mr Si", 'M'};
    Teacher teacher5 = {55, "Mr Wu", 'M'};

    SeqList *list = SeqList_Create(100);
    if (!list) return -1;
    //Insert
    SeqList_Insert(list, &teacher1, 0);
    SeqList_Insert(list, &teacher2, 0);
    SeqList_Insert(list, &teacher3, 0);
    SeqList_Insert(list, &teacher4, 0);
    SeqList_Insert(list, &teacher5, 0);
    //print
    for (int i = 0; i < SeqList_Length(list); i ++) {
        Teacher *teacher = (Teacher*)SeqList_Get(list, i);
        printf("ListNode %d name:%s\n", i, teacher->name);
    }
    //delete
    SeqList_Delete(list, 3);
    SeqList_Delete(list, 5);
    //print
    for (int i = 0; i < SeqList_Length(list); i ++) {
        Teacher *teacher = (Teacher*)SeqList_Get(list, i);
        printf("ListNode %d name:%s\n", i, teacher->name);
    }
    SeqList_Clear(list);
    SeqList_Destroy(list);
    return 0;
}
Пример #2
0
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
    if (list == NULL||node == NULL) return -1;
    TSeqList *tmp = (TSeqList *)list;
    if (pos < 0 || pos > SeqList_Length(tmp) || SeqList_Length(tmp) >= SeqList_Capacity(tmp)) return -2;
    int i;
    for (i = SeqList_Length(tmp); i > pos; i --) {
        tmp->node[i] = tmp->node[i-1];
    }
    tmp->node[i] = node;
    tmp->length++;
    return 0;
}
Пример #3
0
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
    if (list == NULL) return NULL;
    TSeqList *tmp = (TSeqList *)list;
    if (pos < 0 || pos > SeqList_Length(tmp)) return NULL;
    SeqListNode *deletenode = tmp->node[pos];
    int i;
    for (i = pos; i < SeqList_Length(tmp)-1; i ++) {
        tmp->node[i] = tmp->node[i+1];
    }
    tmp->length--;
    return deletenode;
}
Пример #4
0
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
    if (list == NULL) return NULL;
    TSeqList *tmp = (TSeqList *)list;
    if (pos < 0 || pos > SeqList_Length(tmp)) return NULL;
    return tmp->node[pos];
}
Пример #5
0
int main()
{
	int ret = 0, i = 0;
	SeqList* list = NULL;
	Teacher* tmp = NULL;

	Teacher t1, t2, t3;
	t1.age = 31;
	strcpy(t1.name, "tbf1");
	t2.age = 32;
	strcpy(t2.name, "tbf2");
	t3.age = 33;
	strcpy(t3.name, "tbf3");

	list = SeqList_Create(10);	

	SeqList_Insert(list, (SeqListNode *)&t1, 0);
	SeqList_Insert(list, (SeqListNode *)&t2, 0);
	SeqList_Insert(list, (SeqListNode *)&t3, 1);

	for(i = 0; i < SeqList_Length(list); i++)
	{
		tmp = (Teacher *)SeqList_Get(list, i);
		if(tmp != NULL)
		{
			printf("%s is %d\n", tmp->name, tmp->age);
		}
	}

	SeqList_Destroy(list);

	return 0;
}
Пример #6
0
int main(int argc, char *argv[])
{
    BTree* tree = BTree_Create();
    BTreeNode* current = NULL;
    BTreeNode* p = NULL;
    SeqList* list = NULL;
    int i = 0;
    
    struct Node n1 = {{NULL, NULL}, 'A'};
    struct Node n2 = {{NULL, NULL}, 'B'};
    struct Node n3 = {{NULL, NULL}, 'C'};
    struct Node n4 = {{NULL, NULL}, 'D'};
    struct Node n5 = {{NULL, NULL}, 'E'};
    struct Node n6 = {{NULL, NULL}, 'F'};
    
    BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0);
    BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0);
    BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0);
    BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0);
    BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0);
    BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0);
    
    printf("Full Tree: \n");
    
    BTree_Display(tree, printf_data, 4, '-');
    
    printf("Thread via List:\n");
    
    list = SeqList_Create(BTree_Count(tree));
    
    thread_via_list(BTree_Root(tree), list);
    
    for(i=0; i<SeqList_Length(list); i++)
    {
        printf("%c, ", ((struct Node*)SeqList_Get(list, i))->v);
    }
    
    printf("\n");
    
    printf("Thread via Left:\n");
    
    current = BTree_Root(tree);
    
    thread_via_left(current, &p);
    
    while( current != NULL )
    {
        printf("%c, ", ((struct Node*)current)->v);
        
        current = current->left;
    }
    
    printf("\n");
    
    BTree_Destroy(tree);
    
	return 0;
}
Пример #7
0
void thread_via_list(BTreeNode* root, SeqList* list)
{
    if( (root != NULL) && (list != NULL) )
    {
        SeqList_Insert(list, (SeqListNode*)root, SeqList_Length(list));
        
        thread_via_list(root->left, list);
        thread_via_list(root->right, list);
    }
}
void print_list(SeqList* list)
{
    int i = 0;
    
    for(i=0; i<SeqList_Length(list); i++)
    {
        printf("%d, ", (int)SeqList_Get(list, i));
    }
    
    printf("\n");
}
int main (void)
{	
		SeqList* list;
		int capacity = 10;
		int array[10] = {1,2,3,4,5,6,7,8,9,10};
		int i = 0;

		list = SeqList_Create(capacity);
		printf("list was empty,length=%d\n",SeqList_Length(list));
		for (i=0; i<10; i++)
		{
				SeqList_Insert(list, &array[i], 0);
				printf("Insert node,length=%d\n",SeqList_Length(list));
		}
		SeqListNode* delnode = SeqList_Delete(list, 4);
		printf("--------del node elem=%d\n",*(int*)delnode);
		SeqList_print(list);
		SeqList_clear(list);
		printf("list was empty,length=%d\n",SeqList_Length(list));
		SeqList_Destory(list);
}
int dynamic_search(SeqList* list, int key)
{
    int ret = -1;
    int i = 0;
    
    for(i=0; i<SeqList_Length(list); i++)
    {
        if( (int)SeqList_Get(list, i) == key )
        {
            ret = i;
            
            SeqList_Delete(list, i);
            
            break;
        }
    }
    
    return ret;
}
int SeqQueue_Length(SeqQueue* queue) // O(1)
{
    return SeqList_Length(queue);
}
int SeqQueue_Append(SeqQueue* queue, void* item) // O(1)
{
    return SeqList_Insert(queue, item, SeqList_Length(queue));
}