Мне дано задание создать различные методы для связанного списка в C. Я застрял в методе подкачки, который, кажется, испортил весь связанный список. Есть ли у кого-нибудь совет, где я ошибаюсь? Ура!Поменять место в одиночном списке в C
Вот мой код.
int main(int argc, char* argv[])
{
// A list of pointers to Reminders
const int MAX_ENTRIES = 10;
int numOfEntries = 0 ;
reminder_t* pFirst = (reminder_t*) malloc (sizeof(reminder_t));
reminder_t* pSecond = (reminder_t*) malloc (sizeof(reminder_t));
reminder_t* pThird = (reminder_t*) malloc (sizeof(reminder_t));
reminder_t* pStart = NULL;
if (pFirst != NULL)
{
strcpy(pFirst->message, "Mikes Birthday");
pFirst->dateOfEvent.day= 1;
pFirst->dateOfEvent.month= 1;
pFirst->dateOfEvent.year= 2013;
pFirst->pNext = NULL;
}
if (pSecond != NULL)
{
strcpy(pSecond->message, "Als Soccer Match");
pSecond->dateOfEvent.day= 2;
pSecond->dateOfEvent.month= 2;
pSecond->dateOfEvent.year= 2013;
pSecond->pNext = NULL;
}
if (pThird != NULL)
{
strcpy(pThird->message, "School Concert");
pThird->dateOfEvent.day= 3;
pThird->dateOfEvent.month= 3;
pThird->dateOfEvent.year= 2013;
pThird->pNext = NULL;
}
pFirst->pNext = pSecond;
pSecond->pNext = pThird;
pThird->pNext = NULL;
pStart = pFirst;
printf("\n------Before------\n");
listEntries(pStart);
swapPositonOf(pFirst,pThird);
printf("\n------After-aa-----\n");
listEntries(pStart);
getchar();
return 0;
}
void listEntries(reminder_t * pList)
{
printf("\n");
while (pList != NULL)
{
printf("%s\n", pList->message);
pList = pList->pNext;
}
}
void swapPositonOf(reminder_t* first , reminder_t* second)
{
reminder_t* pFirst = (reminder_t*) first;
reminder_t* pSecond = (reminder_t*) second;
reminder_t* temp = second->pNext;
pSecond->pNext = pFirst->pNext;
pFirst->pNext = temp;
temp = pSecond;
pSecond = pFirst;
pFirst = temp;
}
Ожидаемый результат:
------Before------
Mikes Birthday
Als Soccer Match
School Concert
------After-aa-----
School Concert
Als Soccer Match
Mikes Birthday
Выход:
------Before------
Mikes Birthday
Als Soccer Match
School Concert
------After-aa-----
Mikes Birthday
Пожалуйста, предоставьте немного больше информации: что именно происходит при сортировке списка? Каков вход, выход и ожидаемый результат? – razlebe
Действительно ли нужен код, отличный от функции подкачки и определения напоминания? – BrainSteel
Почему резервные копии в начале 'swapPositionOf'? (Почему назначение «first' на« pFirst »и секунда?) – Kninnug