2014-04-02 4 views
0

현재 연결 목록, 특히 이중 연결 목록을 사용하는 방법을 배우고 있으며 역순으로 인쇄하려고하면 프로그램에 문제가 발생했습니다.이중 연결 목록 인쇄

#include <iostream> 

using namespace std; 

struct node 
{ 
    int data; //int to store data in the list 
    node *next; //pointer to next value in list 
    node *prev; //pointer to previous value in list 
}; 

node *appendList(node *current, int newData) //Function to create new nodes in the list 
{ 
    node *newNode; //create a new node 
    newNode = new node; 
    newNode->data = newData; //Assign data to it 
    newNode->next = NULL; //At end of list so it points to NULL 
    newNode->prev = current; //Link new node to the previous value 
    current->next = newNode; //Link current to the new node 
    return newNode; //return the new node 
} 

node *createList(int maxLoop, node *begin, node *current, node *end) //Function to create list 
{ 
    //Allocate the starting node 
    current = new node; 
    current -> data = 1; //First data value is 1 
    current -> next = NULL; //next value is NULL 
    current -> prev = NULL; //previous value is NULL 
    begin = current; //This is the beginning of the list 

    for (int count = 2; count <= maxLoop; count++) //Loop to fill the list 
    { 
     current = appendList(current, count*count); //Create new nodes and fill with square numbers 
    } 
    end = current; //Now we are at the end of the list 
    return begin; //Return begin, this is the problem; I can't return end as well 
} 

void printForward (node *p) //Function to print the list forwards 
{ 
    node *curr = p; //current is the beginning value of the list 
    while (curr != NULL) //Continue while current is not at the end of the list 
    { 
     cout << curr->data << " "; //Print out the data of current 
     curr = curr->next; //Move one value along in the list 
    } 
} 

void printBackward (node *p) //Function to print the list backwards 
{ 
    node *curr = p; //current is the end value of the list 
    while (curr != NULL) //Continue while current is not at the beginning of the list 
    { 
     cout << curr->data << " "; //Print out the data of current 
     curr = curr->prev; //Move one value back in the list 
    } 
} 

int main() 
{ 
    //Initialize current, begin, and end 
    node *current = NULL; 
    node *begin = NULL; 
    node *end = NULL; 
    int maxLoop = 10; //The number of items in the list 

    cout << "The list has now been created." << endl; 
    begin = createList(maxLoop, begin, current, end); //function to create the list 
    cout << "Printed forwards, this list is: "; 
    printForward(begin); //Function to print the list forwards 
    cout << endl; 
    cout << "Printed backwards, this list is: "; 
    printBackward(end); //Function to print the list backwards 
    cout << endl; 
    return 0; 
} 

이 프로그램의 목적은, 목록을 만들 전달을 인쇄, 뒤로, 요소의 삽입, 요소를 삭제하는 것입니다 : 여기

내가 도움이 필요 코드의 부분이다 목록을 파괴하십시오. 필자는 작성, 인쇄 및 인쇄 기능으로 다듬 었습니다.

문제는 createList 함수에서 begin과 end를 모두 수정하지만 하나만 반환 할 수 있다는 것입니다. 즉, 반환하지 않은 함수는 여전히 주 함수에서 NULL이므로 아무 것도 가리 키지 않습니다. 시작/현재/끝을 NULL과 같지 않게 설정하려고 시도했지만 그렇게하면 createList가 작동하지 않습니다.

둘 모두 수정하는 방법에 대한 아이디어가 있습니까? 그냥 분명히하려면 목록 HAS TO이 함수에서 만들어지면 기본으로 초기화하는 것이 매우 쉽습니다.

감사합니다, 트리스탄

+1

참조로 포인터를 가져올 수 있습니다 (노드 * 및 시작) – Kevin

답변

1
귀하의 문제는 당신이 포인터를 복사하는되어

, 당신은 포인터 - 투 - 포인터 또는 참조 - 투 - 포인터를 사용하여, 예를 참조하여 그들을 통과보다는해야 할 때 그냥 복사 main의 포인터가 원래 가리키고있는 값. 당신이하고있는 일로, 당신은 main에 선언 된 원래의 포인터 변수를 수정할 수 없습니다 ... 참조에 의한 전달은 모든 목록 설정 코드를 함수 내에 유지하면서 그렇게 할 수있게합니다.

그래서 예를 들어,

마지막으로 함수의 본문에 계정에 추가 역 참조를 가지고 있는지 확인 후

node* createList(int maxLoop, node *begin, node *current, node *end) 

void createList(int maxLoop, node** begin, node** current, node** end) 

을 변경하고, 당신은 부를 것이다 좋아해 :

createList(maxLoop, &begin, &current, &end); 

대신 createList의 기능 본문 내에 begin의 최종 할당을 지정합니다.

+0

함수에서 추가 역 참조가 무슨 의미인지 이해하지만 어떻게 할 것입니까? 특히 "current = new node;"줄의 경우 나는 "* 현재 = 새로운 노드;"라고 말할 것인가? 또는 "current = new * node;" 아니면 완전히 다른 것? – Tristan

+0

'current '는 이제'createList' 함수 호출자의 스택에있는 원래 포인터를 가리키는 포인터이기 때문에'* current = new node;'가 될 것입니다 – Jason