Solution to exercise 4.1.2:
#define __MINMAX_DEFINED        // use STL's generic min and max templates

#include "list.h"               // include STL-list implementation

#include <iostream.h>

void main (void)
{
  list<int> bit_seq;            // define a list of int
  int input = 0;                // value read from cin
  int count_1 = 0;              // counter for number of 1's

  cout << "Insert values 0 and 1, another value to stop input..." << endl;

  while (cin >> input) {
	 if (!(input == 0 || input == 1)) break;
	 bit_seq.push_back (input);             // list member function push_back
  }

  // create a new list for bit_stuffing
  list<int> bit_stuffed_seq (bit_seq);

  // define loop iterators
  list<int>::iterator first = bit_stuffed_seq.begin();
  list<int>::iterator last  = bit_stuffed_seq.end();

  // bit stuff loop

  while (first != last) {
			if (*first == 0)
					 count_1 = 0;    	// reset 1's-counter
			else if (*first == 1)
					 count_1++;      	// increment number of 
                                                                //  consecutive 1's
			first++;                        // go to the next list element
			if (count_1 == 5) {
                                       // insert a 0 after the fifth consecutive 1
			  bit_stuffed_seq.insert (first, 0);
			  count_1 = 0;				// reset counter
			}
  }
}

Back to index


Johannes Weidl (J.Weidl@infosys.tuwien.ac.at) - Apr 16, 1996