【PTA] 作者 李祥单位 湖北经济学院6-1到6-12 顺序表

6-1 顺序表 - 3. 创建线性表

void AListCreate(ALIST *list){
    list->element=NULL;
    list->element=(LELEMENT*)malloc(AListInitSize*sizeof(LELEMENT));
    list->size = AListInitSize;
    list->length = 0;
}

6-2 顺序表 - 4. 销毁线性表

void AListDestroy(ALIST *p)
{
    free(p->element);
    p->element = NULL;
    p->size = 0;
    p->length = 0;
}

6-3 顺序表 - 12. 线性表长度

int AListLength(const ALIST *list){

    int length;
    length=list->length;
    return length;
}

6-4 顺序表 - 6. 插入元素

void AListInsert(ALIST *list, int index, const LELEMENT *element)
{
    
    if(index < 1 || index > list->length+1)
    {
        printf("Incorrect index!\n");
        return;
    }
    
    if(list->length == list->size)
    {
        AListResize(list,list->size+1);
    }
    
    for(int i = list->length;i >= index;i--)
    {
        list->element[i] = list->element[i-1];
    }
    list->element[index-1]=*element;
    list->length++;
}

6-5 顺序表 - 8. 删除元素

void AListRemove(ALIST *list, int index, LELEMENT *element)
{
	int i;
	LELEMENT x = list->element[index - 1];
	if (index <= list->length && index > 0)
	{
		for (i = index - 1; i < list->length - 1; i++)
		{
			list->element[i] = list->element[i + 1];
		}
		list->length--;
		*element = x;
	}
	else
	{
		printf("Incorrect index!\n");
	}
}

6-6 顺序表 - 9. 清空线性表

void AListClear(ALIST *list)
{
 
    list->length = 0;
}

6-7 顺序表 - 7. 输出线性表

void AListOutput(const ALIST *list)
{
	int i;
	putchar('(');
	for (i = 0; i < list->length; i++)
	{
		if (i)
		{
			putchar(',');
		}
		putchar(' ');
		LElementOutput(&list->element[i]);
	}
	putchar(' ');
	putchar(')');
}

6-8 顺序表 - 10. 输入线性表

void AListInput(ALIST *list)
{
	char c;
	LELEMENT element;
	AListClear(list);
	scanf(" (");
	while (scanf(" %c", &c),c != ')')
	{
		ungetc(c, stdin);
		if (list->length)
			scanf(",");
		LElementInput(&element);
		if (list->length == list->size)
			AListResize(list ,list->size + 10);
		list->element[list->length] = element;
		++list->length;
	}
}

6-9 顺序表 - 14. 读取元素

void AListGet(const ALIST *list, int index, LELEMENT *element)
{
	if (index <= list->length && index > 0){
    	*element = list->element[index - 1];
    }
	else{
    	printf("Incorrect index!\n"); 
    }
}

6-10 顺序表 - 19. 查找元素

int AListFind(const ALIST *list, const LELEMENT *element){
    if(list->length == 0) return 0;
    
    for(int i = 0; i < list->length; i++){
        if(LElementEq(&list->element[i],element) > 0){
            return i+1;
        }
    }
    return 0;
}

6-11 顺序表 - 20. 线性表逆置

void AListReverse(ALIST *list){
    int left = 0;
    int right = list->length-1;
    
    while(left <= right){
        LElementSwap(&list->element[left],&list->element[right]);

        left++;
        right--;
    }
}

6-13 顺序表 - 24. 简单合并



ALIST* AListMerge(ALIST *dst, const ALIST *src1, const ALIST *src2){
    int i = 0;
    int j = 0;
    dst->length = 0;
    
    int h = src1->length + src2->length;
    
    dst->element = (LELEMENT*)malloc(h*sizeof(LELEMENT));
    while(i < src1->length){
        dst->element[dst->length] = src1->element[i];
        i++;
        dst->length++;
        
        if(dst->length == dst->size)
        {
            AListResize(dst,dst->size+1);
        }
    }
    
    while(jlength){
        dst->element[dst->length] = src2->element[j];
            j++;
            dst->length++;
        
        if(dst->length == dst->size)
        {
            AListResize(dst,dst->size+1);
        }
    }
   /*
   while(h < src1->length +  src2->length){
        if(h < src1->length){
            dst->element[h] = src1->element[i];
            i++;
            h++;
        }else{
            dst->element[h] = src2->element[j];
            j++;
            h++;
        }
    }*/
    
    //dst->length = src1->length + src2->length;
    
    return dst;
}

你可能感兴趣的:(pta,java,开发语言)