2012-08-23 2 views
1

연결된 목록을 사용하여 제목 전제 조건 검사기를 만들려고합니다. 단일 데이터를 노드에 삽입하는 방법을 알고 있습니다.C++ 연결된 목록 구현 다중 데이터 삽입

내 문제는 노드에 여러 데이터를 삽입하는 방법입니다. 제 임무를 완벽하게 수행하는 좋은 모범을 발견했습니다. 그러나 문제는 내가 C를 이해하지 못한다는 것입니다. 아래의 void add() 기능을 설명하는 데 도움을 줄 수 있습니까? 저의 add 기능을 제 임무에 사용하고 싶습니다.

#include <stdio.h> 
#include <conio.h> 
#include <malloc.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

struct node 
{ 
    char data [ 20 ]; 
    char m [ 5 ] [ 20 ]; 
    int mcount; 
    struct node * link; 
}; 

struct node * dic [ 26 ]; 

void add (char *); 
int search (char *); 
void show(); 
void deldic(); 

void main() 
{ 
    char word [ 20 ] , ch; 
    int i; 

    clrscr(); 

    while (1) 
    { 
     clrscr(); 
     printf ("\n\t\tDictionary\n"); 
     printf ("\n\t\t1.Add Word.\n"); 
     printf ("\t\t2.Search Word.\n"); 
     printf ("\t\t3.Show Dictionary.\n"); 
     printf ("\t\t0.Exit."); 
     printf ("\n\n\t\tYour Choice "); 
     scanf ("%d", &ch); 

     switch (ch) 
     { 
      case 1 : 

       printf ("\nEnter any word : "); 
       fflush (stdin); 
       gets (word); 
       add (word); 

       break; 

      case 2 : 

       printf ("\nEnter the word to search : "); 
       fflush (stdin); 
       gets (word); 
       i = search (word); 
       if (! i) 
        printf ("Word does not exists."); 
       getch(); 

       break; 

      case 3 : 

       show(); 
       getch(); 

       break; 

      case 0 : 

       deldic(); 
       exit (0); 

      default : 

       printf ("\nWrong Choice"); 
     } 
    } 
} 

void add (char * str) 
{ 
    int i, j = toupper (str [ 0 ]) - 65; 
    struct node * r, * temp = dic [ j ], * q; 
    char mean [ 5 ] [ 20 ], ch = 'y'; 

    i = search (str); 
    if (i) 
    { 
     printf ("\nWord already exists."); 
     getch(); 
     return; 
    } 
    q = (struct node *) malloc (sizeof (struct node)); 
    strcpy (q -> data, str); 
    q -> link = NULL; 

    for (i = 0; tolower (ch) == 'y' && i < 5; i++) 
    { 
     fflush (stdin); 
     printf ("\n\nEnter the meaning(s) : "); 
     gets (mean [ i ]); 
     strcpy (q -> m [ i ] , mean [ i ]); 
     if (i != 4) 
      printf ("\nAdd more meanings (y/n) "); 
     else 
      printf ("You cannot enter more than 5 meanings."); 
     fflush (stdin); 
     ch = getche(); 
    } 

    q -> mcount = i; 
    if (dic [ j ] == NULL || strcmp (dic [ j ] -> data, str) > 0) 
    { 
     r = dic [ j ]; 
     dic [ j ] = q; 
     q -> link = r; 
     return; 
    } 

    else 
    { 
     while (temp != NULL) 
     { 
      if ((strcmp (temp -> data, str) < 0) && ((strcmp (temp -> link -> data, str) > 0) || 
              temp -> link == NULL)) 
      { 
       q -> link = temp -> link; 
       temp -> link = q; 
       return; 
      } 
      temp = temp -> link; 
     } 
    } 
} 

다음은 지금까지

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

struct subjectlist 
{ 
    string subject; 
    string prereq; 
    subjectlist *next; 
}; 

subjectlist *start_prt=NULL; 
subjectlist *current; 
int option=0; 

int main() 
{ 
int x; 
string subject; 

       cout << "1. Add subject" << endl; 
       cout << "2. Search prerequisite" << endl; 
       cout << "3. Delete subject" << endl; 
       cout << "4.Show subjects" << endl; 
       cout << "5. Save to file" << endl; 
       cout << "6. Load from file" << endl; 
       cout << "0. Exit" << endl; 
       cin >> x; 

       switch (x) 
    { 
     case 1: 
      cout<<"Input subject"<<endl; 
      cin >> subject; 
      add(subject); 
      break; 

     case 2: 
      cout<<"Input subject to be checked"<<endl; 
      break; 

     case 3: 
      cout<<"Delete a subject"<<endl; 
      break; 

     case 4: 
      cout<<"Show Subjects"<<endl; 
      break; 

     case 5: 
      cout<<"Save to File"<<endl; 
      break; 

     case 6: 
      cout<<"Load from file"<<endl; 
      break; 

     case 0: 
      cout<<"exit"<<endl; 
      break; 


     default: cout <<"Invalid selection, please try again."<<endl; 
    } 
} 

void add() 
{ 

} 
+0

"연결된 목록을 사용하여 주체 전제 조건 검사기를 만들려고 했습니까?"또는 연결된 목록을 구현하려고합니까? 전자의 경우에는'std :: list'를 사용하십시오. – juanchopanza

+0

std :: list와 같은 STL을 사용하지 않아도됩니다. – user1619170

+0

@JoachimPileborg 사과 IE7 직장에서 그것은 squashes 태그를 –

답변

1

add() 기능을 나열하는 노드를 추가 내 임무이다. 그러나 노드를 목록에 추가하기 전에 노드의 데이터가 이미 목록에 있는지 여부를 확인합니다.

i = search (str); 
    if (i) 

중복 된 데이터를 확인합니다.
데이터가 이미 목록에있는 경우 노드가 목록에 삽입되지 않습니다.

데이터가 목록에 없으면 추가로 이동합니다. 루프

for (i = 0; tolower (ch) == 'y' && i < 5; i++) 

이 배열에 meaning (string)를 받아 5 meaning의 노드 당 첨가 할 수있다. 또한 노드가 목록에 정렬되어 목록이 정렬 된 방식으로 추가됩니다.

1

언어를 사용하는 C++에서는 객체 지향 프로그래밍을 지원하기 때문에이 기능을 사용하지 않는 이유는 무엇입니까?

먼저 모든 유용한 항목을 포함하고 저장하려는 데이터 구조를 만들 수 있습니다. 어떤 데이터 개체 중 하나, 다음에 대한 포인터를 보유하고 (아마에, 그럼 당신은 노드 클래스를 만들 수

struct Data 
{ 
    char data [20]; 
    char m [5][20]; 
    int mcount; 

    bool operator==(const Data& other)const 
    { 
     //probably you need more comparisons 
     return mcount==other.mcount; 
    } 
}; 

: 당신은 또한 어떤 두 개의 데이터 개체를 비교하는 것이 훨씬 명확하게 == 연산자를 쓸 수 이전 항목). 당신이 가지고있는 경우 당신이 원하는, 훨씬 명확하게 보일 것이다) (주에서 다음

class MyLinkedList 
{ 
    Node * head; 

    public: 

    MyLinkedList(){//initialization steps} 

    ~MyLinkedList(){ //Delete the list} 

    void add(Data item) 
    { 
     if(!contains(item)) 
     { 
      //append it 
     } 
    } 

    bool contains(Data item){ //... check if the list already contains item} 

    //create a string representation of the object. 
    //If you dont like this style, you could also provide 
    //an operator>> or operator<< for the class 
    std::string toString() 
    { 
     std::stringstream stream; 
     //iterate through the list, and add elements with 
     return stream.str(); 
    } 
}; 

:이있어 후

struct Node 
{ 
    Data data; 
    Node * next; 
    //Node * previous; 
} 

, 당신은 당신의 자신의 연결리스트 클래스를 만들 수 있습니다 :

MyLinkedList list; 

Data data; //somehow fill it 

//adding items 
list.add(data); 

//printing the list 
cout<<list.toString(); 

//after it goes out of scope the destructor will be called, 
//so you dont need to bother with the deletion. 
관련 문제