Linked list and dynamic array

This is a discussion on Linked list and dynamic array within the C++ Programming forums, part of the General Programming Boards category; Here is my problem. I have created a class student which is to add/remove student names from a dynamic array ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Linked list and dynamic array

    Here is my problem. I have created a class student which is to add/remove student names from a dynamic array student_name. Suppose each student will also has a list of courses created with linked list. So, I created a struct course but I don't know how to link a course list to each student.

    Could someone please show me how to link the course to each student?

    Is it better to create another course class and implement it with link list?

    Are there any better ways?

    Thank you in advance.

    ========
    student.h
    ========
    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    #include <string>
    
    namespace jk1998_A
    {
          class student
          {
                public:
                       static const int DEFAULT_CAPACITY;
                       student(int initial_capacity = DEFAULT_CAPACITY);
                       void reserve(int new_capacity);
                       void insert(const std::string& entry);
                       bool erase_one(const std::string& target);
                       struct course
                       {
                              std::string name;
                              double GPA;
                              double credit;
                              course* node;
                               
                       };
                private:
                        std::string* student_name;
                        int capacity;
                        int used;      
                
          };    
              
          
              
              
    }
    
    #endif
    =========
    implet_student.cpp
    =========
    Code:
    #include <string>
    #include <algorithm>
    #include "student.h"
    
    using namespace std;
    using namespace jk1998_A;
    
          const int student::DEFAULT_CAPACITY = 20;
          
          student::student(int initial_capacity)
          {
                 student_name = new string[initial_capacity]; 
                 capacity = initial_capacity;
                 used = 0;                
          }
    
          void student::reserve(int new_capacity)
          {
                  string* larger_array;
                  if (new_capacity == capacity)
                     return;
                     
                  if (new_capacity < used)
                     new_capacity = used;
                     
                  larger_array = new string[new_capacity];
                  copy(student_name, student_name + used, larger_array);
                  delete [] student_name;
                  student_name = larger_array;
                  capacity = new_capacity;
          }
          
          void student::insert(const string& entry)
          {
                  if (used == capacity)
                     reserve(used + 10);
                  student_name[used] = entry;
                  ++used;     
          }
          
          bool student::erase_one(const string& target)
          {
                  int index = 0;
                  
                  while ((index < used)&&(student_name[index].compare(target)!=0))
                        ++index;
                        
                  if (index == used)
                     return false;
                     
                  --used;
                  student_name[index] = student_name[used];
                  return true;     
               
          }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,972
    The way I see it, your student class should model a student. A student, from what I see, has a name and a (linked) list of courses the student has taken/is taking. Each course has a name, the GPA of the student, and the credit awarded.
    Code:
    // student.h
    #include <string>
    #include <list>
    
    class course
    {
    public:
        // ...
    private:
        std::string name;
        double GPA;
        double credit;
    };
    
    class student
    {
    public:
        // ...
    private:
        std::string name;
        std::list<course> courses;
    };
    Then, the dynamic array of students is easy:
    Code:
    #include <vector>
    #include "student.h"
    
    int main()
    {
        std::vector<student> students;
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    So, I should write two classes; a student and a course. Unfortunately, I can't use your suggestions because I have to implement my own dynamic array and link list, but your idea is good. Thank you

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,972
    Well, you can replace the std::list<course> with a linked list of course objects, and replace the std::vector<student> with a dynamic array of student objects. This would likely mean creating two more classes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which do you prefer, dynamic array or double linked list?
    By ITAmember in forum C Programming
    Replies: 14
    Last Post: 06-03-2009, 01:46 AM
  2. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  3. array of linked list
    By jjeanie in forum C Programming
    Replies: 2
    Last Post: 05-07-2007, 10:08 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. array, linked list & hashing
    By laasunde in forum C++ Programming
    Replies: 5
    Last Post: 05-13-2003, 11:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21