We help IT Professionals succeed at work.

Classes and linked lists? Help!

njones
njones asked
on
293 Views
Last Modified: 2013-12-14
Hi out there. I wonder if you can help me. I have just written a program in C++ for Windows that uses linked lists to create, modify, delete and display personal details.

Just for fun, but now i am wondering why, i am attempting to convert the program into classes. I am finding this excruciatingly hard as i have never used classes before. All the appropriate books don't seem to be using the struct command, and they don't show how to input details in a classes environment, like name, date of birth, etc.
I have searched the web for relevant examples as well, but i can't find any to match mine.

If anyone would like to attempt to convert my program (which follows below) into classes but at the same time trying to keep it simple, i would be very grateful, as i have tryed, but more often than not, ended up very angry and red!!!

As for changing the struct to a class, if it is possible, that would be good because i have used them for a bit now.

I am fairly interested in using classes more often. However, getting started with this is a nightmare.

If anyone does decide to have a go, comments would be appreciated as to why everybody raves about OOP, and why it would be beneficial for an amateur programmer, like myself, to use them.

------------------------------------------------------------

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
#include<string.h>

struct dif
{
      char name[30];
      char dob[30];
      char tel[30];
      struct dif *ptrnext;
};

struct dif *ptrfirst, *ptrthis, *ptrnew;

char temp[30];

void main(void)
{
      int command;

      void perform(int &);
      void wait(void);

      ptrfirst=(struct dif*)NULL;

      do
      {
            clrscr();
            cout<<"1:create/insert (a new structure into the linked list)\n";
            cout<<"2:modify (an existing structure)\n";
            cout<<"3:delete (an existing structure)\n";
            cout<<"4:display (a linked list)\n";
            cout<<"0:exit (from program)\n";
            cout<<"enter command:> ";
            cin>>command;
            perform(command);
      }while(command!=0);
}

void perform(int &option)
{
      void cresert(void);
      void modify(void);
      void del(void);
      void display(void);

      switch(option)
      {
            case 1:cresert();break;
            case 2:modify();break;
            case 3:del();break;
            case 4:display();break;
            case 0:clrscr();cout<<"end of session";break;
            default:clrscr();cout<<"error. invalid command. try again!";getchar();
            break;
      }
}

void wait(void)
{
      char ch;
      cout<<"\n\n\npress <return> to go back to menu";
      scanf("%c",&ch);
      flushall();
}

void cresert(void)
{
      clrscr();
      cout<<"1: create/insert\n";

      ptrnew=(struct dif*) malloc(sizeof(struct dif));

      if(ptrfirst==(struct dif*)NULL)
            ptrfirst=ptrthis=ptrnew;
      else
      {
            ptrthis=ptrfirst;
            while(ptrthis->ptrnext!=(struct dif*)NULL)
                  ptrthis=ptrthis->ptrnext;
                  ptrthis->ptrnext=ptrnew;
                  ptrthis=ptrnew;
      }

      printf("\nenter a name:> ");
      gets(ptrthis->name);
      printf("enter a date of birth:> ");
      gets(ptrthis->dob);
      printf("enter telephone number:> ");
      gets(ptrthis->tel);
      ptrthis->ptrnext=(struct dif*)NULL;
}

void modify(void)
{
      int b;

      clrscr();
      cout<<"2: modify\n";

      if(ptrfirst==(struct dif*)NULL)
      {
            cout<<"\nthere are no structures to modify";
            wait();
      }

      else
      {
            printf("\nenter name of person to have structure modified for:> ");
            gets(temp);
            ptrthis=ptrfirst;

            do
            {
                  b=strcmp(temp,ptrthis->name);

                  if (b==0)
                  {
                        printf("\nexisting name:> %s\n",ptrthis->name);
                        printf("\nexisting date of birth:> %s\n",ptrthis->dob);
                        printf("\nexisting telephone number:> %s\n",ptrthis->tel);
                        printf("\n\nenter new/old name:> ");
                        gets(ptrthis->name);
                        printf("enter new/old date of birth:> ");
                        gets(ptrthis->dob);
                        printf("enter new/old telephone number:> ");
                        gets(ptrthis->tel);
                        wait();
                        return;
                  }

                  else
                        ptrthis=ptrthis->ptrnext;
            }while(ptrthis!=(struct dif*)NULL);

            cout<<"\nthere are currently no structures with that name to modify";
            wait();
      }
}

void del(void)
{
      int b;

      clrscr();
      cout<<"3: delete\n";

      if(ptrfirst==(struct dif*)NULL)
      {
            cout<<"\nthere are currently no structures to delete";
            wait();
      }

      else
      {
            printf("\nenter name of person to have structure deleted for:> ");
            gets(temp);
            ptrthis=ptrfirst;

            do
            {
                  b=strcmp(temp,ptrthis->name);

                  if (b==0)
                  {
                        printf("\nthis structure has now been deleted");
                        ptrfirst=ptrthis->ptrnext;
                        delete ptrfirst;
                        wait();
                        return;
                  }

                  else
                        ptrthis=ptrthis->ptrnext;
            }while(ptrthis!=(struct dif*)NULL);

            cout<<"\nthere are currently no structures with that name to delete";
            wait();
      }
}

void display()
{
      clrscr();
      cout<<"4: display\n";

      if (ptrfirst==(struct dif*)NULL)
      {
            printf("\nno structures have been entered yet");
            wait();
            return;
      }

      ptrthis=ptrfirst;

      do
      {
            printf("\nname:> %s\n",ptrthis->name);
            printf("date of birth:> %s\n",ptrthis->dob);
            printf("telephone number:> %s\n",ptrthis->tel);
            ptrthis=ptrthis->ptrnext;
      }while(ptrthis!=(struct dif*)NULL);

      wait();
}
Comment
Watch Question

Commented:
Suggestions coming.

Commented:
Since you are trying to learn, I'll ofter some suggestions, not an answer, try them and get back to me.

First of all you will need two classes.  One for the linked list and another for the things in the linked list.  They will look like:

class dif;  // forward declaration because ptrfirst is a dif * before dif is defined.

class diflist
{
   dif *ptrfirst;
}

class dif
{
   char name[30];
   char dob[30];
   char tel[30];
   dif *ptrnext;

   friend diflist
};

Commented:
Now a couple of points.

1. the class diflist will store a linked list of dif objects.
2. the class dif was declared forward because the diflist class has a pointer to a dif, but dif hasn't been defined yet.
3.  The diflist class is declared as a friend of dif.  that allows an diflist to access private members of a dif  (if this doesn't make sense, ask).  That was done so that the diflist can access the ptrnext member of the dif class.

Now you try to make the following changes.
1.  add a constructor to the diflist class.   The constructor will initilize the diflist to have an empty linked list.  (that is the first item pointer will be NULL.)
2.  add a destructor to the diflist class.  The destructor will delete all the difs that are in the linked list.
3.  add a function to diflist that adds a new dif to the list.

Can you do these things?  Note this probably won't compile with these change, just put them in and let me know.  If you have questions ask.

If you want to e-mail me your results, my address is nietod@theshop.net.

Commented:
Do you want to change your struct to a class aswell???
//joakimf

Author

Commented:
Edited text of question

Commented:
Please don't edit the text of the question.  It makes it hard to follow the "train" of the comments.  Was the stuff I posted helpful?  do you have questions?
xyu

Commented:
netiod... it wold be better that you will sugest the guy Stroustrup's book on C++ (3d edition). He can find exaxt answers and examples on list's, etc. and many methodological stuff there :) (if we speaking about C++) :)

Author

Commented:
Adjusted points to 205

Commented:
This is a very big topic.  There are lots of details some of them very subtle.  There are two choices:  I can post the code you want and you will not understand it.  Or I can show you how to write that code.

I would choose the second option.

I've made some suggestions do you understand them?  Do you have questions about them?  Can you do them?
A struct by default is public. A class by default is private, so what I would do if you are not going to make it out into an ADT is to change the struct to a class and all the elements in it as public.
If you are going to make it into an ADT then you would have to do as nietod suggested and include a constructor with it which you should do any way with at least setting head to NULL..

Try something like:

class name
{
  ...elemnts...
 prt* next
};

Author

Commented:
Adjusted points to 215

Commented:
njones.  what do you want?  Do you want help or do you just want someone to submit some code that works?   I've offerend to help you get this done.  If you aren't interested let me know.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
I forgot to mention: You can also add "dif *ptrprev" to the class definition if you wish to add a pointer that points to the previous item in the list.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.