Mobile Apps with Qt: Rui's solution to lab3

From CSclasswiki
Jump to: navigation, search

testList.cpp

// testList.cpp
// Rui Huang
// simple program to test our linked list
//
#include <iostream>
#include "intSLList.h"

using namespace std;

int main() {
  intSLList L;
  
  //--- test empty list ---
  cout << "List should be empty.  L = {" << L << "}\n";

  //--- add a few numbers to the list ---
  L.addToTail( 100 );
  L.addToTail( 11 );
  L.addToTail( 12 );
  //--- display List back ---                                                 
  cout << "List = " << L << endl;

  // Experiment #2
  for (int i = 0; i < 10; i++) {
    L.addToTail(i);
  }
  int size2 = L.getSize();
  cout << "List after 10 insertions = " << L << endl;
  cout << "List Size: " << size2 << endl;

  // Experiment #4
  while (L.getSize() != 0) {
    L.deleteFromTail();
  }
  cout << "List after deletion from the tail = " << L << endl;

  // Experiment #3-- deletion
  //while (L.getSize() != 0) {
  //L.deleteFromHead();
  //}
  //cout << "List after deletion from the head = " << L << endl;

  // Experiment #3 -- part2
  for (int j = 0; j < 1000000; j++) {
    L.addToTail(j);
  }
  cout << "Inserted 1,000,000 integers" << endl;
  int size3_1 = L.getSize();
  cout << "List Size: " << size3_1 << endl;
  while (L.getSize() != 0) {
    L.deleteFromHead();
  }
  cout << "Deleted 1,000,000+ integers" << endl;


  return 0;
}

testList2.cpp

// testList2.cpp
//
#include <iostream>
#include "intSLList.h"

using namespace std;

int main() {
  intSLList L;   // create a list object

  //--- read integers from the keyboard ---
  cout << "Enter a list of positive integers.  Enter -1 to stop\n";
  int x;
  do {
    cin >> x;
    if ( x >= 0 ) {
      // cout << x << " ";
      // Experiment #7
      L.addToTail(x);
    }
  } while ( x != -1 );
  cout << "List = " << L;
  cout << endl;

  intSLList L2;
  int min = 1000000;
  while (!L.isEmpty()) {
    int x = L.deleteFromHead();
    L2.addToTail(x);
    if (x < min) {
      min = x;
    }
  }
  cout << "Results: ";
  while (!L2.isEmpty()) {
    int x = L2.deleteFromHead();
    cout << x-min << " ";
  }
  cout << endl;
}