2_Answers_LL_15

# 2_Answers_LL_15 - data Assume the list has more than two...

This preview shows pages 1–3. Sign up to view the full content.

CIS 15BG Answers Chapter 15 LINKED LISTS 1. Which one of the following fragments of code is incorrect? A Why? If pCur is NULL, (*pCur)->data.key means (*NULL)->data.key NULL cannot be de-referenced! 2. Imagine we have a linked list as shown below. The node has two fields: num , an integer, and link , a pointer to the next node. The list is not sorted and contains 4 integers: 50, 25, 90, and 75. Write the definition of a function that calculates and returns the sum of the numbers in the list. int calcSum( NODE *pList ) { int sum = 0; NODE *pW = pList; while( pW ) // while( pW != NULL ) { sum += pW->num; pW = pW->link; } return sum; } 1 50 25 90 75 pList

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CIS 15BG Answers Chapter 15 LINKED LISTS 3. Imagine we have a linked list as shown below. The node has two fields: num , an integer, and link , a pointer to the next node. (A). Write a function that swaps the first and the second node in the list (do not swap
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: data). Assume the list has more than two nodes. HINT: define two local pointer variables, p1 and p2, and initialize them to point to the first and second node respectively. // Solution 1: using two local variables NODE *swap1and2( NODE *pList ) { NODE *p1; NODE *p2; p1 = pList; p2 = pList->link; p1->link = p2->link; p2->link = p1; return p2 ; // the first node in the list } // Solution 2: using only one local variable NODE *swap1and2( NODE *pList ) { NODE *p2; p2 = pList->link; pList->link = p2->link; p2->link = pList; return p2; // the first node in the list } 2 pList pList CIS 15BG Answers Chapter 15 LINKED LISTS (B). Show how would you call it from main() NODE *swap1and2( NODE *pList ); int main( void ) { NODE *pList; // assume a buildList function is called here pList = swap1and2( pList ); return 0; } 3...
View Full Document

{[ snackBarMessage ]}

### Page1 / 3

2_Answers_LL_15 - data Assume the list has more than two...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online