Solved

Linked Lists

Posted on 1998-07-16
8
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 10 total points
ID: 1168009
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
ID: 1168010
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
ID: 1168011
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1168012
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
 
LVL 7

Expert Comment

by:psdavis
ID: 1168013
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
ID: 1168014
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
ID: 1168015
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
ID: 1168016
Going to grade the answer?

Phillip

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

630 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