-4
저는 C++의 초보자입니다. 동적으로 할당 된 링크 된 목록을 큐 순서 (FIFO)로 쓰려고합니다. 프로그램을 컴파일하고 실행할 수 있습니다. 하지만 아무것도 인쇄 할 수 없습니다. 그래서 문제가 링크에 존재하는지 또는 출력 로직을 출력하는지 알 수 없습니다. 도와주세요.큐 목록의 연결된 목록
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include <iomanip>
using std::setw;
using std::left;
struct course
{
char coursename[32];
char term[32];
int unit;
char grade;
course* next;
};
void coutcourse(course*);
int main()
{
char input;
course *p;
course *prev;
course PHILO225 = {"PHILO-225", "SP2017", 3, 'A'}; // examples
course COMSC110 = {"COMSC-110", "SP2017", 4, 'A'}; //
course COMSC165 = {"COMSC-165", "FA2017", 4, 'X'};
course* start = 0;
course *t;
cout.setf(ios::left, ios::adjustfield);
while(true)
{
cout <<"Do you want to add a new course? [Y for yes, N for no]" << endl;
cin >> input;
if(input=='y'||input=='Y')
{
t= new course;
cout <<"Enter the name, term, units and grade for the new course in the same line, space separated." << endl;
cin >> t->coursename;
cin >> t->term;
cin >> t->unit;
cin >> t->grade;
for(p=start; p ; p=p->next)
{
t->next = 0;
if(start==0)
{
start=t;
p=t;
}
else
{
p->next=t;
p=t;
}
}
cout << endl <<setw(16)<<"COURSE"<<setw(16)<<"TERM" <<setw(16) <<"UNITS"<< setw(10)<<"GRADE" <<endl;
cout << "------------- -------- --------- -----------\n";
for (p=start;p;p=p->next)
coutcourse(p);
cout << endl;
continue;
}
if(input=='N'||input=='n')
break;
else
{
cout << "Invalid. Please try again." << endl;
continue;
}
}
for(p=start, prev=0; p ; prev=p, p=p->next)
{
if(p)
prev->next=p->next;
else
start=p->next;
delete p; // this can be written in another way. delete from start
}
}
void coutcourse(course* start)
{
cout<< setw(16) << start->coursename<< setw(16) << start->term << setw(16) << start->unit << setw(16) << start->grade << endl;
}
어디에서 실수했는지 알려주십시오. 고맙습니다.
디버거를 사용하는 법을 배워야합니다. – Incomputable
권장 사항 : 의미있는 변수 이름을 사용하십시오. 널 포인터는'nullptr' 대신에'nullptr'을 사용하고,'for' 루프 밖에서't-> next = 0;'을 취하십시오. – user4581301