2017-12-01 5 views
-3

아래 코드에서 링크 목록 구현에 내적 계산식을 구현하려고하는데 아래 오류가 있습니다. 'B'의 'add_node'요청, 포인터 유형 'linked_list {일명 노드 *}'(어쩌면 당신은 '->'를 사용하겠습니까?) 어떻게하면 그것을 지우고 작동 코드를 만들 수 있습니까? 나는 잘도트 제품 계산 링크 목록 구현

#include <iostream> 
#include <stdlib.h> 
using namespace std; 

struct node 
{ 
    int data; 
    int index; 
    node *next; 
}; 
typedef node* linked_list; 

node *head = NULL; 
node *tail = NULL; 

void add_node(int i,int n) 
{ 
    node *tmp = new node; 
    tmp->index = i; 
    tmp->data = n; 
    tmp->next = NULL; 
    if(head == NULL) 
    { 
     head = tmp; 
     tail = tmp; 
    } 
    else 
    { 
     tail->next = tmp; 
     tail = tail->next; 
    } 
} 
void display(node *head) 
{ 
    while(head!=0) 
    { 
     cout << head->index <<" ," << head->data << endl; 
     display(head->next); 
     break; 
    } 
} 
int main() 
{ 
    linked_list A; 

    A.add_node(2,7); 
    A.add_node(4,5); 
    A.add_node(7,8); 
    A.add_node(9,4); 

    linked_list B; 

    B.add_node(3,5); 
    B.add_node(4,6); 
    B.add_node(9,5); 

    int product=0; 

while(A!=0 && B!=0) 
{ 
    if(A->index == B->index) 
    { 
     product = product + A->data * B->data; 
     A=A->next; 
     B=B->next; 
    } 
    else if(A->index < B->index) 
    { 
     A=A->next; 
    } 
    else 
    { 
     B=B->next; 
    } 
} 
return product; 
    return 0; 
} 
+0

'node' 구조체에는 멤버 함수가 없습니다. 일반적으로 포인터가있는 typedef를 사용하면 슬픔에 처하게됩니다. –

+1

나는이 선생님들이 하루에 20 번 여기에 오는 "연결된 목록 할당"질문을 위장 할 수있는 참신한 방법을 제시 할 수 있다고 말해야합니다. 연결된 목록을 사용하여 내적을 계산 하시겠습니까? 무엇 향후 계획? – PaulMcKenzie

+0

하나의 글로벌'head'와'tail'에서 두 개의 서로 다른 링크드리스트를 실행하려고합니다. 이것은 작동하지 않습니다. 두 가지를 나눌 링크드리스트 클래스를 만들고 싶습니다. – user4581301

답변

0
#include <iostream> 
using namespace std; 

struct node 
{ 
    int data; 
    int index; 
    node *next; 
}; 
class linked_list 
{ 
private: 
    node *head,*tail; 
public: 
    linked_list() 
    { 
     head = NULL; 
     tail = NULL; 
    } 
    void add_node(int i,int n) 
    { 
     node *tmp = new node; 
     tmp->index = i; 
     tmp->data = n; 
     tmp->next = NULL; 

     if(head == NULL) 
     { 
      head = tmp; 
      tail = tmp; 
     } 
     else 
     { 
      tail->next = tmp; 
      tail = tail->next; 
     } 
    } 
    node* gethead() 
    { 
     return head; 
    } 
}; 
    void display(node *head) 
    { 
     while(head!=0) 
     { 
      cout << head->index <<" ," << head->data << endl; 
      display(head->next); 
      break; 
     } 
    } 
int main() 
{ 
    linked_list A; 

    A.add_node(2,7); 
    A.add_node(4,5); 
    A.add_node(7,8); 
    A.add_node(9,4); 

    linked_list B; 

    B.add_node(3,5); 
    B.add_node(4,6); 
    B.add_node(9,5); 

    display(A.gethead()); 
    display(B.gethead()); 

    int product=0; 


    node *current_a = A.gethead(); 
    node *current_b = B.gethead(); 

    while(current_a != 0 && current_b!=0) 
    { 
     if(current_a->index == current_b->index) 
     { 
      product = product + current_a->data * current_b->data; 
      current_a=current_a->next; 
      current_b=current_b->next; 
     } 
     else if(current_a->index < current_b->index) 
     { 
      current_a=current_a->next; 
     } 
     else 
     { 
      current_b=current_b->next; 
     } 
    } 
    cout<<"\nDot Product : "<< product<<endl; 

    return 0; 
} 

    enter code here 
0

오류가 당신이 알아야 할 사항을 알려줍니다으로 클래스를 사용하지 않습니다. linked_list은 포인터입니다. 도트 연산자가 아닌 -> 연산자를 사용해야합니다.

또한 node 구조체에는 add_node()이라는 메서드가 없습니다. 실제로 그것은 어떤 메소드도 전혀 포함하지 않습니다.

+2

'add_node'는 멤버 함수가 아닙니다. 오류가 맞습니다. 필요합니다. -> 역 참조하려면,하지만 일단 당신이 새로운 오류가 발생합니다. –

+0

오, 네 말이 맞아. 나는 대답에 추가 할 것이다. –