단일 링크 된 목록과 해당 복사본 생성자를 만들려는 프로그램이 있습니다. 프로그램이 성공적으로 컴파일됩니다. 그러나 실행 중에는 예상되는 출력의 절반을 인쇄 한 다음 나머지 출력을 인쇄하지 않고 충돌합니다 (기술 보 집이 좋지 않아 죄송합니다).단일 링크 된 목록에 대해 오버로드 생성자가 작동하지 않습니다.
List 클래스에서 내 복사본 생성자에 문제가 있다는 느낌이 들지만 프로그램이 그 시점에 도달하면 충돌이 발생합니다.
# include<iostream>
using namespace std;
class Node
{
public:
int value;
Node* next;
};
class List
{
public:
List();
List(const List &other){
head = new Node;
head = NULL;
Node* temp7;
temp7 = head;
Node* temp6 = other.head;
while (temp6 != NULL)
{
temp7 = new Node;
temp7->value = temp6->value;
temp7 = temp6;
temp6 = temp6->next;
}
}
void push_front(int value){
temporary = new Node;
temporary->value = value;
temporary->next = NULL;
temporary->next = head;
head = temporary;
}
void insert_at(int index, int value){
Node* temp4 = new Node;
temp4->value = value;
temp4->next = NULL;
if (index == 1)
{
temp4->next = head;
head = temp4;
return;
}
Node* temp5 = head;
for (int k = 0; k < index - 2; k++)
temp5 = temp5->next;
temp4->next = temp5->next;
temp5->next = temp4;
}
void remove_at(int index){
Node* temp2 = head;
if (index == 1)
{
head = temp2->next;
delete temp2;
return;
}
for (int j = 0; j < index - 2; j++)
temp2 = temp2->next;
Node* temp3 = temp2->next;
temp2->next = temp3->next;
delete temp3;
}
string printList(void);
private:
Node* head;
Node* temporary;
};
List::List(){
head = NULL;
}
string List::printList(void)
{
Node* temp1 = head;
int counting = 0;
while (temp1 != NULL)
{
cout << "list[" << counting << "] == " << temp1->value << endl;
temp1 = temp1->next;
counting++;
}
}
int main()
{
List list1;
list1.push_front(4);
list1.push_front(3);
list1.push_front(2);
list1.push_front(1);
cout << "list1" << endl;
list1.printList();
cout << endl;
List list2(list1);
cout << "list2" << endl;
list2.printList();
cout << endl;
list1.insert_at(1, 6);
list2.remove_at(2);
cout << "list1" << endl;
list1.printList();
cout << endl;
cout << "list2" << endl;
list2.printList();
cout << endl;
return 0;
}
내가 프로그램에서 오류의 원인을 정확히 파악할 수 없습니다
여기 내 코드입니다. 누구든지 해결책을 제안 할 수 있습니까?
출력은 다음과 같아야합니다. (프로그램은 충돌하기 전에 처음 5 행만 출력합니다). 그런데
>list1
>
>list[0] == 1
>
>list[1] == 2
>
>list[2] == 3
>
>list[3] == 4
>
>list2
>
>list[0] == 1
>
>list[1] == 2
>
>list[2] == 3
>
>list[3] == 4
>
>list1
>
>list[0] == 1
>
>list[1] == 6
>
>list[2] == 2
>
>list[3] == 3
>
>list[4] == 4
>
>list2
>
>list[0] == 1
>
>list[1] == 2
>
>list[2] == 4
,이 스택 오버 플로우 내 첫 번째 질문, 그래서 아무 잘못이나 내가 일을해야 뭔가가 있다면, 정정 해줘 주시기 바랍니다.
도와 주셔서 감사합니다 모든 사람들을위한 많은 내가 문제를 해결 ***
을 :). 분명히 복사 생성자를 제대로 구현하지 못했습니다. 덕분에 어쨌든
// :-) 모든 입력 및 조언을 표준 파일을 그냥이 페이지에 정상 인해 충분하지 않은 점을 대답 할 수없는
// Use standard namespace
using namespace std;
// Declare node class
class Node
{
// All values are public; value of node, and next node pointer
public:
int value;
Node* next;
};
// Declare singly linked list class
class List
{
public:
// Declare main constructor
// Declare copy constructor
List();
List(const List ©ing) : head(NULL)
{
// Use a node to move through the source linked list
// Set the size of the new linked list
// For every node in old list, copy it to a new node and link it to the new singly linked list
Node* cur = copying.head;
int size = copying.size();
Node* end = NULL;
for(int q = 0; q < size; q++)
{
Node* n = new Node;
n->value = cur->value;
if (head == NULL)
{
head = n;
end = head;
}
else
{
end->next = n;
end = n;
}
cur = cur->next;
}
end->next = NULL;
}
// Push front a new node
// Add its value and set its next pointer to NULL
void push_front(int value){
temporary = new Node;
temporary->value = value;
temporary->next = NULL;
temporary->next = head;
head = temporary;
}
// Insert node between x and x+1
// Get the new node's value
// Create the new node by moving from the head->next method
// Add the value and set up the node
void insert_at(int index, int value){
Node* temp4 = new Node;
temp4->value = value;
temp4->next = NULL;
if (index == 1)
{
Node* temp9 = head->next;
temp4->next = temp9;
head->next = temp4;
return;
}
Node* temp5 = head;
for (int k = 0; k < index - 2; k++)
temp5 = temp5->next;
temp4->next = temp5->next;
temp5->next = temp4;
}
// Remove node number [index]
// Get the head
// Iterate through the linked list
// When at the node before the one that has to be deleted, set its value to the node after the next one
// Delete the node that should be deleted
void remove_at(int index){
Node* temp2 = head;
if (index == 1)
{
head = temp2->next;
delete temp2;
return;
}
for (int j = 0; j < index - 1; j++)
temp2 = temp2->next;
Node* temp3 = temp2->next;
temp2->next = temp3->next;
delete temp3;
}
// Simple function to pass the head of a singly linked list
// Simple function to get the size of a function
Node * PassHead(void);
int size()const;
private:
Node* head;
Node* temporary;
};
// Returns head
Node * List::PassHead()
{
return head;
}
// Constructor sets head to NULL
List::List(){
head = NULL;
}
// Gets the size of the singly linked list.
// While the node_>next is not NULL, add 1 to counter
// return counter
int List::size()const {
Node* temp1 = head;
int counting = 0;
while (temp1 != NULL)
{
counting++;
temp1 = temp1->next;
}
return counting;
}
// Same function as the size() function, excetp this time, print the node value while iterating through list
// Nothing returned
void printList(List object)
{
Node* temp1 = object.PassHead();
int counting = 0;
while (temp1 != NULL)
{
cout << "list[" << counting << "] == " << temp1->value << endl;
temp1 = temp1->next;
counting++;
}
}
// Declare main function here
int main()
{
// Object of List
List list1;
// Push some values
list1.push_front(4);
list1.push_front(3);
list1.push_front(2);
list1.push_front(1);
// Print the first list
cout << "list1" << endl;
printList(list1);
cout << endl;
// Copy constructor for second list
List list2(list1);
// Print second list
cout << "list2" << endl;
printList(list2);
cout << endl;
// Insert node in first list
list1.insert_at(1, 6);
// Remove node in second list
list2.remove_at(2);
// Print first list
cout << "list1" << endl;
printList(list1);
cout << endl;
// Print second list
cout << "list2" << endl;
printList(list2);
cout << endl;
// Program ran successfully
return 1;
}
복사 생성자에서'}'을 잊어 버렸습니다. – Rakete1111
** complete ** 오류 메시지를 포함해야합니다. 그들은 보통 문제를 찾는 데 큰 도움이되는 줄 번호를 포함합니다. –
감사합니다. @ Rakete1111! 나는 그 걸림돌을 놓쳤다. 그러나 추가 한 후에도 여전히 작동하지 않습니다. –