Hi, I have to implement a class called linked list that must read from file the following information about persons: name, social security and age. Then I have to implement some methods for manipulate the data. The problem is that I don't know how to start the implementation, because I have to create a Class Person then a Class List and use them? How?

Any suggestion will be appreciated.

Dani AI

Generated

A simple, maintainable approach is to treat this as two responsibilities: a small value type Person and a list that stores Person objects (or pointers to them). Implement Person with std::string name, std::string ssn (preserve leading zeros and dashes), and int age, plus a constructor, accessors, and comparison operators (operator<, operator==) if you want ordered insertion or equality checks. For real code prefer the STL std::list<Person> for correctness and fewer bugs (see std::list reference); for learning, a step‑by‑step singly linked list walkthrough is useful (GeeksforGeeks linked list introduction).

The compiler error you saw is classic name shadowing: a parameter and a local variable used the same identifier. Remove the local redeclaration and either assign to the reference parameter or rename it to make intent clear. Also ensure comparisons in in_list use the Person comparison operators you implemented, and use nullptr in modern C++. A concise skeleton looks like this:

bool LinkedList::in_list(const Person &key, Nodeptr &prev)
{
    Nodeptr cur = myFirst;
    prev = nullptr;
    while (cur && key > cur->data) { prev = cur; cur = cur->next; }
    return (cur && !(key < cur->data));
}

Practical implementation steps: (1) implement Person fully (ctors, getters, operator</==), (2) make Node hold a Person member and a next pointer, (3) implement insert, erase, find/in_list, display, and a destructor that frees nodes (or use smart pointers), (4) read the file using ifstream (use std::getline for names that contain spaces, read SSN as string, age as int), and (5) test with small inputs and a memory checker. As advised, drive the list from main() by creating the list, inserting several Person objects, removing some, and printing the results.

Recommended Answers

All 4 Replies

Read you text book to see how to create a class. Then you can use google to find examples of a linked list class. The Person class should be straight forward -- just follow the example in your text book of in any of the hundreds of tutorials you can find on the net.

After you get started post the code you have done and we'll be glad to help you out.

Read you text book to see how to create a class. Then you can use google to find examples of a linked list class. The Person class should be straight forward -- just follow the example in your text book of in any of the hundreds of tutorials you can find on the net.

After you get started post the code you have done and we'll be glad to help you out.

typedef int ElementType;

class LinkedList
{
      private:
              class Node
              {
                    public:
                           Node *next;
                           ElementType info;
                  
              };
              typedef Node * Nodeptr;
      
      public: 
             LinkedList(); // constructor
             ~LinkedList(); // destructor
             
             bool in_list(ElementType, Nodeptr&);
             bool empty();
             
             void display(ostream & out); 
             void erase(ElementType);
             void insert(ElementType);

      private:
              int mySize; // number of nodes
              Nodeptr myFirst; //  point to first node. 
             
};

ostream & operator<<(ostream & out, const LinkedList & aList);

Then I'm supposed to read the information about the person. I must create a class person, but I don't what I must do later?

Where did you get that linked list? the class Node doesn't make sense because it doesn't contain any data other than an integer that says what kind of data its supposed to contain.

The implementation is in the main() program. Create an instance of the linked list class, add several Person objects to it, delete a Person object, print all of them, etc. Before you can do any of that you have to create the Person class and its methods.

Where did you get that linked list? the class Node doesn't make sense because it doesn't contain any data other than an integer that says what kind of data its supposed to contain.

The implementation is in the main() program. Create an instance of the linked list class, add several Person objects to it, delete a Person object, print all of them, etc. Before you can do any of that you have to create the Person class and its methods.

I got it from my book ADTs, Data Structures, and Problem Solving with C++ of Larry Nyhoff.

class Person
{
      public:
             Person();
             
      private: 
               string name,
                      ss;
               int age;
};

Before I can use an instances of Class Linked List to manipulate an object of type Person I must change something in Class Linked List?

BTW I getting error from this Linked List method:

bool LinkedList::in_list(ElementType item, Nodeptr & anterior)
{ // in_list began
	Nodeptr p = myFirst,
            anterior = NULL; // previous
            
	while ( (p!=NULL) && (item > p->info) )
	{
		anterior = p;
		p = p->next;
	}
	if ( (p==NULL) || (item < p->info) ) 
		return false;
	else 
         return true;
} // in_list end

Line 33 declaration of 'LinkedList::Node*anterior' shadows a parameter

Thanks -

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.