Скачиваний:
31
Добавлен:
02.05.2014
Размер:
3.24 Кб
Скачать
//Class "List"
#ifndef __list_cpp
#define __list_cpp
#include "list.h"
void List::lerror (char *err) { //if error occured
cout<<" List: "<<err<<"\n";
}
List::List () { //default constructor
cnt=0;
id=0;
curr=NULL;
start=NULL;
}
List::~List () {
while(!empty())
del();
}
bool List::empty() { //is list empty?
return(start==NULL);
}
bool List::rewind (int topos) { //rewind on <topos> position
if (curr==NULL) {
lerror("List is empty");
return(0);
}
if (topos>=cnt||topos<=-cnt) {
topos%=cnt;
if (topos>cnt/2)
topos-=cnt;
else
if (topos<-cnt/2)
topos+=cnt;
}
if (topos>0) {
while (topos>0) {
topos--;
curr=(*curr).next;
}
}
else {
while (topos<0) {
topos++;
curr=(*curr).prev;
}
}
return(1);
}

bool List::rewindto (int topos) {
if (curr==NULL) {
lerror("List is empty");
return(0);}
curr=start;
rewind(topos);
return(1);
}

int List::count() {
return(cnt);
}

void List::add(int pos, char a, char b, char c, char d) {
node* f;
if (start==NULL) { //Empty list
f=new node;
start=f;
(*f).a=a;
(*f).b=b;
(*f).c=c;
(*f).d=d;
(*f).id=id;
(*f).prev=f;
(*f).next=f;
curr=f;
}
else
{
node *next, *prev;
if(!rewindto(pos))
return;
prev=(*curr).prev;
next=curr;
f=new node;
(*f).a=a;
(*f).b=b;
(*f).c=c;
(*f).d=d;
(*f).id=id;
(*f).prev=prev;
(*f).next=next;
(*prev).next=f;
(*next).prev=f;
if (pos==0)
start=f;
curr=f;
}
id++;
cnt++;
}

void List::add(char a, char b, char c, char d) {
if (start==NULL) {
add(0,a,b,c,d);}
else
{ node *f;
f=new node;
(*f).a=a;
(*f).b=b;
(*f).c=c;
(*f).d=d;
(*f).id=id;
(*f).prev=curr;
(*f).next=(*curr).next;
(*curr).next=f;
(*(*f).next).prev=f;
curr=f;
id++;
cnt++;
}
}

bool List::get(char &a,char &b, char &c, char &d) {
if (curr==NULL)
return(0);
a=(*curr).a;
b=(*curr).b;
c=(*curr).c;
d=(*curr).d;
return(1);
}

void List::print(int from) {
if (empty()) {
cout<<"There no items\n";
return;
}
if (!rewindto(from))
return;
int startid=(*curr).id;
cout<<(*curr).a<<"\t"<<(*curr).b<<"\t"<<(*curr).c<<"\t"<<(*curr).d<<"\n";
curr=(*curr).next;
while ((*curr).id!=startid) {
cout<<(*curr).a<<"\t"<<(*curr).b<<"\t"<<(*curr).c<<"\t"<<(*curr).d<<"\n";
curr=(*curr).next;
}
}

void List::print() {
print(0);
}

void List::del() {
if (empty()) {
lerror("List is empty");
return;
}
node* f;
f=curr;
(*(*curr).prev).next=(*curr).next;
(*(*curr).next).prev=(*curr).prev;
if ((*curr).id==(*start).id) {
if ((*curr).id==(*(*curr).next).id) {
start=NULL;
curr=NULL;
}
else {
start=(*curr).next;
curr=start;
}
}
else
curr=(*curr).next;
delete f;
cnt--;
}

void List::del(int pos) {
if (!rewindto(pos))
return;
del();
}

void List::exchange(int pos) {
if (!rewindto(pos))
return;
node* next=(*curr).next;
(*(*curr).prev).next=next;
(*(*next).next).prev=curr;
(*next).prev=(*curr).prev;
(*curr).next=(*next).next;
(*curr).prev=next;
(*next).next=curr;
if (pos==0)
start=next;
}

void List::update (char a, char b, char c, char d) {
(*curr).a=a;
(*curr).b=b;
(*curr).c=c;
(*curr).d=d;
}

void List::update (int pos, char a, char b, char c, char d) {
rewindto(pos);
(*curr).a=a;
(*curr).b=b;
(*curr).c=c;
(*curr).d=d;
}
#endif
Соседние файлы в папке Исходник