Код: Выделить всё
struct card
{
char *author;
char *title;
char *city;
char *firm;
int year;
int pages;
};
struct record
{
card book;
record *prior;
record *next;
};
card books[] =
{
{ "Winer R.S.", "Jasik Turbo C++", "M", "Mir", 1991, 384 },
{ "Stroustrup B.", "Jasik C++", "Kiev", "DiaSoft", 1993, 560 },
{ "Turbo C++", "Rukovodstvo programmista", "M", "INTKV", 1991, 394 },
{ "Lippman S.B.", "C++ dlj nachinauchih", "M", "GELION", 1993, 496 }
};
void main()
{
record *begin = NULL, //указатель на начало списка
*last = NULL, // указатель на очередную запись
*list; //указатель на элементы списка
//n – кол-во записей в списке
int n = sizeof(books)/sizeof(books[0]);
for(int i = 0; i < n; ++i)
{
last = new(record);
(*last).book.author = books[i].author;
(*last).book.title = books[i].title;
last->book.city = books[i].city;
last->book.firm = books[i].firm;
last->book.year = books[i].year;
last->book.pages = books[i].pages;
if(begin == NULL)
{
last->prior = NULL;
begin = last;
last->next = NULL;
}
else
{
list = begin;
while(list)
{
if(strcmp(last->book.author, list->book.author) < 0)
{
if(begin == list)
{
last->prior = NULL;
begin = last;
}
else
{
list->prior->next = last;
last->prior = list->prior;
}
list->prior = last;
last->next = list;
break;
}
if(list->next == NULL)
{
last->next = NULL;
last->prior = list;
list->next = last;
break;
}
list = list->next;
}
}
}
}
books[] = {…} (ф-ции вывода опущены). Все работает. Необходимо в ходе выполнения программы добавить (вручную) какие-то новые данные. Делаю так:
Код: Выделить всё
char *b;
cin >> b;
card str = {b};
books[4] = str;
Winer R.S., Jasik Turbo C++, M, Mir, 1991, 384
Stroustrup B., Jasik C++, Kiev, DiaSoft, 1993, 560
Turbo C++, Rukovodstvo programmista, M, INTKV, 1991, 394
Lippman S.B., C++ dlj nachinauchih, M, GELION, 1993, 496
……………………………………………………
Ххххххххх, хххххххххххх, ххххх, 1111, 222