문제는 내가 작성한 삽입 기능과 함께 나타납니다.삽입 된 링크 된 목록 문제
3 조건이 작동해야합니다. b/w 1 및 2, b/w 2 및 3을 테스트했으며 마지막 요소로 작동했습니다.
EDIT; 그것은 내 자신의 문제였습니다. 나는 MAXINPUT = 3 (4 대신)을 넣었다는 것을 몰랐다. C++의 더 진보되고 간결한 기능을 사용하여 더 나은 프로그래머가되는 데 도움이되는 모든 노력에 감사드립니다.
기본적으로 문제가 해결되었습니다.
효율성은 아직 (아직) 내 관심사가 아닙니다. 이 디버그 과정을 안내해주십시오.
대단히 감사합니다.
#include<iostream>
#include<string>
using namespace std;
struct List // we create a structure called List
{
string name;
string tele;
List *nextAddr;
};
void populate(List *);
void display(List *);
void insert(List *);
int main()
{
const int MAXINPUT = 3;
char ans;
List * data, * current, * point; // create two pointers
data = new List;
current = data;
for (int i = 0; i < (MAXINPUT - 1); i++)
{
populate(current);
current->nextAddr = new List;
current = current->nextAddr;
}
// last record we want to do it sepeartely
populate(current);
current->nextAddr = NULL;
cout << "The current list consists of the following data records: " << endl;
display(data);
// now ask whether user wants to insert new record or not
cout << "Do you want to add a new record (Y/N)?";
cin >> ans;
if (ans == 'Y' || ans == 'y')
{
/*
To insert b/w first and second, use point as parameter
between second and third uses point->nextAddr
between third and fourth uses point->nextAddr->nextAddr
and insert as last element, uses current instead
*/
point = data;
insert(());
display(data);
}
return 0;
}
void populate(List *data)
{
cout << "Enter a name: ";
cin >> data->name;
cout << "Enter a phone number: ";
cin >> data->tele;
return;
}
void display(List *content)
{
while (content != NULL)
{
cout << content->name << " " << content->tele;
content = content->nextAddr;
cout << endl; // we skip to next line
}
return;
}
void insert(List *last)
{
List * temp = last->nextAddr; //save the next address to temp
last->nextAddr = new List; // now modify the address pointed to new allocation
last = last->nextAddr;
populate(last);
last->nextAddr = temp; // now link all three together, eg 1-NEW-2
return;
}
어리석은 질문을 함축하십시오 : 당신은 흑백으로 무엇을 의미합니까? 그리고 insert (())는 무엇을합니까? 평균? 감사합니다 – Simon
b/w는 위와 같은 매개 변수를 삽입하고 삽입 (삽입)을 의미합니다. 예를 들어, insert ((point-> nextAddr)). – CppLearner