C++ link-listed question

Page 1 of 1

1 Replies - 230 Views - Last Post: 23 January 2013 - 08:17 AM Rate Topic: ***** 1 Votes

#1 teohenrui  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 13

C++ link-listed question

Posted 23 January 2013 - 12:56 AM

Hi everybody. I am a C++ user who is new to link-listed. Now I am having a problem with one of my program. The program is about design a student recording system. The problem that I am facing is on the int main part. I am not sure how to link those void print or void delete to the main function, which means I don't know how to do the delete student part and the print part. So I hope someone can help me on this. Below is the code that I did by far.

#include<iostream>
#include<string>

using namespace std;

class studentDet
{   private:
        struct node
        {
            string name;
			double cgpa;
			int id;
			string prog;
			int progcode;
			node *link;
			studentDet* next;
        }*head;          
     public:
		 studentDet();
         void InsNew(string name, int id, string prog, int progcode,double cgpa);
		 void UpdateS(string name, int id, string prog, int progcode,double cgpa);
         void PrintList();
         bool SearchId(int id);
         void DeleteNode();         
         ~studentDet();
};
///////////////////////////////////////////////////////
studentDet::studentDet()
{
     head=NULL;
}
studentDet::~studentDet()
{
   node *q;
   if( head == NULL )
        return;

   while( head != NULL )
   {
      q = head->link;
      delete head;
      head = q;
   }
}
//////////////////////////////////////////////////////

/* Inserting a New Student Node (always last) */
void studentDet::InsNew(string name, int id, string prog, int progcode,double cgpa)

{	node *newNode, *prev;
	newNode = new node;
	newNode->name = name;
	newNode->id = id;
	newNode->prog = prog;
	newNode->progcode = progcode;
	newNode->cgpa;
	newNode->link = NULL;	
    if (head == NULL)
       head = newNode;
    else
    { 	prev = head;
    	
        while ((prev->link) != NULL)
    		prev = prev->link;
    		
        prev->link = newNode;
     }
     
}


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

/*Update anywhere Reinsert
void studentDet::UpdateS(string name, int id, double cgpa, char prog, int progcode);
{ node *newNode, *prev;
	newNode = new node;
	newNode->name = name;
	newNode->id = id;
	newNode->prog = prog;
	newNode->progcode = progcode;
	newNode->cgpa;
	newNode->link = NULL;


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


	*/

void studentDet::PrintList()
{    node *current;
    current = head;    
   
	while (current != NULL)
	{
		cout<<"\n\n------------Student Info------------"<<endl;
		cout<<"Name: \tID: \tProgramme: \tProgramme Code: \tCGPA: "<<endl;
		cout<<current->name;
		cout<<"\t"<<current->id;
		cout<<"\t\t"<<current->prog;
		cout<<"\t"<<current->progcode;
		cout<<"\t"<<current->cgpa;
		current=current->link;
	}
}
/////////////////////////////////////////
bool studentDet::SearchId(int id)
{
    node *current;
    current = head;
   	bool found;
    found = false;
    
    while ((current != NULL) && (!found))
    { 
    		if (current->id == id)
      		  found = true;
    		else
 		      current = current->link;
    }
        return found;
}
/////////////////////////////////////////////////  

/* Deleting the Last Node */
void studentDet::DeleteNode()
{   node *PreviousNode, *CurrentNode;
    if (head != NULL)
    {   if (head->link == NULL)
        {
        	delete head;
        	head = NULL;
        }
        else
        {       PreviousNode = head;
               CurrentNode = head->link;            
               while ((CurrentNode->link) != NULL) 
               {   	PreviousNode = CurrentNode;
            	    CurrentNode = CurrentNode->link;
               }            			
               PreviousNode->link = NULL;
               delete CurrentNode;
        }
     }
}
///////////////////////////////////////////////

int main()
{   
	studentDet list;
	string name;
	int id;
	char prog[20];
	int progcode;
	double cgpa;
    char selected;
	int cont;

	do {
	
		cout<<"A. Add a student information."<<endl;
		cout<<"B. Update a student information."<<endl;
		cout<<"C. Delete a student information."<<endl;
		cout<<"D. Print."<<endl;
		cout<<"E. Search."<<endl;
		cin>>selected;

		switch (selected)
		{
		case 'a':
		case 'A':
		cout<<"Please Fill In The Following Blanks"<<endl;
		cout<<"Enter Name: ";
			cin>>name;
			cout<<"\nEnter ID: ";
			cin>>id;
			cout<<"\nEnter Programme: ";
			cin>>prog;
			cout<<"\nEnter Programme Code: ";
			cin>>progcode;
			cout<<"\nEnter CGPA: ";
			cin>>cgpa;
			list.InsNew(name, id, prog, progcode, cgpa);
			
			break;

		case 'b':
		case 'B':

			
		break;

		case 'c':
		case 'C':
		
		break;

		case 'd':
		case 'D':
			void PrintList();
		
		break;

		case 'e':
		case 'f':
			break;}

	} 


	while ( cont=1);
			

    system("pause");

    getchar();        
    getchar();
    return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ link-listed question

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: C++ link-listed question

Posted 23 January 2013 - 08:17 AM

Duplicate topic closed.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1