submit100 link solution
Be sure to include your name, the time you spent, and a list
of collaborators in the file solution (there's no need for
a separate README file).
In all these problems assume that type Node exists as shown
below.
struct Node
{
string info;
Node * next;
Node(const string & s, Node * follow = 0){ // constructor
info = s;
next = follow;
}
};
length() . as in
{\tt str.length()} that is supplied with the class string .
You should write this function both recursively and iteratively
(non-recursively).
int TotalChars(Node * list)
// precondition: list has no header node
// postcondition: returns total of chars in list
("apple","cat","xray") becomes
("apple","apple","cat","cat","xray","xray") when it's doubled.
void Double(Node * list)
// precondition: list = a1, a2, ... an
// postcondition: list = a1, a1, a2, a2, ..., an, an
rev = NULL . since
initially no nodes have been reversed and all nodes need to be reversed.
void Reverse(Node * & list)
//precondition: list represents the list \(a\sb{1},a\sb{2},\ldots a\sb{n}\)
//postcondition: list represents \(a\sb{n},\ldots a\sb{2},a\sb{1}\)
Pictorial invariant for linked-list reversal.
rev list
/ \
/ \
+---+ +---+ +---+ +---+ +---+ +---+
||<--| |<--| |<--| | | |-->| |-->| |-->||
+---+ +---+ +---+ +---+ +---+ +---+
nodes reversed so far nodes needing to be reversed
Remove(list,"big") removes all
nodes containing the word "big" from list .
You should write this function both iteratively and recursively.
ListSplit(list,sub1,sub2);
will create new lists pointed to by parameters sub1, and sub2 from the
list initially referenced by parameter list. After ListSplit
is called, the first parameter should be NULL, i.e., new nodes are NOT
created, but are redistributed evenly between the second and third
parameters. It doesn't matter how you divide the nodes between the two
lists.
The prototype for the function is:
void ListSplit(Node * & list, Node * & first, Node * & second);
(extra credit if you develop a useful invariant for your code)