Luc@s
16-10-2003, 16:02
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define OK 0
#define ERR -1
using namespace std;
struct List
{
/*
* The information index
*/
int index;
/*
* The information value
*/
void * data;
/*
* The list size
*/
int _size;
/*
* The next list element
*/
List * next;
/*
* Create struct
* 1. The struct index
* 2. The struct data
* 3. The next structure addres
*/
List(int Index, void * Data, List * Next)
{
data = Data;
index = Index;
next = Next;
}
/*
* Return next element list
* 1.A list pointer
*/
List *Next(List * pr)
{
// first element of list
if(pr->next == NULL)
return pr;
// another case
List * tmp = pr;
tmp = tmp->next;
return tmp;
}
/*
* Add a element
* 1. A List pointer
* 2. A index of element
* 3. The value of element
*/
int add(List * pr, int index, void * data)
{
if(pr == NULL){ /* first element */
return ERR; // error
}
/* another case */
List * p = pr;
while(p->next != NULL)
p = p->next;
p->next = new List(index, data, NULL);
inc_size(false);
return OK;
}
/*
* Print all element
* 1. A List pointer
* 2. A mode
* 0 = wiew into a addres
* 1 = wiew into a string
*/
void print(List * pr, int mode=1)
{
List * p = pr;
while(p != NULL){
if(mode==0)
cout << "Info " << p->index <<" = " << p->data << "\n";
else if(mode==1)
cout << "Info " << p->index <<" = " << (char *) p->data << "\n";
p = p->next;
if(mode > 1){
cout << "\nUndefined Mode!\n";
}
}
}
/*
* Find a element
* 1. A List pointer
* 2. A index that you wont to find
*/
List *find(List * pr, int index)
{
List * p = pr;
while((p->next != NULL) && (p->index != index))
p = p->next;
return p;
}
/*
* Remove a element
* 1. A List pointer
* 2. A index of element
*/
void remove(List* pr, int n)
{
List *p;
while((pr != NULL))
{
if(pr->next != NULL && pr->index == n)
{
p = pr->next;
pr->next = pr->next->next;
delete p;
}
pr = pr->next;
dec_size(false);
}
}
/*
* Generate a list.
* 1. a array
* 2. a array lenght
*/
List *generate(List * pr, void * parms[], int lenght)
{
for(register int i=0; i<lenght; i++){
pr->next = new List(i, parms[i], NULL);
}
}
/*
* Return the size of list
*/
int size() const
{
return _size;
}
/*
* Increment a list size
* 1. The mode
* true = increment
* false = show
*/
void inc_size(bool show)
{
if(show == true){
cout << _size;
return;
}
_size++;
}
/*
* Decrement a list size
* 1. The mode
* true = decrement
* false = show
*/
void dec_size(bool show)
{
if(show == true){
cout << _size;
return;
}
_size--;
}
};
}
#include <cstdio>
#include <cstring>
#include <string>
#define OK 0
#define ERR -1
using namespace std;
struct List
{
/*
* The information index
*/
int index;
/*
* The information value
*/
void * data;
/*
* The list size
*/
int _size;
/*
* The next list element
*/
List * next;
/*
* Create struct
* 1. The struct index
* 2. The struct data
* 3. The next structure addres
*/
List(int Index, void * Data, List * Next)
{
data = Data;
index = Index;
next = Next;
}
/*
* Return next element list
* 1.A list pointer
*/
List *Next(List * pr)
{
// first element of list
if(pr->next == NULL)
return pr;
// another case
List * tmp = pr;
tmp = tmp->next;
return tmp;
}
/*
* Add a element
* 1. A List pointer
* 2. A index of element
* 3. The value of element
*/
int add(List * pr, int index, void * data)
{
if(pr == NULL){ /* first element */
return ERR; // error
}
/* another case */
List * p = pr;
while(p->next != NULL)
p = p->next;
p->next = new List(index, data, NULL);
inc_size(false);
return OK;
}
/*
* Print all element
* 1. A List pointer
* 2. A mode
* 0 = wiew into a addres
* 1 = wiew into a string
*/
void print(List * pr, int mode=1)
{
List * p = pr;
while(p != NULL){
if(mode==0)
cout << "Info " << p->index <<" = " << p->data << "\n";
else if(mode==1)
cout << "Info " << p->index <<" = " << (char *) p->data << "\n";
p = p->next;
if(mode > 1){
cout << "\nUndefined Mode!\n";
}
}
}
/*
* Find a element
* 1. A List pointer
* 2. A index that you wont to find
*/
List *find(List * pr, int index)
{
List * p = pr;
while((p->next != NULL) && (p->index != index))
p = p->next;
return p;
}
/*
* Remove a element
* 1. A List pointer
* 2. A index of element
*/
void remove(List* pr, int n)
{
List *p;
while((pr != NULL))
{
if(pr->next != NULL && pr->index == n)
{
p = pr->next;
pr->next = pr->next->next;
delete p;
}
pr = pr->next;
dec_size(false);
}
}
/*
* Generate a list.
* 1. a array
* 2. a array lenght
*/
List *generate(List * pr, void * parms[], int lenght)
{
for(register int i=0; i<lenght; i++){
pr->next = new List(i, parms[i], NULL);
}
}
/*
* Return the size of list
*/
int size() const
{
return _size;
}
/*
* Increment a list size
* 1. The mode
* true = increment
* false = show
*/
void inc_size(bool show)
{
if(show == true){
cout << _size;
return;
}
_size++;
}
/*
* Decrement a list size
* 1. The mode
* true = decrement
* false = show
*/
void dec_size(bool show)
{
if(show == true){
cout << _size;
return;
}
_size--;
}
};
}