home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_09_10
/
9n10023a
< prev
next >
Wrap
Text File
|
1991-08-14
|
2KB
|
82 lines
/* --------------------------------------------------------------
FUNCTION REMOVE_NODE: The steps to remove a selected node are:
A. Complain if list empty.
B. Get a string from the user.
C. If no such node, complain and get out.
D. If there are duplicated decrement the count and get out.
E. If this is the only node in the list, clear root and tail
pointers.
F. Else if it's the first node in the list adjust root pointer to new
first node and new first node's bwd pointer.
G. Else if it's the last node in the list adjust tail pointer to new
last node and new last node's fwd pointer.
H. Else it's in the middle of the list so adjust its predecessor and
successor to point to each other.
I. Free the node being removed along with its string and decrement
the number in use.
-------------------------------------------------------------- */
void remove_node(void)
{
Node *ploc_node; /* ptr to located node */
char string[21]; /* tmp holder for node's string */
/*A*/ if (proot_node == NULL) {
printf("\n List contains no nodes\n");
return;
}
/*B*/ printf("\n Enter string: ");
scanf("%20s", string);
/*C*/ ploc_node = locate_node(string, EXACT);
if (ploc_node == NULL) {
printf("No such node exists\n");
return;
}
/*D*/ if (ploc_node->count > 1) {
--ploc_node->count;
printf("\n One duplicate removed");
return;
}
if (ploc_node->pbwd == NULL) {
/*E*/ if (ploc_node->pfwd == NULL) {
proot_node = ptail_node = NULL;
}
/*F*/ else {
proot_node = ploc_node->pfwd;
ploc_node->pfwd->pbwd = NULL;
}
}
else {
/*G*/ if (ploc_node->pfwd == NULL) {
ptail_node = ploc_node->pbwd;
ploc_node->pbwd->pfwd = NULL;
}
/*H*/ else {
ploc_node->pbwd->pfwd = ploc_node->pfwd;
ploc_node->pfwd->pbwd = ploc_node->pbwd;
}
}
/*I*/ free(ploc_node->pstring);
put_free_node(ploc_node);
--nodes_in_use;
printf("\n Node removed");
}