using linked list store data

I am writing a program about storing some data using linked list.However, I dont know the steps to store the data.Could you help me to do that?

Here is the class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class info
{
  private: 
  info *nextPtr;
  int age;
  string name;

  public:
  info *getNext()
  { 
    return nextPtr;
  }
  void setNext(info *ptr)
  {
    nextPtr=ptr;
  }
  int getAge()
  {
    return age;
  }
  void setAge(int x)
  {
    age = x;
  }
  string getName()
  {
    return name;
  }
  void setName(string y)
  {
    name = y;
  }
}


The following programs are the main program
Do I use array is correct?
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  info *record[]
  
  record[0}->setName("Lily Smith");
  record[0]->setAge(18);
  record[1]->setName("Sam Anderson");
  record[1]->setAge(21);

  // I dont know how to write the following program to displace the above record
}
/

plz help me ><
Last edited on
You shouldn't need an array. The whole point of a linked list is that the items are allocated dymanically and you can store any number of them.

Also, when writing code for a linked list, it helps to code it as two separate classes. The first class is for a single node in the list. That is what you have now. The second is a class for a list as a whole. This contains the pointer to the head of the list and the various operations you can perform on the list (find, insert, delete, whatever).

Although many people would disagree with me, I would not use get/set methods for your info class. They don't add any value in this case, they make the class declaration more than 4 times longer than it needs to be, and, most importantly, they make it impossible to get a reference or a pointer to the data members, which will make the code more complicated than it needs to be.
1
2
3
int main()
{
  info * record[]

1. Statement must end with semicolon.
2. How many elements are in the array? [] does not say.
3. What elements are in the array? info * Pointers. uninitialized pointers.
4. Thus, you don't have any info objects to fill with data.
5. Array? It was a linked list that you are learning. Your class is at most a node in a list.
How to refer to first node of a list?
How to create a node?
How to remove a node?
6. Node's constructor should initialize members sensibly.
Topic archived. No new replies allowed.