数据结构与算法分析(C语言描述)(十一)第三章课后习题

3.2.The comments for Exercise 3.4 regarding the amount of abstractness used apply here. The running time of the procedure in Fig. 3.1 is OO(LO + PO). 

void PrintLots(List L, List P) 
{
	int Counter;
	Position Lpos, Ppos;

	Lpos = First(L);
	Ppos = First(P);
	Counter = 1;
	while (Lpos != NULL && Ppos != NULL) 
	{ 
		if (Ppos->Element == Counter++) 
		{ 
			printf("%? ", Lpos->Element);
			Ppos = Next(Ppos, P);
		} 
		Lpos = Next(Lpos, L);
	}
}

3.3

(a) For singly linked lists

/* BeforeP is the cell before the two adjacent cells that are to be swapped. */ /* Error checks are omitted for clarity. */
void SwapWithNext(Position BeforeP, List L) 
{
	Position P, AfterP;

	P = BeforeP->Next;
	AfterP = P->Next;/* Both P and AfterP assumed not NULL. */
	P->Next = AfterP->Next;
	BeforeP->Next = AfterP;
	AfterP->Next = P;
}

(b) For doubly linked lists

/* P and AfterP are cells to be switched. Error checks as before. */
void SwapWithNext(Position P, List L) 
{
	Position BeforeP, AfterP;

	BeforeP = P->Prev;
	AfterP = P->Next;

	P->Next = AfterP->Next;
	BeforeP->Next = AfterP;
	AfterP->Next = P;
	P->Next->Prev = P;
	P->Prev = AfterP;
	AfterP->Prev = BeforeP;
}

3.4 IntersectO is shown on page 9.
/* This code can be made more abstract by using operations such as */

/* Retrieve and IsPastEnd to replace L1Pos->Element and L1Pos != NULL. */

/* We have avoided this because these operations were not rigorously defined. */
 

/* This code can be made more abstract by using operations such as */ /* Retrieve and IsPastEnd to replace L1Pos->Element and L1Pos != NULL. */ /* We have avoided this because these operations were not rigorously defined. */
List Intersect(List L1, List L2) 
{
	List Result;
	Position L1Pos, L2Pos, ResultPos;

	L1Pos = First(L1);
	L2Pos = First(L2);
	Result = MakeEmpty(NULL);
	ResultPos = First(Result);
	while (L1Pos != NULL && L2Pos != NULL) 
	{ 
		if (L1Pos->Element < L2Pos->Element) 
			L1Pos = Next(L1Pos, L1);
		else if (L1Pos->Element > L2Pos->Element)
			L2Pos = Next(L2Pos, L2);
		else 
		{ 
			Insert(L1Pos->Element, Result, ResultPos);
			L1 = Next(L1Pos, L1);
			L2 = Next(L2Pos, L2);
			ResultPos = Next(ResultPos, Result);
		} 
	} 
	return Result;
}

3.7

List Union(List L1, List L2) 
{
	List Result;
	ElementType InsertElement;
	Position L1Pos, L2Pos, ResultPos;

	L1Pos = First(L1);
	L2Pos = First(L2);
	Result = MakeEmpty(NULL);
	ResultPos = First(Result);
	while (L1Pos != NULL && L2Pos != NULL)
	{ 
		if (L1Pos->Element < L2Pos->Element) 
		{ 
			InsertElement = L1Pos->Element;
			L1Pos = Next(L1Pos, L1);
		} 
		else if (L1Pos->Element > L2Pos->Element) 
		{ 
			InsertElement = L2Pos->Element;
			L2Pos = Next(L2Pos, L2);
		} 
		else 
		{ 
			InsertElement = L1Pos->Element;
			L1Pos = Next(L1Pos, L1);
			L2Pos = Next(L2Pos, L2);
		} 
		Insert(InsertElement, Result, ResultPos);
		ResultPos = Next(ResultPos, Result);
	} 
	/* Flush out remaining list */
	while (L1Pos != NULL) 
	{ 
		Insert(L1Pos->Element, Result, ResultPos);
		L1Pos = Next(L1Pos, L1);
		ResultPos = Next(ResultPos, Result);
	} 
	while (L2Pos != NULL) 
	{ 
		Insert(L2Pos->Element, Result, ResultPos);
		L2Pos = Next(L2Pos, L2);
		ResultPos = Next(ResultPos, Result);
	} 
	return Result;
}

3.12 Reversal of a singly linked list can be done nonrecursively by using a stack, but this requires OO(NO) extra space. The solution in Fig. 3.5 is similar to strategies employed in garbage collection algorithms. At the top of the whileO loop, the list from the start to PreviousPosO is already reversed, whereas the rest of the list, from CurrentPosO to the end, is normal. This algorithm uses only constant extra space.

/* Assuming no header and L is not empty. */
List ReverseList(List L) 
{
	Position CurrentPos, NextPos, PreviousPos;

	PreviousPos = NULL;
	CurrentPos = L;
	NextPos = L->Next;
	while (NextPos != NULL) 
	{ 
		CurrentPos->Next = PreviousPos;
		PreviousPos = CurrentPos;
		CurrentPos = NextPos;
		NextPos = NextPos->Next;
	} 
	CurrentPos->Next = PreviousPos;
	return CurrentPos;
}

3.16

/* Array implementation, starting at slot 1 */
Position Find(ElementType X, List L) 
{
	int i, Where;

	Where = 0;
	for (i = 1;i < L.SizeOfList;i++) 
		if (X == L[i].Element) 
		{ 
			Where = i;
			break;
		}
	if (Where) /* Move to front. */ 
	{ 
		for (i = Where;i > 1;i--)
			L[i].Element = L[i - 1].Element;
		L[1].Element = X;
		return 1;
	}
	else 
		return 0;/* Not found. */
}

3.22

/* Assuming a header. */
Position Find(ElementType X, List L)
{
	Position PrevPos, XPos;

	PrevPos = FindPrevious(X, L);
	if (PrevPos->Next != NULL) /* Found. */ 
	{
		XPos = PrevPos->Next;
		PrevPos->Next = XPos->Next;
		XPos->Next = L->Next;
		L->Next = XPos;
		return XPos;
	}
	else 
		return NULL;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值