Создаем связный список из строк

Апрель 5th, 2011 § 0 comments

Возникла задача реализовать связный список из строк польза от этого очевидна в силу того что мы не знаем сколько строк хранится в файле. Вот тут хранится неплохая статья о том как реализовывать список.


struct accounts
{
 char* line;
 accounts* next;
};
struct accs_dyn_list
{
 accounts* head;
 accounts* tail;
};

void init_dyn_list(accs_dyn_list& list)
{
 list.head=NULL;
}
bool chk_list(accs_dyn_list& list)
{
 return (list.head==NULL);
}
void add_list_item(accs_dyn_list& list,char* str)
{
 accounts* item= new accounts;
 item->line = new char[strlen(str)];
 strcpy(item->line,str);
 item->next=NULL;
 if(chk_list(list))
 {
 list.head=item;
 }else
 {
 list.tail->next=item;
 }
 list.tail=item;
}

accs_dyn_list list;

void LoadFileInList(HANDLE& hFile,accs_dyn_list& list)
{
 size_t buffer_length=GetFileSize(hFile,NULL)+1;
 char* buffer = new char[buffer_length+1];
 DWORD nIn;
 char* temp;
 size_t offset=0;
 size_t index=0;
 size_t size;
 ZeroMemory(buffer,buffer_length);
 while(ReadFile(hFile,buffer,buffer_length-1,&nIn,NULL) && nIn>0)
 {
 buffer[nIn]='\0';
 while(buffer[index]!='\0')
 {
 if(buffer[index]=='\n')
 {
 size=&buffer[index]-&buffer[offset];
 temp= new char[size+1];
 strncpy(temp,&buffer[offset],size);
 temp[size]='\0';
 add_list_item(list,temp);
 delete[] temp;
 offset=index+1;
 }
 index++;

 }
 }
 if(offset!=index)
 {
 size=&buffer[index]-&buffer[offset];
 temp= new char[size+1];
 strncpy(temp,&buffer[offset],size);
 temp[size]='\0';
 add_list_item(list,temp);
 delete[] temp;
 }

}

Оставить комментарий

Ваш email не будет опубликован. Обязательные поля отмечены *

Вы можете использовать это HTMLтеги и атрибуты: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Смотреть фильмы онлайн