Solved

Linked Lists

Posted on 1998-07-16
8
180 Views
Last Modified: 2013-12-14
I am writing a program using linked lists in C++.  I need to have a program linked lists using employee last and first names, an insert procedure, a print procedure, a procedure to get new data, to delete and a main program.  I have had so many problems with the linked lists.  Can you please send me a linked list program that will allow me to do this in C++.  I am just so confused.  I'm having the most problems with understanding the declaring and using objects, constructors, destructors and calling the functions as well as the proper setup of the functions to use given variables.  Please Help!!

Sincerely,

Desperate to Understand...
0
Comment
Question by:Gilkgray
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 10 total points
Comment Utility
Sounds like you have one heck of a project on your hands.

Have you considered using MFC to do your development?  It already has database support for your employees, printer capabilities, as well as complete CObList and CObArray support for linked lists.  It would sure save you a lot of time.

Linked lists in C++ can be either based on a class or on a structure.  Included in a class or structure are pointers to a similar class.  When adding a entity to a linked list, use the 'new' operation to create it, and use the address of the entity to store in the original entity.  While traversing the list, start at the original node and use the pointers to go to the next node.

I hope I started to talk you out of using your own personalized linked list.  There's entire college courses taught about the subject.  Use CObList if you can.

Phillip
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
psdavis,
>> There's entire college courses taught about the subject.
And this is one of them!!!  This is an assigment.  (thus comercial classes would not be accepted)  

Gilkgray,
We cannot do your work for you.  That is unethical and is grounds for removal fromt eh site based on the customer service agreement we all agreed to (you as well).

We can ofter support in the same way your professor would:  We can answer very specific questions.  We can review your work and make suggestions for you to try.  But you have to do the work.
0
 

Author Comment

by:Gilkgray
Comment Utility
This is the linked list program that I have come up with, but am having problems with.  Is there some way that you can help me to figure out what I'm doing wrong within this program?


//********************************************************
// lab3.cpp
//This C++ program demonstrates the use or method of an
//object-oriented linked list.  The program keeps track of
// employee name (firstname, lastname).
//********************************************************

 
#include <string.h>
#include <iostream.h>
#define Name_Length 30

// Definition for parent, base, or root class object

class Employee
{
class Node;
protected:
      char LastName [Name_Length],
           FirstName [Name_Length];
      Employee *pointer;
      Employee *NextNodePtr;
public:
      Employee (char *LName, char *FName)
      {
            strcpy (LastName, LName);
            strcpy (FirstName, FName);
            NextNodePtr = 0;
      }

Employee()
      {
            LastName[0] = NULL;
            FirstName[0] = NULL;
            NextNodePtr = 0;
      }

void InitLastName(char *LName)
{
      strcpy (LastName, LName);
}

void InitFirstName(char *FName)
{
      strcpy (FirstName, FName);
}

virtual void Add_Data (void){
}

virtual void Send_Data (void)
 {
      cout << "\n\n" << LastName << ", " << FirstName << "\n";
 }
};

//Definition for child or derived class object

class Employee_Record:public Employee{

class Node;
public:
      Employee_Record(char *LName, char *FName):
                  Employee(LName, FName)

      {}
      Employee_Record():Employee(){}

      void Add_Data(void)
      {
          pointer = this;
      }

      void Send_Data(void)
      {
          Employee::Send_Data();
      }
};

class Node{

private:
      Employee *location;

public:
      Node()
      {
          location = 0;
      }

      void Print_Record (void);
      void AddEmployee (Employee *Node);
      void RemoveEmployee (char *LName, char *FName);
      
      };

      void Node::Print_Record(void)
      {
          Employee *present = location;
          while (present != 0 ) {
            present -> Send_Data();
            present = present -> NextNodePtr;
      }
}

void Node::AddEmployee (Employee *Node)
{
        Employee *current_node = location;
      Employee *previous_node = 0;
      while (current_node != 0 &&
            strcmp (current_node -> LastName, Node->LastName) < 0) {
            previous_node = current_node;
            current_node = current_node->NextNodePtr;
}

void Node::Remove_Employee (char *LName, char *FName)
{
      Employee *current_node = location;
      Employee *previous_node = 0;
      while (current_node != 0 &&
            strcmp (current_node -> LName, FName) != 0 {
            previous_node = current_node;
            current_node = current_node -> NextNodePtr;
}
if (current_node != 0 && previous_node = 0) {
      location = current_node -> NextNodePtr;
      // deletes current_node; needed if new() used in Add_Data()
      }

      else if (current_node != 0 && previous_node != 0) {
            previous_node -> NextNodePtr = current_node -> NextNodePtr;
            // deletes current_node; needed if new() used in Add_Data()
            }
}

void main (void)
{
      Record EmployeeList;
      EmployeeRecord E1 ("Williams", "Sandy");
      EmployeeRecord E2 ("Gilkey", "Lillie");
      EmployeeRecord E3 ("Stone", "Melvin");
      EmployeeRecord E4 ("Atkins", "Gene");
      EmployeeRecord E5 ("Jones", "Jacqueline");

      EmployeeList.AddEmployee (&E1);
      EmployeeList.AddEmployee (&E2);
      EmployeeList.AddEmployee (&E3);
      EmployeeList.AddEmployee (&E4);
      EmployeeList.AddEmployee (&E5);

      EmployeeList.PrintRecord();

      EmployeeList.RemoveEmployee ("Williams");

      cout << "\n\n========================================";

      EmployeeList.PrintRecord();

}
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
you've got the right idea more or less. First of all some questions

what is the goal?  I see you have an employee that is (mostly) correct) but you are trying to derive from it and I'm not sure why.  Then we have a node class.  what is that? is that supposed to be what is stored in the linked list?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:psdavis
Comment Utility
Sigh...  What exactly is your first problem?
10 points is pretty menial for much of a solution.

Some general remarks.  

1) You have a "class Node;" in your class Employee and in your class Employee_Record.  Remove it, it's not doing anything.
2) Always have default destructors for all your classes.  Especially for linked lists, you'll have memory leakage all over the floor.
3) What's the "Employee *pointer" in your class Employee?  It looks erroneous.
4) Use tabs man!  It makes your code much more readable.
5) Don't have two classes for both Employee and Employee_Record.  One will do the job fine.
6) I'm probably just missing it, but what is "Record EmployeeList" in your first line of main?  Is it supposed to be Node EmployeeList?

Phillip


0
 
LVL 2

Expert Comment

by:VEngineer
Comment Utility
First off, some semantics issues:
-- In C++, it is not void main(void), it is int main(), and it matters behind the scenes.

-- You add items to your list like this:
EmployeeRecord E1 ("Williams", "Sandy");
EmployeeList.AddEmployee (&E1);

this is not C++ semantics.  You should write it like this instead:

Employee* e1 = new Employee("Williams", "Sandy");
list.add(e1);

Or even better, take psdavis's suggestion and have the whole employee list be one class such that you hide the implementation details to the user.. i.e. something like this in main:

EmployeeList theList;
theList.add("Williams", "Sandy");

and you do all your linked list handling withing the EmployeeList class.

Do you have a textbook on data structures?  The implementation for a linked list as well as many other data structures are usually explained as well as written out in code.
I suggest you check out "Data Structures in C++" by Timothy Budd.  It will teach you how to implement each data structure using classes in C++ as well as teach you how to use the Standard Template Library to do the equivalent.

0
 

Author Comment

by:Gilkgray
Comment Utility
Thank you all for your help.  It was extremely useful.  I have the idea understood for the first time.  You all were of a great.  I could not have done it without you.

Again, thanks...

Sincerely, no longer confused.
0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
Going to grade the answer?

Phillip

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now