Solutions to Chapter 15

Review Questions
1. a. True
3. a. True
5. a. the order structure of elements
7. b. last in-first-out.
9. a. anywhere in the list
11. d. none of the above
13. b. queue
15. a. pop
17. d. dequeue
19. c. data structure

Exercises
21. At the beginning of the search, pCur is pointing the first node and pPre is null.
So, the second statement (pPre = pPre->link) creates a run-time error.
The correct code is:
pPre = pCur;
pCur = pCur->link;

23. The code to delete a node in the middle of a list is shown below/ Since this is the
same code to delete from the beginning of the list, using a dummy node does
simplify the insert for a linked list by eliminating the special case of deleting the
first node.
pPre->link = pCur->link;
free (pCur);

25.
pNew->link = pPre->link;
pPre->link = pNew;

Since this is the same code as what is used to add a node at the beginning of the
list, using a dummy node can simplify the operations to a linked list by eliminating the special case of adding the first data node.
27. We append the second linked list to the first linked list.

93

7 10 14 23 33 56 66 70 80 Figure 15-4 Solution to Exercise 35 .94 Solutions to Chapter 15 29. See Figure 15-1 3 5 7 9 11 13 S1 S2 Figure 15-1 Solution to Exercise 29 31. See Figure 15-3. See Figure 15-4. 31 Q1 19 Q2 20 11 Q3 Figure 15-2 Solution for Exercise 31 33. See Figure 15-2. 5 12 9 5 9 12 9 5 12 5 12 12 5 9 9 Figure 15-3 Solution to Exercise 33 35.

// Other data goes here } DATA. traverses it.key < minPtr->key) minPtr = &(pWalker->data). pWalker = pWalker->link. } NODE. DATA* minPtr = NULL. See Figure 15-5. typedef struct { KEY_TYPE key.Problems 37. /* ================== findMinimum =============== This function accepts a linked list. } // while } // if list !empty } return minPtr. and returns the data in node with minimum key value. minPtr = &(pWalker->data). Pre pList is a pointer to a linked list Post returns pointer to data with minimum key NULL if list is empty */ DATA* findMinimum (NODE* pList) { // Local Declrations NODE* pWalker. // findMinimum 95 . typedef struct nodeTag { DATA data. while (pWalker) { if (pWalker->data. 7 10 14 23 33 56 44 66 50 70 80 Figure 15-5 Solution to Exercise 37 Problems 39. // Statements if (pList) { pWalker = pList. struct nodeTag* link. Program 15-1 Solution to Problem 39 // Global Declarations typedef int KEY_TYPE. See Program 15-1.

pCur = pCur->link. // Other data goes here } DATA. /* =================== deleteAfter ================ This function traverses a linked list and deletes all nodes that are after a node with negative key. See Program 15-3.96 Solutions to Chapter 15 41. // deleteAfter 43. free (pCur). free (pCur). struct nodeTag* link. } // if } // while return pList. Program 15-3 Solution to Problem 43 /* ==================== deleteNode =================== This function deletes a single node from the link designed with a dummy node. typedef struct nodeTag { DATA data. NODE* pCur = pList. return. Pre pList is a pointer to the head (dummy) node pPre points to node before the delete node pCur points to the node to be deleted Post deletes and recycles pCur */ void deleteNode (NODE* pList. } NODE. typedef struct { KEY_TYPE key. NODE* pPre. NODE* pCur) { // Statements pPre->link = pCur->link.key < 0 && pCur) { pPre->link = pCur->link. Program 15-2 Solution to Problem 41 // Global Declarations typedef int KEY_TYPE. } if (pPre->data. pCur = pPre->link. Pre pList is a pointer to a linked list Post returns pointer to revised list */ NODE* deleteAfter (NODE* pList) { // Local Declrations NODE* pPre = NULL. // Statements while (pCur) { pPre = pCur. } // deleteNode . See Program 15-2.

exit (100). Pre pList is a valid linked list Post returns a pointer to the last node */ NODE* lastNode (NODE* pList) { // Local Declrations NODE* pWalker. NODE* pWalker. Program 15-4 Solution to Problem 45 /* ===================== lastNode =================== This function returns a pointer to the last node in a linked list. See Program 15-4. if (!pNewList) pNewList = pRear = pNew. pWalker = pWalker->link. See Program 15-5 Program 15-5 Solution to Problem 47 /* ================ doubleList =============== This function appends a linked list to itself. NODE* pRear. while (pWalker) { if (!(pNew = (NODE *) malloc (sizeof (NODE)))) { printf ("\aMemory overflow in doubleList\n"). else { pRear->link = pNew. // lastNode 47. } return pWalker. Pre pList is a valid linked list Post List appended to itself */ void doubleList (NODE* pList) { // Local Declrations NODE* pNewList. } // if pNew->data = pWalker->data. while (pWalker->link != NULL) pWalker = pWalker->link. // Statements pWalker = pList. } // else not first node in new list pPre = pWalker. // Statements pWalker = pList. NODE* pNew. } // while 97 .Problems 45. NODE* pPre. pNew->link = NULL. pRear = pNew. pNewList = pPre = NULL.

if (!tempStack) return false.” 49.} bool copyStack (STACK* sourceStack. return false. } // while // Now free tempStack memory free (tempStack). data). . See Program 15-6. data)) ABORT. } // while // Now copy data to new and original stack while (tempStack->count != 0) { if (pop (tempStack. Program 15-6 Solution to 49 /* Copy the source stack to the destination stack preserving the top-to-base ordering. // list was empty to begin with else pPre->link = pNewList. } // if else ABORT. tempStack->count = 0. data)) ABORT. // doubleList NOTE To make it easier to solve the following stack problems. while (sourceStack->count != 0) { if (pop (sourceStack. &data)) push (tempStack.H. we created a header file containing the stack functions developed in the text. false if not */ // Error Abort Macro #define ABORT {free (tempStack). It is named “P15-STACK. tempStack->top = NULL. &data)) { if (!push (sourceStack.98 Solutions to Chapter 15 Program 15-5 Solution to Problem 47 (continued) } if (!pPre) . if (!push (newStack. STACK* newStack) { // Local Declarations STACK* tempStack. int data. // Statements tempStack = malloc(sizeof(STACK)). Pre sourceStack is a valid stack Post newStack is a copy of sourceStack Returns: true if successful. return. else ABORT.

if (!temp1) printf("Error allocating stack"). pop(temp2. &data1). equal = true. Program 15-7 Solution to Problem 51 /* =================== equalStack =================== This function determines if the contents of one stack are identical to that of another. // equalStack /* ================ printStack ==================== 99 . push(stack2. while (pop(stack1. See Program 15-7. data2). data1). // copyStack 51. temp2->top = NULL. temp1->count = 0. if (!temp2) printf("Error allocating stack"). if (data1 != data2) equal = false. &data1)) push(temp1. temp1->top = NULL. data1).Problems Program 15-6 Solution to 49 (continued) } return true. exit(100). free (temp2). int data1. // Statements if (stack1->count != stack2->count) return false. &data2). temp2 = malloc(sizeof(STACK)). STACK* stack2) { // Local Definitions bool equal. temp1 = malloc(sizeof(STACK)). temp2->count = 0. STACK* temp1. return equal. push(stack1. Pre stack1 and stack2 are valid stacks Post contents of stack1 and stack2 compared Return true if the stacks are equal false if the stacks are not equal */ bool equalStack (STACK* stack1. int data2. while (temp1->count != 0) { pop(temp1. &data2)) push(temp2. while (pop(stack2. data2). exit(100). STACK* temp2. } // while } free (temp1).

queue->count = 0. // Statements printf ("Top=>"). while (node) { printf ("%3d". // stackToQueue . Pre stack is a valid stack Post stack is empty--data now in queue Return address of new queue--null if failure */ QUEUE* stackToQueue (STACK* stack) { // Local Declarations QUEUE* queue. &data). Program 15-8 Solution to Problem 53 /* =================== stackToQueue =================== This algorithm creates a queue from a stack. queue->rear = NULL. if (!queue) return NULL. data)) return NULL. } // printStack NOTE To make it easier to solve the following queue problems. int data. } // while printf("<=Base\n").” 53. return. node = node->link. if (!enqueue (queue. node->data). It is named “P15-QUEUEU. Top item becomes queue front. we created a header file containing the queue functions developed in the text. top to base */ void printStack(STACK* stack) { // Local Definitions STACK_NODE* node = stack->top.H. } while (stack->count) { pop (stack. It is non-standard because it accesses the stack structures. // Statements queue = malloc (sizeof(QUEUE)).100 Solutions to Chapter 15 Program 15-7 Solution to Problem 51 (continued) A non-standard function that prints a stack. } // while return queue. queue->front = NULL. Pre stack is a valid stack Post stack data printed. See Program 15-8. base item becomes queue rear.

Problems 101 55.H. } // for return. if (num >= 0) enqueue (queue. Program 15-10 Solution to Problem 57 /* ================== countLeaves ================== Count the number of leaves in a binary tree. } // countLeaves . } // dequeueNeg NOTE To make it easier to solve the following binary tree problems. It is named “P15-TREE. Program 15-9 Solution to Problem 55 /* ==================== dequeueNeg =================== Delete queue nodes with negative integers.” 57. num). See Program 15-9. See Program 15-10. if (!root->left && !root->right) return 1. Pre queue is a queue of integers Post nodes with negative integers deleted */ void dequeueNeg (QUEUE* queue) { // Local Declarations int num. Pre tree is a pointer to root of a binary tree Post returns count of leaves */ int countLeaves (NODE* root) { // Statements if (!root) return 0. &num). i > 0. return (countLeaves(root->left) + countLeaves(root->right)). i--) { dequeue (queue. // Statements for (int i = queue->count. we created a header file containing the queue functions developed in the text.

102 Solutions to Chapter 15 .