User friendly information of student linked list

sahasrara 0 Tallied Votes 160 Views Share

Hi ladies and gentlemen
This program is a sample of data structure for information of students . this data structure is Linked List that include public & private filds.
Its private has the other class which i named it node.
node builds physical structure of linklist class.
node class consist of only public filds. because the public method of linkelist class be able to access to filds of it( node class).
node consist of first name, last name, address ,student number , age ,5 scores of student and average cell that must be we calculate average and fill it .
Program main function is user friendly .
This program get information and sorted it at the same time to average record.
if you want to edit these information you can enter a number of edit choice , and do it. if you'll change scores of student ,certainly its average will change.thereupon list must be sorted , hence the sort2 function , will sort it by bubblesort instruction again.
main also , has remove function that be able to delete a record which you enter its student last name.
This class have a method which , find maximum and minimum average of list.
meantime, i get character filds by char* syntax , because bubble sort can be doing its work!
If you get character filds by char[] , its bubble sort will be changed.
If you want its subprogram first think and providing unsuccessfully send me report.
Please pardon me if I have misspelling & incorrect phraseology

//Student information in sorted linked list
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/1/9

#pragma once
#include <iostream>
#include <string>

    class Linklist
    {
        class node{
        		public:
            char *address;
            char *name;
            char *lname;
            char *stno;
            float scores[5];
            float avg;   // haminja bayad yek filde moaddel begirid
            int age;
            node * next;
            }*first;
        public:

            Linklist(){  first = NULL;  }
            ~Linklist(){};
            int count();
            void add();
            void search();
            void Remove(char*);
            void MaxMin();
            void sort(node*);
            void sort2();
            void disply();
            void Edit(char*);
    };
///////////////////////////////////////////////////////////////////////////////

    void Linklist :: add()
    {
        char flag[3];
        node*q;
        do{
        q = new node;
        float sum = 0;
        cin.ignore();
        q->name = new char[20];
        q->lname = new char[20];
        q->stno = new char[10];
        q->address = new char[70];
        cout<<"\nName:";
        cin.getline(q->name,20);
        cout<<"Last name:";
        cin.getline(q->lname,20);
        cout<<"Age = ";
        cin>>q->age;
        cin.ignore();
        cout<<"Address : ";
        cin.getline(q->address,50);
        cout<<"stno:";
        cin.getline(q->stno,10);
        cout<<"scores:\n";
        for(int i=0 ; i<5 ; ++i)
        {
        		cout<<"a["<<(i+1)<<"] = ";
        		cin>>q->scores[i];
            sum +=(q->scores[i]);  	//sum +=(q->scores);
        }

        q->avg = sum/5;

        q->next=NULL;
        sort(q);
        cin.ignore();
        cout<<"Do you want to continue? if no type END : ";
        cin.getline(flag,4);

        }while(strcmp(flag ,"end")!=0 && strcmp(flag ,"END")!=0);          // while(flag=='end'|| flag=='END');
    }
///////////////////////////////////////////////////////////////////////////////

 void Linklist :: sort(node*temp)
 {
 node *temp2,*s;
 if(first==NULL)
 {
	first=new node;
	first=temp;
	first->next=NULL;
 }

 else if(first->avg >= temp->avg)
 {
   temp->next=first;
   first=temp;
 }

 else if(first->avg < temp->avg)
 {
	temp2=first;                                       //|                                                         |
	while(temp2->next  &&  temp2->avg <= temp->avg )   //| while(temp2->next &&  (temp2->next)->num <= temp->num ) |
	{                                                  //|     temp2 = temp2->next;                                |
	 s=temp2;                                          //|                                                         |
	 temp2=temp2->next;                                //| temp->next  = temp2->next;                              |
	}                                                  //| temp2->next = temp;                                     |
                                                 		//|---------------------------------------------------------|

   if(temp2 && temp2->avg >= temp->avg)
   {
   	temp->next = s->next;
      s->next = temp;
   }

	else
   {
   	temp2->next = temp;
      temp->next = NULL;
   }

  }
 }

///////////////////////////////////////////////////////////////////////////////
 int Linklist :: count()
 {
 	if(!first)
		return 0;
   int c = 0;
   for(node *q=first ; q ; q=q->next)
   	c++;
   return c;
 }

///////////////////////////////////////////////////////////////////////////////

 void Linklist :: sort2()
 {
 	int n = count();
   int i,j;
   int   inthold;
   float floathold;
   char *charhold;
   char *charhold1;
   char *charhold2;

   node *q,*t;
   for(i=1, t=first ; i<=n-1 ; t=t->next, ++i)
   	for(j=0, q=first ; j<n-i ; q=q->next, ++j)
      	if(q->avg > (q->next)->avg)
         {
         	floathold = q->avg;
            q->avg = (q->next)->avg;
            (q->next)->avg = floathold;

            charhold = q->name;
            q->name = (q->next)->name;
            (q->next)->name = charhold;

            charhold = q->lname;
            q->lname = (q->next)->lname;
            (q->next)->lname = charhold;

            charhold = q->stno;
            q->stno  = (q->next)->stno;
            (q->next)->stno = charhold;

            charhold = q->address;
            q->address = (q->next)->address;
            (q->next)->address = charhold;

            inthold = q->age;
            q->age = (q->next)->age;
            (q->next)->age = inthold;

             for(int w=0 ; w<5 ; ++w)
        			{
               	floathold = q->scores[w];
                  q->scores[w] = (q->next)->scores[w];
                  (q->next)->scores[w] = floathold;
               }

         }
 }

///////////////////////////////////////////////////////////////////////////////

 void Linklist :: Edit(char*s)
 {
 	float sum = 0;
 	if(first)
   {
   	for(node*q = first ; q ; q = q->next)
      {
   		if(strcmpi(s,q->stno)== 0)
         {
         	cout<<"Edit its information :\n"
            	 <<"Its first name : ";
                cin.ignore();
            cin.getline(q->name,20);

            cout<<"Its last name : ";
            cin.getline(q->lname,20);

            cout<<"Address : ";
        		cin.getline(q->address,50);

            cout<<"Age = ";
        		cin>>q->age;
        		cin.ignore();

            cout<<"student number : ";
            cin.getline(q->stno,20);

            cout<<"Enter its scores :\n";
            for(int i=0 ; i<5 ; ++i)
        		{
        			cout<<"a["<<(i+1)<<"] = ";
        			cin>>q->scores[i];
        		   sum +=(q->scores[i]);  	//sum +=(q->scores);
            }

            q->avg = sum/5;
            sort2();
            return;
         }
      }  //end of for

     cout<<"This ID is incorrect !\n";

    } // ent of first if
 }// end of method

///////////////////////////////////////////////////////////////////////////////
 void Linklist :: Remove(char *a)
 {
 	if(first)
   {
   	node *s = NULL;
   	for(node*q = first ; q ; q = q->next)
      {
         if(q == first && strcmpi(a,q->lname)==0)
         {
         	q = first;
         	first = q->next;
            delete q;
            cout<<"\nThis record was found , then removed from list" <<endl;
            return;
         }
      	if(strcmpi(a,q->lname)==0)
         {
         	s->next = q->next;
            delete q;
            cout<<"\nThis record was found , then removed from list" <<endl;
            return;
         } s=q;
      }
      cout<<"This record not found !\n";
   }
 }

///////////////////////////////////////////////////////////////////////////////

void Linklist ::disply()
{
       if(first)
            for(node*q=first ;  q  ; q=q->next)
            {
            	 cout<<"\n\n			Average = "<<q->avg<<endl     //namayeshe avg?
                	  <<"\n\nName : "<<q->name<<endl
                    <<"Last name : "<<q->lname<<endl
                    <<"Age : "<<q->age<<endl
                    <<"stno : "<<q->stno<<endl
                	  <<"Address : "<<q->address<<endl
                    <<"scores :\n";
                for(int i=0 ; i<5 ; ++i)
        			 {
        				cout<<"a["<<(i+1)<<"] = "
        					 <<q->scores[i]<<endl;
        			 }
                cout<<"\n\n---------------------------------------------------------------------------\n";

            }
}
///////////////////////////////////////////////////////////////////////////////

    void Linklist ::search()
    {
    	 if(first)
       {
       cin.ignore();
    	 node*q,*s;
       char ptr[10];        // curr=first;    curr az che noieye?
                             // aslan curr in vasat che sigheiye
       cout<<"Enter its stno:";
       cin.getline(ptr,10);
       for(node *q=first ; q ; q=q->next)
       	if(strcmpi(q->stno , ptr) == 0)
       	 {
         	cout<<"It is found , his/her name is : "
            	 <<q->name<<" "<<q->lname<<endl;
            return;
          }
         cout<<"It is not found\n";
       }
    }
///////////////////////////////////////////////////////////////////////////////

 void Linklist :: MaxMin()
 {
 	if(first != NULL)
   {
 	float max , min;
   node* MAX,* MIN;
   max = min = first->avg;
   for(node *q=first ; q ; q=q->next)
   {
   	if(max <= q->avg)
      {
      	max = q->avg;
         MAX = q;
      }
     	if(min >= q->avg)
      {
      	min= q->avg;
         MIN = q;
      }
   }
   cout<<"\nMaximum average is "<<MAX->avg<<" from "<<MAX->name<<" "<<MAX->lname<<endl;
   cout<<"Minimum average is "<<MIN->avg<<" from "<<MIN->name<<" "<<MIN->lname<<endl;
  }
 }
///////////////////////////////////////////////////////////////////////////////

void main()
{
	int key;
   char *s = new char[20];
   Linklist l; //////////////////
   do
   {
   	cout<<"\n\n---------------------------------------------------------------------------\n"
   		 <<"\nselect one of item :\n"
      	 <<"1-Insert in list\n"
          <<"2-Remove from list\n"
          <<"3-display the list\n"
          <<"4-Search in list\n"
          <<"5-Find maximum and minimum average\n"
          <<"6-Edit student information\n"
          <<"7-Exit\n"
          <<"your choice is : ";
          cin>>key;

      switch(key)
      {
         case(1) :  l.add();   		break;
         case(2) :
                    cout<<"Enter student last name : ";
                    cin.ignore();
                    cin.getline(s,20);
         			  l.Remove(s);    break;
         case(3) :  l.disply();     break;
         case(4) :  l.search();     break;
         case(5) :  l.MaxMin();     break;
         case(6) :
                    cout<<"Enter his/her stno : ";
                    cin.ignore();
                    cin.getline(s,20);
                    l.Edit(s);		break;
         case(7) :                  break;

      }
   }while(key != 7);

}