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;
}