Мои записи
[CPP] Roll (Список)
== Magic Technologic Inc.==
Volume 0x0c, Algoritms 0x14, Phile #0x0a of 0x0f
|=---------------------------------------------------------------=| |=----------------=[ Список - Структура данных ]=----------------=| |=---------------------------------------------------------------=| |=---------------=[ Copyright (c) Flame Of Soul ]=---------------=| |=--------------=[ [E-mail]: _allbasse@yandex.ru ]=--------------=| |=---------------------------------------------------------------=| |=----------------------------=[ CPP ]=--------------------------=| |=---------------------------------------------------------------=| LIST.h ****************************************************************** #ifndef __list_h #define __list_h #include <iostream.h>
class List { private: struct node { //Default data structure int id; //Indificator char a; //Field1 char b; //Field2 char c; //Field3 char d; //Field4 node* prev; //Link to prev node node* next; //Link to next node };
node* start; //Link to start node node* curr; //Mark int cnt; //Count of list int id; //Group ID void lerror (char *err); //If error occured public: List (); //Default Constructor ~List (); //Destructor bool empty(); //Is list empty? bool rewind (int topos); //Rewind on <> positions bool rewindto (int topos); //Rewind to <> position int count(); //Get list nodes count void add(int pos, char a, char b, char c, char d); void add(char a, char b, char c, char d); bool get(char &a,char &b,char &c,char &d); void update (char a, char b, char c, char d);void update (int pos, char a, char b, char c, char d); void print(int from); //Print from <> position void print(); //Print list nosed void del(); //Delete active node void del(int pos); //Delete node in <> position void exchange(int pos); //Exchange <n> and <n+1> nodes }; #endif *******************************************************************
LIST.cpp ******************************************************************* //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 |=-----------------------------=[ CPP ]=---------------------------=| |=-----------------------------------------------------------------=|
|
Категория: Структуры данных | Добавил: flame (30.05.2009)
|
Просмотров: 707 | Комментарии: 1
| Рейтинг: 0.0/0 |
|
Добавлять комментарии могут только зарегистрированные пользователи. [ Регистрация | Вход ]
|
Статистика
Онлайн всего: 1 Гостей: 1 Пользователей: 0
|