C++/C++

Advertisement


Question
Please i want to cheack for this code
Sorted Doubly Linked List with Insertion and Deletion

#include <iostream.h>
#include <cstdlib.h>
#include <string.h>
#include<conio.h>
using namespace std;

class  student info,Dllist
{
  private:
Long studid;
Char name[10];
Float cgpa;
      typedef struct Node
      {
         string name;
         Node* next;
         Node* prev;
      };
      Node* head;
      Node* last;
  public:
void input();
void display();
} student[5];
void studentinfo:: input()
{
cout << "please input student information:";
for(i=0;i<5;i++)
{
Cout<<”nn student”<<(i+1)<<”n”;
Cout<<”enter studentid:”;
cin >> student[i].studentid;

Cout<<”enter student name:”;
cin >> student[i].name;
cout << "Enter cgpa:";
cin >> student[i].cgpa;
}}

void studentinfo:: display()     {
Cout<<”n student information:”;

for(i=0;i<5;i++)
{
Cout<<”nn student”<<(i+1)<<”n”;
Cout<<”enter studentid:”<< student[i].studentid;
Cout<<”enter student name:”<<student[i].name;

cout << "Enter cgpa:"<< student[i].cgpa;

}}


Void main()
{
clrscr();
for(i=0;i<5;i++)
{
student[i].input();
}
for(i=0;i<5;i++)
{
student[i].display();
}



Dllist()
      {
        head = NULL;
        last = NULL;
      }
      bool empty() const { return head==NULL; }
      friend ostream& operator<<(ostream& ,const Dllist& );
      void Insert(const string& );
      void Remove(const string& );
};

void Dllist::Insert(const string& s)
{
   // Insertion into an Empty List.
   if(empty())
   {
      Node* temp = new Node;
      head = temp;
      last = temp;
      temp->prev = NULL;
      temp->next = NULL;
      temp->name = s;
   }
   else
   {
      Node* curr;
      curr = head;
      while( s>curr->name && curr->next != last->next) curr = curr->next;

      if(curr == head)
      {
        Node* temp = new Node;
        temp->name = s;
        temp->prev = curr;
        temp->next = NULL;
        head->next = temp;
        last = temp;
     //  cout<<" Inserted "<<s<<" After "<<curr->name<<endl;
      }
      else
      {
      if(curr == last && s>last->name)
      {
        last->next = new Node;
        (last->next)->prev = last;
        last = last->next;
        last->next = NULL;
        last->name = s;
     //  cout<<" Added "<<s<<" at the end "<<endl;
      }
      else
      {
        Node* temp = new Node;
        temp->name = s;
        temp->next = curr;
        (curr->prev)->next = temp;
        temp->prev = curr->prev;
        curr->prev = temp;
     //  cout<<" Inserted "<<s<<" Before "<<curr->name<<endl;
      }
     }
   }
}

ostream& operator<<(ostream& ostr, const Dllist& dl )
{
   if(dl.empty()) ostr<<" The list is empty. "<<endl;
   else
   {
       Dllist::Node* curr;
       for(curr = dl.head; curr != dl.last->next; curr=curr->next)
         ostr<<curr->name<<" ";
       ostr<<endl;
       ostr<<endl;
       return ostr;
   }
}

void Dllist::Remove(const string& s)
{
   bool found = false;
   if(empty())
   {
     cout<<" This is an empty list! "<<endl;
     return;
   }
   else
   {
     Node* curr;
     for(curr = head; curr != last->next; curr = curr->next)
     {
         if(curr->name == s)
         {
         found = true;
         break;
         }
     }
     if(found == false)
     {
      cout<<" The list does not contain specified Node"<<endl;
      return;
     }
     else
     {
        // Curr points to the node to be removed.
        if (curr == head && found)
        {
         if(curr->next != NULL)
         {
         head = curr->next;
         delete curr;
         return;
         }
         else
         {
         delete curr;
         head = NULL;
         last = NULL;
         return;
         }
        }
       if (curr == last && found)
       {
        last = curr->prev;
        delete curr;
        return;
       }
      (curr->prev)->next = curr->next;
      (curr->next)->prev = curr->prev;
       delete curr;
    }
 }
}

int main()
{
   Dllist d1;
   int ch;
   string temp;
   while(1)
   {
      cout<<endl;
      cout<<" Doubly Linked List Operations "<<endl;
      cout<<" ------------------------------"<<endl;
      cout<<" 1. Insertion "<<endl;
      cout<<" 2. Deletion "<<endl;
      cout<<" 3. Display "<<endl;
      cout<<" 4. Exit "<<endl;
      cout<<" Enter your choice : ";
      cin>>ch;
      switch(ch)
      {
         case 1: cout<<" Enter Name to be inserted : ";
         cin>>temp;
         d1.Insert(temp);
         break;
         case 2: cout<<" Enter Name to be deleted : ";
         cin>>temp;
         d1.Remove(temp);
         break;
         case 3: cout<<" The List contains : ";
         cout<<d1;
         break;
         case 4: system("pause");
         return 0;
         break;
getch();
}
      }
   }

Answer
Hello Maha

I hate to bring you bad news, but the code is having some trouble. When you are writing a program in a new language, you should not write it all out, and then try to compile it at the end. You should write a little bit, compile, and then write more. In that way, if you are misusing the language, you will be corrected before you spend too much time going down the wrong path. You would learn that all the C++ built in types start with lower case letters. The Long, Char, Cout, Float, Void, all start with lowercase. The compiler would tell you that there should be only  1 main function in your program. In fact, your first main function should just be deleted. There is nothing useful there.

You have some fundamental problems with your input and display methods. Those are method of your student_info class and so operate on one instance of the student_info. Although you have 5 students, all in an array, the input and display methods should not know aything about the array. They operate on only 1 student_info instance. You should remove the for(i=0;i<5;i++) loop from those methods.

I do not think you should have an array of students at all. Whenever the user chooses an insert operation, a new student should be created using the new operator. Then the student info can be input with the input method. Something like this:
student_info* pStudent = new student_info();
pStudent->input();

The links head and last, are not part of the student. It is not true that each student has a head and last because each student does not have a doubly linked list. The student is stored on a doubly linked list, but there is only 1 list, so there is only 1 head and 1 last for the entire program, not one for each student. The head and last should be moved out of student_info. They can be made global variables in the program.

Simplify your program by removing the Node struct from student. For simplicity, just have the links in the StudentInfo class, like this

class  StudentInfo
{
private:
   long studid;
   char name[10];
   float cgpa;

   StudentInfo *prev;
   StudentInfo *next;

public:
   void input();
   void display();
};

// global head and last pointers
StudentInfo* head = NULL;
StudentInfo* last = NULL;

It is not so nice to have the prev and next links right inside the StudentInfo, but it is good for a beginner.

Start with the class above, then write the input and display methods. Write a simple main funtion to create a new student, get the input, and display it. Make it compile. Show me that effort and I will know you are serious and I will help you more.

Best regards
Zlatko  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.