0

So I am trying to improve this code in c++. What this does it creates two classes: Student and Studentlist. Any suggestions on improving the linked list data structure here will be greatly appreciated.

#include <iostream>

using namespace std;

//declaring a class student 

class Student
{

public:
    char *RollNo;
    Student *next;
    //function student that includes arguments roll number and a pointer poniting to next node student

    Student(char *rollNo, Student *Next)
    {
        this->RollNo = rollNo;
        this->next = Next;
    }
    //fucntion to get roll number 
    char* getRollNo()
    {
        return RollNo;
    }
    //function to get the pointer to next node named student
    Student *getNext()
    {
        return next;
    }

    void setNext(Student *aNode)
    {
        this->next = aNode;
    }
};

//declareing a class StudentList

class StudentList
{
public:
    Student *head;
    // default constructor
    StudentList()
    {
        head = NULL;
    }
    void Add(char *aRollNo)
    {
        Student *newStudent = new Student(aRollNo, NULL);
        Student *temp = head;
        if (temp != NULL)
        {
            while (temp->getNext() != NULL)
            {
                temp = temp->getNext();
            }
            temp->setNext(newStudent);
        }
        else
        {
            head = newStudent;
        }
    }
    void display()
    {
        Student *temp = head;
        if (temp == NULL)
        {
            cout << "no student data in the Student List" << endl;
            return;
        }
        if (temp->getNext() == NULL)
        {
            cout << temp->getRollNo();
        }
        else
        {
            do
            {
                cout << temp->getRollNo() << " --next--> ";
                temp = temp->getNext();
            } while (temp != NULL);
            cout << " end --> null" << endl;
        }
    }
};

main()
{
    StudentList list;
    list.Add("My Roll Number is 411\n");
    list.display();
    cout << "--------------------------------\n";
    system("pause");
    return 0;
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
Darthvader
  • 3
  • 1
  • 2

2 Answers2

0

The declaration of main() is not complete.

 main()

See What is the proper declaration of main?

Also literal strings have a type of char const*. So your method call Add("XXX") has no matching point in the class. The closest you have is Add(char*) which does not match the const part.

Personally I would avoid using C-Strings in C++. You should look at using std::string to handle strings of characters it will avoid many problems.

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

while you are always add at the last i recomended you to replace your Add algorithm code with this

Student* Add(char *aRollNo,Student* last)
{
    Student *newStudent = new Student(aRollNo, NULL);
    Student *temp = last;
    if (head == NULL){
        head=newStudent;
        temp=head;}
    else
        temp=temp->setNext(newStudent);
  return temp;     
}
  • if you are looking for speed this will be a fine algorithm for Add especially if you have a long list of Students then the while loop will take pretty long time of processor first you pass NULL then you pass the returning value – Mohammad Al Baghdadi May 18 '15 at 01:36