C ++链表实现多个数据插入

时间:2012-08-23 08:50:54

标签: c++ linked-list

我正在尝试使用链接列表创建主题先决条件检查程序。我知道如何将单个数据插入节点。

我的问题是如何将多个数据插入节点?我找到了一个很好的例子,完全适合我的任务。但问题是我不太了解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 ()
{

}

2 个答案:

答案 0 :(得分:1)

add()函数将节点添加到列表中。但在将节点添加到列表之前,它会检查节点中的数据是否已存在于列表中?

 i = search ( str );
   if ( i )

检查重复数据 如果列表中已存在数据,则节点不会插入列表中。

如果列表中没有数据,它会进一步移动。

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

此for循环接受数组中的meaning (string),每个节点只能添加5 meaning个。 此外,节点也会以列表形式添加到列表中。

答案 1 :(得分: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; 
    }
};

然后你可以创建一个Node类,什么包含你的一个Data对象,以及一个指向下一个(可能是前一个)项的指针。

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

获得此功能后,您可以创建自己的链接列表类:

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();
    }
};

如果你有了这个,那么在你的主()中它会更清楚,你想要什么:

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.