,,,
, , , . , , , , , . , , , . - , #include. , , .
slist , , , . ent:
typedef void* ent;
ent , , . slink:
class slink {
friend class slist;
friend class slist_iterator;
slink* next;
ent e;
slink(ent a, slink* p) { e=a; next=p;}
};
ent, slist:
class slist {
friend class slist_iterator;
slink* last; // last->next -
public:
int insert(ent a); //
int append(ent a); //
ent get(); //
void clear(); //
slist() { last=0; }
slist(ent a) { last=new slink(a,0); last->next=last; }
~slist() { clear(); }
};
, , ent", . , slink" slist", .
slist . - , , , get() - . slist. , append() insert():
int slist::insert(ent a)
{
if (last)
last->next = new slink(a,last->next);
else {
last = new slink(a,0);
last->next = last;
}
return 0;
}
int slist::append(ent a)
{
if (last)
last = last->next = new slink(a,last->next);
else {
last = new slink(a,0);
last->next = last;
}
return 0;
}
ent slist::get()
{
if (last == 0) slist_handler("get fromempty list");
//
slink* f = last->next;
ent r f->e;
if (f == last)
last = 0;
else
last->next = f->next;
delete f;
return f;
}
, slist_handler. , . :
(*slist_handler)("get fromempty list");
slist::clear(), , :
void slist::clear()
{
slink* l = last;
if (l == 0) return;
do {
slink* ll = l;
l = l->next;
delete ll;
} while (l!=last);
}
slist , . , slist, slink, slist_iterator , . , :
class slist_iterator {
slink* ce;
slist* cs;
public:
slist_iterator(slist& s) { cs = &s; ce = cs->last; }
ent operator()() {
// 0
// ,
ent ret = ce ? (ce=ce->next)->e : 0;
if (ce == cs->last) ce= 0;
return ret;
}
};
slist . , void*? , slist , . C++. ; -
struct name {
char* string;
// ...
};
, . e slist", . nlist, slist:
#include "slist.h"
#include "name.h"
struct nlist : slist {
void insert(name* a) { slist::insert(a); }
void append(name* a) { slist::append(a); }
name* get() {}
nlist(name* a) : (a) {}
};
slist , . nlist - , slist. ent void*, name*, .
, :
struct classdef {
nlist friends;
nlist constructors;
nlist destructors;
nlist members;
nlist operators;
nlist virtuals;
// ...
void add_name(name*);
classdef();
~classdef();
};
:
void classdef::add_name(name* n)
{
if (n->is_friend()) {
if (find(&friends,n))
error("friend redeclared");
else if (find(&members,n))
error("friend redeclared as member");
else
friends.append(n);
}
if (n->is_operator()) operators.append(n);
// ...
}
is_iterator() is_friend() name. find() :
int find(nlist* ll, name* n)
{
slist_iterator ff(*(slist*)ll);
ent p;
while ( p=ff() ) if (p==n) return 1;
return 0;
}
, slist_iterator nlist. , - nlist". nlist , , :
void print_list(nlist* ll, char* list_name)
{
slist_iterator count(*(slist*)ll);
name* p;
int n = 0;
while ( count() ) n++;
cout << list_name << "n" << n << "membersn";
slist_iterator print(*(slist*)ll);
while ( p=(name*)print() ) cout << p->string << "n";
}
, , slist ( C++ ):
;
;
, slist;
, .
, , . .
, , . , slist.
, , ( / istream ostream). , , , , . , . .
, , . , , , , . , .
, , , (#4.5), . 3 4 ( ), , . , slist, , , , , , , , .
:
typedef void (*PFC)(char*); //
extern PFC slist_handler;
extern PFC set_slist_handler(PFC);
set_slist_hanlder() . , cerr, exit():
#include "slist.h"
#include
void default_error(char* s)
{
cerr << s << "n";
exit(1);
}
, , :
PFC slist_handler = default_error;
PFC set_slist_handler(PFC handler);
{
PFC rr = slist_handler;
slist_handler = handler;
return rr;
}
, set_slist_hanlder() slist_hanlder(). . , slist , , , .
:
{
PFC old = set_slist_handler(my_handler);
// , slist
// my_handler
set_slist_handler(old); //
}
, slist_hanlder slist, .
, (classdef*, int, char* ..) , nlist: slist. ( ), "". , C , . .
, (generic) slist, gslist, . :
.html#include "slist.h"
#ifndef GENERICH
#include
#endif
#ifndef , , . GENERICH .
name2(), , :
#define gslist(type) name2(type,gslist)
#define gslist_iterator(type) name2(type,gslist_iterator)
, , gslist() gslist_iterator():
#define gslistdeclare(type)
struct gslist(type) : slist {
int insert(type a)
{ return slist::insert( ent(a) ); }
int append(type a)
{ return slist::append( ent(a) ); }
type get() { return type( slist::get() ); }
gslist(type)() { }
gslist(type)(type a) : (ent(a)) { }
~gslist(type)() { clear(); }
};
struct gslist_iterator(type) : slist_iterator {
gslist_iterator(type)(gslist(type)& a)
: ( (slist&)s ) {}
type operator()()
{ return type( slist_iterator::operator()() ); }
}
, .
, nlist, :
#include "name.h"
typedef name* Pname;
declare(gslist,Pname); // gslist(Pname)
gslist(Pname) nl; // gslist(Pname)
declare () . , gslistdeclare, . declare . name*, typedef.
, . , , .
slist - . . , , , . , . , :
#include "slist.h"
class iqueue : slist {
// sizeof(int)<=sizeof(void*)
public:
void put(int a) { slist::append((void*)a); }
int det() { return int(slist::get()); }
iqueue() {}
};
: ( ), int, , iqueue. . - , , :
#include "slist.h"
class stack : slist {
public:
slist::insert;
slist::get;
stack() {}
stack(ent a) : (a) {}
};
" ":
#include "stack.h"
class cp : stack {
public:
void push(char* a) { slist::insert(a); }
char* pop() { return (char*)slist::get(); }
nlist() {}
};
Copyright (c) 2024 Stud-Baza.ru , , , .