打印链表L中由链表P的元素指定位置的值


给你一个链表L和另一个链表P,它们包含以升序排序的整数。操作PrintLots(L,P)将打印L中那些由P所指定的位置上的元素。

例如,如果p = 1,3,4,6,那么,L中的第1,3,4,6个元素被打印出来。写出过程PrintLots(L,P)。你应该只使用基本的表操作。

该过程的运行时间是多少? 最大为 L元素个数+P元素个数  个时间单位,最少为L元素个数时间单位

#include 
#include 
typedef int ElementType;
#include "_List.h"
#include "_List.c"

void PrintLots( List L, List P );
void PrintLots( List L, List P )
{
	int i = 0;
	ElementType E;
	Position TempL, TempP;
	TempL = L;
	TempP = P;
	
	while( TempL->Next != NULL && TempP->Next != NULL )
	{
		E = TempP->Next->Element;
		TempL = TempL->Next;
		i++;
		if( E == i )
		{
			printf( "%d ", TempL->Element );
			TempP = TempP->Next;
		}
	}
}

int main()
{
	List L, P;
	L = CreateList();
	P = CreateList();
	Insert( 5, P, P );
	Insert( 3, P, P );

	Insert( 10, L, L );
	Insert( 6, L, L );
	Insert( 5, L, L );
	Insert( 3, L, L );
	Insert( 1, L, L );
	
	PrintLots( L, P );
	return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注