2012-04-04 2 views
0

내 "void union"함수에서 "AUB"가 void 함수 내에 있지 않기 때문에 사용자가 각각 입력 한 연결된 목록 "A"와 "B"모두에서 데이터를 삽입하는 방법을 확신 할 수 없습니다. 나는 넣었을 것이다 :linked list union

AUB.insert 

나는 확실하지 않았다. 어떤 제안?

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

class Sets 
{ 
private:struct NODE 
     { 
      int info; 
      NODE *next; 
     }; 
     NODE *list; 
public:Sets() 
     { 
      list=NULL; 
     } 
     void Insert(int x) 
     { 
      NODE *p=list, *q=list, *r; 
      //create a new node 
      r = new (NODE); 
      r->info = x; 
      r->next = NULL; 
      //find the insertion place 
      while(p != NULL && p->info < x) 
      { 
       q=p; 
       p=p->next; 
      } 
      if(p==list)//x is the first info 
      { 
       list=r; 
       r->next=p; 
      } 
      else if(p==NULL)//x is the last info 
      { 
       q->next=r; 
      } 
      else //x is neither forst nor last info 
      { 
       r->next=p; 
       q->next=r; 
      } 
     } 
     void display() 
     { 
      NODE *p=list; 
      while(p != NULL) 
      { 
       cout << p->info << "-->"; 
       p=p->next; 
      } 
      cout << "NULL\n"; 
     } 
     void Union(Sets setA,Sets setB) 
     { 
      NODE *p=setA.list, *q=setB.list; 
      while(p != NULL && q != NULL) 
      { 
       if(p->info > q-> info) 
       { 
        (q->info) 
        q=q->next; 
       } 
       else if(p->info == q->info) 
       { 
        insert(p->info) 
        p=p->next; 
        q=q->next; 
       } 
       else//P->info < q->info 
       { 
        insert(p->info); 
        p=p->next; 
       } 
      } 
      while(p !=NULL) 
      { 
       insert(p->info); 
       p=p->next; 
      } 
      while(q != NULL) 
      { 
       insert(q->info); 
       q=q->next; 
      } 
     } 
}; 


int main() 
{ 
    //create a set of integers 
    int x; 
    Sets A, B, setAUB; 
    cout << "Enter data for setA:\n"; 
    cout << "Enter a group of positive integer numbers with -1 at the end end: "; 
    cin >> x; 
    while(x != -1) 
    { 
     A.Insert(x); 
     cin >> x; 
    }; 
    //display setA 
    cout << endl << "setA="; 
    A.display(); 

    cout << "Enter data for setB:\n"; 
    cout << "Enter a group of positive integer numbers with -1 at the end end: "; 
    cin >> x; 
    while(x != -1) 
    { 
     B.Insert(x); 
     cin >> x; 
    }; 
    //display setB 
    cout << endl << "setB="; 
    B.display(); 

    setAUB.Union(A, B); 
    //display setAUB 
    cout << endl << "setAUB="; 
    setAUB.display(); 

    system ("pause"); 

    //terminate program 
    return 0; 
}; 
+2

'void Union()'은 함수의 나쁜 이름입니다. 의미있는 것으로 변경해야합니다. –

+0

좀 더 자세히 설명해 주시겠습니까? 함수 목적은 A와 B를 결합하는 것입니다. – BuzzSmarter

+0

union은 "union"을 정의하는 키워드입니다.이 경우 대문자 만 사용하는 키워드와 다른 functinon을 정의 할 때 잘못된 가정으로 이어질 수 있습니다 – Alex

답변

0

정의 할 내용 : void Union(Sets setA,Sets setB).

뭐하시는 거예요? 둘 다 값으로 전달되고 반환 값은 void입니다. 결과는 어디에 있습니까?

현재 객체 (Union 함수의 this)가 해당 공용체가됩니까? 그렇다면 이미 들어있는 데이터는 어떻게됩니까? 당신은 그것을 삭제하지 않는, 그래서 당신은 기본적으로 세 가지 세트가 아닌 두 ... 병합하고

나는 두 개의 매개 변수와 반환새로운를 취할 것 정적merge 함수를 만드는 제안

이 둘의 병합 일 것입니다.

그렇지 않은 경우 일반 매개 변수 merge을 만들고 매개 변수 1 개만 가져 와서 현재 개체에 병합하십시오.

부두 - 왜 Sets, 분명히 정렬 된 링크 된 목록일까요?

+0

1. 값을 전달했습니다. setA와 setB를 함수에 넣기 위해 p와 q를 사용하여 그 블록과 결과가 나오는 것을 가리킨다. 2. 세 번째 세트를 지워서 삭제했습니다. 3.Sets는 orderedlinkedlist보다 짧습니다! – BuzzSmarter

+0

@SeanFlores # 1과 # 2를 이해하지 못했습니다. 내가 작성한 코드는 게시 한 코드와 관련이 있습니다. 다시 # 3 - * 집합 *은 ** 아니 ** 목록 *입니다. 이 용어는 매우 잘 정의되어 있으며, 다른 사람들의 눈에는 그 의미가 없습니다. 그것의 * list * - 그것을 * set *이라고 부르지 마십시오. – littleadv