Professional Documents
Culture Documents
3.
pList is a unique pointer that is used to keep track of the head (first node) of the list. Executing the statement
pList = pList->link modifies pList to no longer point to the first node of the list. This may have devastating
effects for the entire program. This fact justifies the need for the two walking pointers, pPre and pLoc, which
keep track of the predecessor and the current nodes without altering the head pointer.
pPre->link = pCur->link
recycle (pCur)
5.
1
2
3
4
7.
list1 no longer points to the head of the first list, rather it points to the head of the second list in the figure. The
first list is lost forever.
This will create a circularly linked list.
9.
allocate (pNew)
...
pNew->link = pPre->link (pPre points to dummy node)
pPre->link = pNew
Problems
11.
13.
algorithm appendAndCount
This algorithm is basically a modified version of the Algorithm 3-16,
appendTwoLists and the build and the append algorithms are unmodified.
The whole algorithm creates two linked lists, appends the second to the
first, and displays the count of nodes in the appended list.
Pre nothing
Post creates and appends the lists,
prints final list and displays total count
1 build (pList1, file1)
2 build (pList2, file2)
3 append (pList1, pList2)
list2 is appended after list1 and list count updated
4 printList (pList1)
5 print(pList1->count)
6 return
end appendAndCount
17.
Page 3.2
// Prototype Declarations
void build_table (TPOINTER
void print_table (TPOINTER
void build_list
(POINTER
void add_node
(POINTER
bool search_list (POINTER
POINTER
POINTER
KEY_TYPE
void delete_table (TPOINTER
int main (void)
{
// Local Definitions
TPOINTER t_list =
&t_list);
t_list);
&p_list, int row);
&p_list, KEY_TYPE key);
p_list,
&p_pre,
&p_cur,
target);
&t_list);
NULL;
// Statements
build_table (t_list);
print_table (t_list);
delete_table (t_list);
}
return 0;
// main
";
Page 3.3
t_cur
} // else
t_new;
return;
// build_list
Page 3.4
while (t_walker)
{
cout << " Row " << setw(4) << row << " :
p_walker = t_walker -> row;
";
while (p_walker)
{
cout << setw (5) << p_walker -> data.key;
p_walker = p_walker -> link;
} // while p_walker
cout << endl;
t_walker = t_walker -> link;
row++;
} // while t_walker
cout << endl;
}
return;
// print_table
=
=
key;
NULL;
Page 3.5
return;
// add_node
==
target)
return found;
}
// Search_list
Page 3.6
POINTER
POINTER
TPOINTER
TPOINTER
p_walker;
p_del;
t_walker;
t_del;
// Statements
t_walker = t_list;
while (t_walker)
{
p_walker = t_walker -> row;
while (p_walker)
{
p_del
= p_walker;
p_walker = p_walker -> link;
delete (p_del);
} // while p_walker
t_del
= t_walker;
t_walker = t_walker -> link
delete (t_del);
} // while t_walker
t_list = NULL;
return;
// delete_table
19.
21.
23.
Page 3.7
if (pList->count is zero)
1 print (Error: Empty List can't be appended)
2 else
1 pred = pList->head
2 loop (pred->link not NULL)
1 pred = pred->link
3 end loop
4 pEnd = pred
5 walk = pList->head
6 loop(walk not equal pEnd)
1 success = insertNode (pList, pred, walk->data)
2 if (not success)
1 print (Overflow error in appendToItself)
2 abort program
3 end if
4 pred = pred->link
5 walk = walk->link
7 end loop
8 success = insertNode(pList, pred, walk->data)
9 if (not success)
1 print (Overflow error in appendToItself)
2 abort program
10 end if
3 end if
4 return
end appendToItself
25.
int>
int>
int>
int>
Page 3.8
(list1 , FILE_1);
(list1);
(list2 , FILE_2);
(list2);
return 0;
// main
Page 3.9
list1.addNode (item);
return;
// PrintList
algorithm merge
Page 3.10
This
end loop
Now merge pList2
6 walk = pList2.head
7 loop (walk not NULL)
1 found = mergeList._search (pPre, pLoc, walk->data.key)
2 if (found is true)
1 print (Duplicate data not merged,
pLoc->data.key)
3 else
1 mergeList._insert(pPre, walk->data)
4 end if
5 walk = walk->link
8 end loop
9 return mergeList
end merge
Page 3.11