E:\myprj\mylib\IntSetList.h
class IntSetList
{
public:
IntSetList(int maxelements = 0, int maxval = 0);
~IntSetList();
int size(){ return n; }
void insert(int t);
void report(int * v);
private:
int n;
struct node {
int val;
node * next;
node(int v, node * p) { val = v; next = p;}
};
node * head, * sentinel;
node * rinsert(node * p, int t);
};
E:\myprj\mylib\IntSetList.cpp
#include "IntSetList.h"
IntSetList::IntSetList(int maxelements, int maxval)
{
sentinel = head = new node(maxval, 0);
n = 0;
}
IntSetList::~IntSetList()
{
node * p;
do
{
p = head->next;
delete head;
head = p;
} while (head != sentinel);
delete head;
}
void IntSetList::insert(int t)
{
head = rinsert(head, t);
}
void IntSetList::report(int * v)
{
int j = 0;
node * p;
for (p = head; p != sentinel; p = p->next)
{
v[j++] = p->val;
}
}
IntSetList::node * IntSetList::rinsert(node * p, int t)
{
if (p->val < t)
{
p->next = rinsert(p->next, t);
}
else if (p->val > t)
{
p = new node(t, p);
n++;
}
return p;
}
//
//测试
#include <cstdlib>
#include <iostream>
using namespace std;
static void gensets(int m, int maxval)
{
int * v = new int[m];
IntSetList s(m, maxval);
while(s.size() < m)
{
s.insert(rand()%maxval);
}
s.report(v);
for (int i = 0; i < m; i++)
{
cout << v[i] << "\n";
}
}
void testSetList(void)
{
gensets(10, 100);
}