submit100 link solution.cc README
(you may need to use ~ola/bin/submit100 if your path doesn't
have my bin in it).
Be sure to include your name, the time you spent, and a list of collaborators in the README file, and the solutions in the file solution.cc.
In all these problems assume that types Node and TNode exist as shown below.
struct Node
{
string info;
Node * next;
Node(const string & s, Node * follow = 0) // constructor
{
info = s;
next = follow;
}
};
template <class Kind>
struct TNode
{
Kind info;
TNode<Kind> * next;
TNode(const Kind & k, TNode<Kind> * follow = 0) // constructor
{
info = k;
next = follow;
}
};
Problem 1: Counting You should write this function both recursively and iteratively (non-recursively).
int TotalChars(Node * list, char ch)
// postcondition: returns # of occurrences of ch in all nodes of list
Problem 2: Replicate
("apple","cat","xray")
Then the call Replicate(list,3)
should change list as shown below.
("apple","apple","apple","cat","cat","cat","xray","xray","xray")
Note that list is not passed by reference.
void Replicate(Node * list,int count)
// precondition: list = a1, a2, ... an
// 1 < count
// postcondition: list = a1, a1, ... a1, ... an, an, ... an
// where each element appears count times
Problem 3: Reverse
void Reverse(Node * & list)
//precondition: list is represented by a1, a2, a3, ..., an
//postcondition: list is represented by an, ..., a3, a2, a1
Here's the invariant. The picture should be "true" each time the loop
test is evaluated.
Problem 4: Remove TNode<string> * slist;The call Remove(ilist,33) should remove all nodes containing 33 from a list whose first nodes is pointed to by ilist defined as
TNode<int> * ilist;
Write Remove both iteratively and recursively.
template <class Kind> void Remove(TNode<Kind> * & list,const Kind & deadValue) // postcondition: all nodes containing deadValue have been removed // from list
Problem 5: Split++
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
has executed, the first parameter should be 0/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:
template <class Kind>
void ListSplit(TNode<Kind> * & list,
TNode<Kind> * & first, TNode<Kind> * & second);
(you should write a postcondition for the function)