can anyone help me to fix this problem ?
i can delete the midder and the tail of the node , the only problem is i cannot delete the head node when i have more than one node.
#include<iostream>
#include<string>
using namespace std;
class linklist
{
private:
struct node{
struct student{
string name;
int id;
string prog;
double cgpa;
}st;
node *link;
}*head;
public:
linklist();
void linklist_2();
void insFirstNode (string name, int id, string prog, double cgpa);
void printAllList();
void deleteSearchList(int id);
};
linklist::linklist(){
head = NULL;
}
void linklist::linklist_2()
{
node *p;
if (head == NULL)
return ;
while (head !=NULL)
{
p = head -> link ;
delete head;
head = p;
}
}
void linklist::insFirstNode(string name, int id,string prog, double cgpa)
{
node * newNode,*p;
newNode = new node;
newNode -> st.name = name;
newNode -> st.id = id;
newNode -> st.prog = prog;
newNode -> st.cgpa = cgpa;
newNode -> link = NULL;
if (head == NULL)
head = newNode;
else
{
p = head;
while ((p->link !=NULL))
p = p->link;
p->link = newNode;
}
}
void linklist::deleteSearchList(int id)
{
node * tmp;
tmp = head;
if (tmp == NULL)
return; // no node in list
if (tmp->link == NULL)
{
delete tmp;
head = NULL;
}
else
{
node * p;
p = head;
do{
if(tmp->st.id==id )
break;
p = tmp;
tmp = tmp->link;
}
while (tmp !=NULL);
p->link = tmp->link;
delete tmp;
}
}
void linklist :: printAllList()
{
node * cur;
cur = head;
cout<<" { ";
while (cur !=NULL)
{
cout<<cur ->st.name<<" ";
cout<<cur ->st.id<<" ";
cout<<cur ->st.prog<<" ";
cout<<cur ->st.cgpa<<" ";
cur = cur->link;
if (cur != NULL)
cout<<", ";
}
cout<<" } "<<endl;
}
int main()
{
char ans;
string name1,prog1;
int id1;
double cgpa1;
linklist L;
do{
cout<<"A.) Insert new student"<<endl
<<"B.) Delete student info"<<endl
<<"C.) Print out ALL student info" <<endl
cout<<"Pls choice one option : ";
cin>>ans;
switch (ans)
{
case 'a':
case 'A':
cout<<"student name : "<<endl;
cin>>name1;
cout<<"ID: "<<endl;
cin>>id1;
cout<<"prog : "<<endl;
cin>>prog1;
cout<<"CGPA: "<<endl;
cin>>cgpa1;
L.insFirstNode (name1,id1,prog1,cgpa1);
L.printAllList();
break;
case 'b':
case 'B':
cout <<"pls enter Student ID tat wan delete form node:";
cin >> id1;
L.deleteSearchList(id1);
break;
case 'c':
case 'C':
L.printAllList();
break;
case 'f':
case 'F':
break;
default :
cout<<"enter again"<<endl;
}
}while (toupper(ans) !='F');
cout<<"exit the loop"<<endl;
system("pause");
return 0;
}