p align="left"> { last=new element[1]; last->info=new char[strlen(s)+1]; strcpy(last->info,s); last->down=NULL; last->up=current; current->down=last; current=last; return; }; //put in buffer adress down last->down=current->down; current->down=new element[1]; current->down->up=current; current=current->down; current->down=last->down; //clear buffer last->down->up=current; last->down=NULL; current->info=new char[strlen(s)+1]; strcpy(current->info,s); return; }; void list::add_before(char* s) { if (current==NULL) { add_after(s); return; }; if (current==first) { first=new element[1]; first->up=NULL; first->down=current; current=current->up=first; first->info=new char[strlen(s)+1]; strcpy(first->info,s); return; }; first->up=current->up; current->up=new element[1]; current->up->down=current; current=current->up; current->up=first->up; first->up->down=current; first->up=NULL; current->info=new char[strlen(s)+1]; strcpy(current->info,s); return; }; void list::add_element(char* s, int &k) { int i; i=current_element(k); if ((i<k)&&(i!=0)) { cout<<"V spiske vsego "<<i<<" elmentov. Po etomu vmesto "<<k<<" nomera, functsia dobavit info elementa na "<<i+1<<"-e mesto(poslednee v spiske)!!!\n"; k=i+1; add_after(s); }; if(i==0) add_before(s); return; }; void list::del_element() { if (first==NULL) { cout<<"Nemogu udalit element so spiska. Spisok pust.\n"; return; }; if (current==first) { if (current==last) {first=last=NULL;} else { first=current->down; first->up=NULL; }; delete[]current->info; delete[]current; current=first; return; }; if (current==last) { last=current->up; last->down=NULL; delete[]current->info; delete[]current; current=last; return; }; last->down=current->down; current->up->down=current->down; current->down->up=current->up; delete[]current->info; delete[]current; current=last->down; last->down=NULL; return; }; void list::del_element(int &k) { int i=current_element(k); if ((i<k)&&(i!=0)) { cout<<"V spiske vsego "<<i<<" elmentov. Po etomu vmesto "<<k<<" elementa, functsia udalit "<<i<<" element!!!\n"; k=i; }; del_element(); return; }; list::~list() { del_list(); }; void list::del_list() { while (last!=first) { current=last; delete [] last->info; last=last->up; delete [] current; }; if (first) delete [] first->info; delete[]first; current=first=last=NULL; return; }; //current ostaetsa netronutym int list::num_list() { if(first==NULL) return 0; int i(1); element* temp=first->down; while (temp!=NULL) { i++; temp=temp->down; }; return i; }; //if OK return 0, esle return i (number current element) int list::current_element(int k) { if (first==NULL) { cout<<"V spiske 0 elementov.\n"; return 0; }; int i(1); current=first; if(i==k) return 0; while (current->down) { current=current->down; if (++i==k) return 0; }; return i; }; //Esli sled. element sdelalsa tekuschim return 1, else 0 int list::next_element() { if (current->down) { current=current->down; return 1; }; return 0; }; //Esli pred. element sdelalsa tekuschim return 1, else 0 int list::previous_element() { if (current->up) { current=current->up; return 1; }; return 0; }; int list::num_current() { if(current==NULL)return 0; int i(1); element* temp=current; while (temp->up!=NULL) { i++; temp=temp->up; }; return i; }; char* list::read_element() { return current->info; }; char* list::read_element(int &k) { int i; i=current_element(k); if ((i<k)&&(i!=0)) { cout<<"V spiske vsego "<<i<<" elmentov. Po etomu vmesto "<<k<<" elementa, functsia vozvrashchaet "<<i<<" element!!!\n"; k=i; }; return current->info; }; void list::init_list(int k, char* str) { if (k==0) return; int f(2); char* s; s=new char[strlen(str)+7]; if (first!=NULL) { cout<<"Spisok ne pust!!!\n"; cout<<"Chto delat?(0/1/2)\n"; cout<<" 0)Otmenit initsializatsiu;\n"; cout<<" 1)Dopisat fonarnuu infomatsiu v konets spiska;\n"; cout<<" 2)Zamenit spisok novoi fonarnoi informatsyei.\n"; cin>>f; }; if(f==0) return; if(f==1) current=last; if(f==2) { del_list(); first=new element[1]; first->info=new char[strlen(str)+3]; sprintf(first->info,"%s 1",str); first->up=first->down=NULL; current=last=first; }; for (int j=f; j<=k; j++) { sprintf(s,"%s %d",str,j); add_after(s); }; return; }; void list::print_list() { cout<<"Vmestimoe spiska:\n"; element* temp=first; if (first==NULL) { cout<<"Spisok pust!!!\n"; return; }; do "<<temp->info<<" while (temp); cout<<"\n"; return; }; int list::fput_list(char* s) { FILE* f; if ((f=fopen(s,"w+"))==NULL) { cout << "Can not open file "<<s<<"\n"; return 0; }; if (!first) fclose(f); element* temp=first; do { fputs(temp->info,f); fputs("\n",f); temp=temp->down; } while (temp); fclose(f); return 1; }; int list::fget_list(char* s) { int ff=2; if (first!=NULL) { cout<<"Spisok ne pust!!!\nChto delat?(0/1/2)\n 0)Otmenit vostanovlenie iz faila;\n"; cout<<" 1)Vostanovit infomatsiu iz faila v konets spiska;\n"; cout<<" 2)Zamenit spisok informatsyei iz faila.\n"; cin>>ff; }; if(!ff) return ff; FILE* f; if ((f=fopen(s,"r"))==NULL) { cout<<"Can not open file "<<s<<"\n"; return 0; }; char str[255]; if(ff==1) current=last; char *sss; if(ff==2) del_list(); while (fgets(str,256,f)) { sss=new char[strlen(str)]; int j=0; while(str[j]!='\n') sss[j]=str[j++]; sss[j]='\0'; add_after(sss); delete [] sss; }; fclose(f); return ff; }; #endif #include "list_.h" void main() { int n=16; cout<<"Rozhdenie spiska...\n"; list s; char a[255]; strcpy(a,"Seha"); s.print_list(); cout<<"\nVsego elementov v spiske: "<<s.num_list(); cout<<"\nTekuschiy element spiska: "<<s.num_current()<<'\n'; cout<<"Press <Enter> for go on...\n"; getchar(); cout<<"Skol'ko proinitsializirovat' elementov spiska?\nn="; cin>>n; cout<<"Initsialitsia spiska...\n"; s.init_list(n,a); s.print_list(); cout<<"\nVsego elementov v spiske: "<<s.num_list(); cout<<"\nTekuschiy element spiska: "; cout<<s.num_current()<<'\n'; cout<<"Press <Enter> for go on...\n"; getchar();//clear iostream getchar(); cout<<"Kakoi element v spiske sdelat' tekeschim?\nn="; cin>>n; s.current_element(n); cout<<"\nVsego elementov v spiske: "<<s.num_list()<<"\nTekuschiy element spiska: "<<s.num_current()<<'\n'; cout<<"Skol'ko elementov dobavit'?\n"; cin>>n; getchar();//clear iostream for (int i=0; i<n; i++) { cout<<"Vvedite stroku dlja dobavlenija v spisok\n"; gets(a); cout<<"Dobavlenie posle tekuschego...\n";
Страницы: 1, 2, 3
|