SSL02
Home ] Up ]

 

//file   SLL02.cpp
//date   04/25/2005
//author aou
//csis250.tripod.com


///////////////////////////////////////////////////////////
//includes
///////////////////////////////////////////////////////////
#include <iostream>
using namespace std;


class CInfo
  {
  private:
    int value;
    CInfo *next;

  friend class CSLL;
  friend ostream & operator << (ostream & bob, const CSLL & list);
  };


class CSLL
  {
  private:
    int n;
    CInfo *first;
    CInfo *last;
    void initialize(void);
  public:
    CSLL(void);  //default constructor
    CSLL(int n); //create a list with given integer value
    friend ostream & operator << (ostream & bob, const CSLL & list);
    CSLL(char ch); //constructs based on value of ch
    CSLL(int low, int high);
    CSLL(int low, int high, int n);
    CSLL(const CSLL &list);
    void input(void);
    bool insert(int x);
    bool insert(const CSLL & list2);
    bool operator == (const CSLL &list2);
  };


void main(void)
  {
  CSLL myList;
  cout << myList << endl;
  myList.insert(666);
  cout << myList << endl;

  myList.insert(555);
  cout << myList << endl;
  }

bool CSLL::insert(int x)
  {
  /*
  set agent to available block of type CInfo
  copy x to value part of new block
  set next part of new block to NULL
  set first to agent
  set last to agent
  */
  CInfo *agent;
  agent = new CInfo;
  agent->value = x;
  agent ->next = NULL;
  if (n == 0)
    {
    first = agent;
    last  = agent;
    }
  else
    {
    last->next = agent;
    last = agent;
    }


  this->n++;
  return true;
  }


ostream & operator << (ostream & bob, const CSLL & list)
  {
  bob << "SLL(" << list.n << "): ";

  CInfo *agent;

  agent = list.first;

  while (agent != NULL)
    {
    bob << agent->value << ' ';
    agent = agent->next;
    }

  return bob;
  }


CSLL::CSLL(void)
  {
  this->initialize();
  }

void CSLL::initialize(void)
  {
  //n = 0;
  //(*this).n = 0;
  this->n = 0;
  this->first = NULL;
  this->last = NULL;
  }