DSortedList01
Home ] Up ]

 

//DSortedList01.cpp
//date: 10/30/2002
//author: AOU


//Dynamic sorted list

////////////////////////////////////////////////////////////
// Include files
////////////////////////////////////////////////////////////
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>


////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
const int MAX_SIZE    = 10; //maximum entries allowed in a list
const int UNDEFINED   = -9999;
const int MAX_VALUE   = 5;
const int TEST_COUNT  = 20;

const int INFINITY    = 32000;


////////////////////////////////////////////////////////////
//class CNode
////////////////////////////////////////////////////////////
class CNode
  {
  private:
    int m_key;
    // int age;
    // char name[20];
    // char socsec[10];
    CNode *m_next;
  public:
    CNode(void);
    CNode(int x);
    CNode(char ch);
    void display(void);
    //...
  friend class CDSortedList;
  };


////////////////////////////////////////////////////////////
//class CDSortedList
////////////////////////////////////////////////////////////
class CDSortedList
  {
  private:
    int m_count;
    CNode *m_first;
  public:
    CDSortedList(void);
    CDSortedList(int n);
    CDSortedList(char ch);
    void display(void);
    bool insert(int x);
  };


////////////////////////////////////////////////////////////
//void main(void)
////////////////////////////////////////////////////////////
void main(void)
  {
  CNode n1;
  n1.display();
  cout << endl;

  CDSortedList myList;
  myList.display();
  }


////////////////////////////////////////////////////////////
//void CDSortedList::display(void)
////////////////////////////////////////////////////////////
void CDSortedList::display(void)
  {
  CNode *p;
  p = this->m_first;
  while (p != NULL)
    {
    p->display();
    p = p->m_next;
    }

  cout << endl;
  }


////////////////////////////////////////////////////////////
//CDSortedList::CDSortedList(void)
////////////////////////////////////////////////////////////
CDSortedList::CDSortedList(void)
  {
  /*
  create two nodes 
    one pointed to by p1, 
    other pointed to by p2

  make first point to p1
  make p1 point to p2
  make p2 point to nothing

  put -infin in p1 node
  put +infin in p2 node

  set count = 0
  */
  CNode *p1 = new CNode;
  CNode *p2 = new CNode;

  this->m_first = p1;
  p1->m_next = p2;
  p2->m_next = NULL;

  p1->m_key = -INFINITY;
  p2->m_key = +INFINITY;
  }


////////////////////////////////////////////////////////////
//CNode::CNode(void)
////////////////////////////////////////////////////////////
CNode::CNode(void)
  {
  this->m_key = UNDEFINED;
  this->m_next = NULL;
  }


////////////////////////////////////////////////////////////
//void CNode::display(void)
////////////////////////////////////////////////////////////
void CNode::display(void)
  {
  cout << '(' << this->m_key << " ->" 
    << this->m_next << ") ";
  }


/*
SAMPLE RUN:
(-9999 ->0x00000000)
(-32000 ->0x00320B00) (32000 ->0x00000000)
Press any key to continue
*/