1 Replies - 720 Views - Last Post: 23 August 2012 - 03:17 AM Rate Topic: -----

#1 Vestorimin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-August 12

C++ linked list implementation multiple data insertion

Posted 23 August 2012 - 02:04 AM

Ok here it goes.

Im trying to create a subject pre-requisite checker using linked list. I know how to insert single data into a node.

My problem is how to insert multiple data into a node? I found a good example which fits my assignment perfectly. But the problem is I do not understand C very much. Can any one help to explain the void add() function below? I want to use that add function to my assignment.

#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;
       }
   }
}



Here is what I have done for my assignment so far
#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 ()
{

}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ linked list implementation multiple data insertion

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: C++ linked list implementation multiple data insertion

Posted 23 August 2012 - 03:17 AM

The code you're copying and pasting is crap, like so much of the code out there.

Suggestion: if you're in a C++ class, try taking what you've been taught and applying it, rather than being a lazy copy/paste coder.

EDIT: Did you create a new account here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1