【数据结构】顺序表练习

(注:下面是顺序表部分习题的练习,其中用到了顺序表和单链表的相关函数,读者可以参考“https://blog.csdn.net/xing1584114471/article/details/83004289”了解有关单链表的函数实现,参考“https://blog.csdn.net/xing1584114471/article/details/83655736”了解顺序表表的函数实现)

/*
(一)设顺序表va中的数据元素递增有序。试写一算法,将x插入到顺序表的适当位置上,以保持该表的有序性。
*/ 

int Insert_orderly(Psqlist list, ElemType val)
{
	assert(list != NULL);
	int i = 0;
	while( list->elem[i] < val && i < list->count)
	{
		i++;
	}
	InsertSqList(list, i , val);
	return TRUE;
}

/*
(二)试写一算法,实现顺序表的就地逆置,即利用原表的存储空间将线性表 a1……an 逆置为 an……a1
*/ 

void Inversion(Psqlist list)
{
	assert(list != NULL);
	int i = 0;
	int j = list->count - 1;
	int tmp = 0;
	while(i != j)
	{
		tmp = list->elem[i];
		list->elem[i] = list->elem[j];
		list->elem[j] = tmp;
		i++;
		j--;
	}
}

/*
  (三) 假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并成一个按元素值递减有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原表(即A表和B表)的结点空间构造C表。
*/ 

Node* Decrease_Combine(Plist list_a, Plist list_b) 
{
	assert(list_a != NULL);
	assert(list_b != NULL);

	Node *p_a = list_a->head.next;
	Node *p_b = list_b->head.next;
	Node *head_c = p_a->data > p_b->data ? &list_b->head : &list_a->head;//谁小就用谁的头
	Node *p_c = head_c;

	while( p_a  != NULL && p_b != NULL)
	{
		if(p_a->data >= p_b->data)
		{
			p_c->next = p_b;
			p_b = p_b->next;
		}
		else
		{
			p_c->next = p_a;
			p_a = p_a->next;
		}
		p_c = p_c->next;
	}
	while( p_a != NULL)
	{
		p_c->next = p_a;
		p_c = p_c->next;
		p_a = p_a->next;
	}
	while( p_b != NULL)
	{
		p_c->next = p_b;
		p_c = p_c->next;
		p_b = p_b->next;
	}

	//逆置
	struct Node *m = head_c->next;
	struct Node *f = NULL;
	struct Node *l = m->next;
	while(1)
	{
		m->next = f;
		f = m;
		m = l;
		if( m == NULL)
		{
			break;
		}
		l = l->next;
	}
	head_c->next = f;
	return head_c;
}

 /*
    (四)已知A,B和C为三个递增有序的线性表,现要求对A表作如下操作:删去那些既在B表中出现又在C表中出现的
元素。试对顺序表编写实现上述操作的算法,并分析你的算法的时间复杂度(注意:题中没有特别指明同一表中
的元素值各不相同)

*/

void Delete_Repetition(Plist list_a, Plist list_b, Plist list_c)
{
	assert(list_a != NULL);
	assert(list_b != NULL);
	assert(list_c != NULL);

	int pos = 1;
	Node *p_a = list_a->head.next;
	Node *p_b = list_b->head.next;
	Node *p_c = list_c->head.next;
	
	while( p_a != NULL && p_b != NULL && p_c != NULL)
	{
		if(p_b->data > p_c->data)
		{
			p_c = p_c->next;
			continue;
		}
		else if(p_b->data < p_c->data)
		{
			p_b = p_b->next;
			continue;
		}
		else
		{
			ElemType tmp = p_b->data;
			p_b = p_b->next;
			p_c = p_c->next;	
			while(p_a != NULL)
			{
				if(p_a->data < tmp)
				{
					p_a = p_a->next;
					pos++;		
					continue;
				}
				else if(p_a->data == tmp)
				{
					p_a = p_a->next;
					DeleteLinkList(list_a, pos);
				}
				else
				{			
					break;
				}
			}
		}		
	}
}

/*
(五)两个顺序表递增有序,执行 C=AUB,算法时间复杂度要求为O(n+m)(A,B这两个顺序表只允许遍历一遍);
*/

SqList Combine(Psqlist list_a, Psqlist list_b)
{
	assert(list_a != NULL);
	assert(list_b != NULL);

	SqList list_c;
	InitSqList(&list_c);
	int a = 0;
	int b = 0;
	int c = 0;

	while(a < list_a->count &&  b < list_b->count)
	{
		if(list_a->elem[a] <= list_b->elem[b])
		{
			InsertSqList(&list_c, c, list_a->elem[a]);
			if(list_a->elem[a] == list_b->elem[b])
			{
				b++;
			}
			a++;
		}
		else 
		{
			InsertSqList(&list_c, c, list_b->elem[b]);
			b++;
		}
		c++;
	}
	while(a < list_a->count)
	{
		InsertSqList(&list_c, c, list_a->elem[a]);
		c++;
		a++;
	}
	while(b < list_b->count)
	{
		InsertSqList(&list_c, c, list_b->elem[b]);
		c++;
		b++;
	}
	return list_c;
}

函数测试

int main()
{
/*
Combine函数测试
********************************************************
	SqList list_a;
	SqList list_b;
	InitSqList(&list_a);
	InitSqList(&list_b);

	for( int i = 0; i < 10; i++)
	{
		InsertSqList(&list_a, i, i);
		InsertSqList(&list_b, i, i * 2);
	}
	ShowSqList(&list_a);
	ShowSqList(&list_b);
	printf("***************************\n");
	SqList list_c = Combine(&list_a, &list_b);
	ShowSqList(&list_c);
********************************************************
*/

/*
Delete_Repetition函数测试
********************************************************
	Linklist list_a;
	Linklist list_b;
	Linklist list_c;
	InitLinkList(&list_a);
	InitLinkList(&list_b);
	InitLinkList(&list_c);

	for( int i = 0; i < 10; i++)
	{
		InsertLinkList(&list_a, i + 2 , i );
		InsertLinkList(&list_b, i * 2, i );
		InsertLinkList(&list_c, i + 1, i );
	}
	Show(&list_a);
	Show(&list_b);
	Show(&list_c);
	printf("***************************\n");
	Delete_Repetition(&list_a, &list_b, &list_c);
	printf("****************************\n");
	Show(&list_a);
********************************************************
*/

/*
Decrease_Combine 函数测试
********************************************************
	Linklist list_a;
	InitLinkList(&list_a);
	Linklist list_b;
	InitLinkList(&list_b);

	for( int i = 0; i < 5; i++)
	{
		InsertLinkList(&list_a, i + 1, i );
	}
	Show(&list_a);

	for( int i = 0; i < 8; i++)
	{
		InsertLinkList(&list_b, i + 3, i);
	}
	Show(&list_b);

	Node * tmp = Decrease_Combine(&list_a, &list_b);
	tmp = tmp->next;

	while(tmp != NULL)
	{
		printf("%3d",tmp->data);
		tmp = tmp->next;
	}
	printf("\n");
********************************************************
*/

/*
Inversion 函数测试
********************************************************
	SqList list;
	InitSqList(&list);
	for( int i = 0; i < 15; i++)
	{
		InsertSqList(&list, i, i);
	}
	ShowSqList(&list);
	Inversion(&list);
	ShowSqList(&list);
********************************************************
*/

/*
Insert_orderly 函数测试
********************************************************
	Insert_orderly(&list, 11);
	ShowSqList(&list);

	Insert_orderly(&list, 19);
	ShowSqList(&list);
********************************************************
*/
	return 0;
}

 

你可能感兴趣的:(数据结构,习题)