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

#c

#c

Вопрос:

Нас просят создать программу, использующую структуру списка, чтобы перечислить информацию об учениках, но, похоже, я не могу найти способ вывести все это. Он показывает только один, и это последний ввод. Я новичок в создании связанного списка, поэтому я не знаю, как ими манипулировать. Любая помощь будет высоко оценена! Спасибо, ребята!

 #include <iostream>
#include <iomanip>
using namespace std;

int main(){

struct StudList{
string studID, studName, Course;
int Yr_level;

StudList *next;
};

StudList *head;
head = NULL;

StudList *stud;
StudList *studptr;

int studNum;
system("color F0");
cout << "Enter number of stuedents: ";
cin >> studNum;

for (int i=1; i<=studNum; i  )
{
    stud = new StudList;

    cout << "nEnter Student ID   : ";
    cin >> stud -> studID;
    cout << "Enter Student Name : ";
    cin >> stud -> studName;
    cout << "Course             : ";
    cin >> stud -> Course;
    cout << "Year Level         : ";
    cin >> stud -> Yr_level;

    stud -> next = NULL;
    cout << endl;
}
cout << "nThe students are: ";
while (stud != NULL)
{
    cout << endl << endl << left << setw(10) << stud -> studID << setw(10) << stud -> studName <<
    setw(8) << stud -> Course << setw(4) << stud -> Yr_level;
    stud = stud -> next;
}

if (head == NULL)
{
    head == stud;
}
else
{
    studptr = head;

    while (studptr -> next)
    {
        studptr = studptr -> next;
    }
    studptr -> next = stud;
    }
    }
  

Комментарии:

1. В for цикле, в котором вы вводите информацию об ученике, вы не подключаете вновь созданный stud список к самому списку (вы должны сохранить хвост вашего списка и сохранить указатель stud на tail->next поле at

Ответ №1:

В for цикле вы должны сохранить новый stud объект для настройки stud->next во время следующей итерации.

В while цикле вы проверяете только последний созданный stud объект. Вы должны установить stud для первого объекта из списка, вы можете использовать head object для этого, чтобы сохранить первый элемент списка.

Я думаю, что если (head == NULL) ... часть не нужна.

 #include <iostream>
#include <iomanip>
using namespace std;

int main(){

struct StudList{
string studID, studName, Course;
int Yr_level;

StudList *next;
};

StudList *head;
head = NULL;

StudList *stud;

int studNum;
system("color F0");
cout << "Enter number of stuedents: ";
cin >> studNum;



StudList * temp = nullptr;
for (int i=1; i<=studNum; i  )
{
    
    stud = new StudList;
    if ( i == 1 ) 
        {head = stud;}
    else
        {temp->next = stud;}
    
        
    cout << "nEnter Student ID   : ";
    cin >> stud -> studID;
    cout << "Enter Student Name : ";
    cin >> stud -> studName;
    cout << "Course             : ";
    cin >> stud -> Course;
    cout << "Year Level         : ";
    cin >> stud -> Yr_level;
    
    stud -> next = NULL;
    temp = stud;
    cout << endl;
}

stud = head;

cout << "nThe students are: ";
while (stud != NULL)
{
    cout << endl << endl << left << setw(10) << stud -> studID << setw(10) << stud -> studName <<
    setw(8) << stud -> Course << setw(4) << stud -> Yr_level;
    stud = stud -> next;
}

}
  

Комментарии:

1. Я действительно ценю это! Я приму это к сведению в следующий раз!