14SatanA88, в общем я сам по себе ленивый и каждый раз набивать зависимости утомляет. Плюс, я так и не понял зачем тут нужен стек. Предлагаю свою реализацию, правда я реализовал естественно не всё, но то что осталось - это семечки. Программа выводит все пары вместе с номером "тусовки" (очерёдность номеров может теряться).
| Код | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
typedef struct _TPersonInfo { char* name; // Имя int friend; // Номер друга int group; // Принадлежность к группе } TPersonInfo; // Данные о человеке
typedef struct _TPersonList { TPersonInfo* person; // Список людей unsigned size; // Количество людей } TPersonList;
//----------------------------------------------// // Генерация случайного имени (верхний регистр лат. алф.) char* GetRandomName(char* theName, unsigned theLength) { char* cur = theName;
while (theLength--) { *cur = rand() % 26 + 'A'; cur++; } return theName; } //----------------------------------------------// // Создание новой записи о человеке TPersonInfo* PushPerson(TPersonInfo* thePerson, char* theName, unsigned theLength) { thePerson->name = malloc(sizeof(char) * theLength + 1); memset(thePerson->name, 0, theLength + 1); strncpy(thePerson->name, theName, theLength); thePerson->friend = -1; thePerson->group = -1; return thePerson; } //----------------------------------------------// // Генерация людей вместе с информацией о их друзьях TPersonList* GeneratePerson(TPersonList* theList, unsigned theCount, unsigned theNameLength) { char* buff = malloc(sizeof(char) * theNameLength + 1);
theList->size = theCount; theList->person = malloc(sizeof(TPersonInfo) * theList->size); unsigned i; for (i = 0; i < theList->size; ++i) { PushPerson(&theList->person[i], GetRandomName(buff, theNameLength), theNameLength); theList->person[i].friend = rand() % theList->size; }
free(buff); return theList; } //----------------------------------------------// // Смена группы, т.е. слияние двух групп TPersonList* ChangeGroupForList(TPersonList* theList, unsigned theOldGroup, unsigned theNewGroup) { unsigned i; for (i = 0; i < theList->size; ++i) { if (theList->person[i].group == theOldGroup) { theList->person[i].group = theNewGroup; } } return theList; } //----------------------------------------------// // Расстановка принадлежности человек к конкретной группе TPersonList* SetGroupForPerson(TPersonList* theList) { unsigned curr = 0;
unsigned j;
for (j = 0; j < theList->size; ++j) { unsigned i = j;
if (theList->person[i].group < 0) { curr++; while (theList->person[i].group < 0) { theList->person[i].group = curr; i = theList->person[i].friend; } if (theList->person[i].group != curr) { ChangeGroupForList(theList, curr, theList->person[i].group); } } } return theList; } //----------------------------------------------// // Выводим всю информацию. Формт: // Номер группы : [имя самого человека] <=> [имя друга] void PrintPerson(const TPersonList* theList) { unsigned i; for (i = 0; i < theList->size; ++i) { unsigned j = theList->person[i].friend; printf("%2d : %s <=> %s\n", theList->person[i].group, theList->person[i].name, theList->person[j].name); } } //----------------------------------------------// int main(int argc, char** argv) { if (argc != 2) { printf("Usage: program.exe COUNT_PERSON\n"); return 0; } srand(time(0));
TPersonList list; GeneratePerson(&list, atoi(argv[1]), 3); SetGroupForPerson(&list); PrintPerson(&list);
return 0; }
|
Результат выполнения: http://www.radikal.ru
http://codepad.org/YO0Iawzn
Исходник с бинарником ниже:
|