can anyone help me to slove the problem ??
i wan to delete a node either form the head , midder or last , then print out all the 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 * cur,*p;
p = head;
cur = head;
bool found;
found = false;
while ((cur !=NULL) && (!found))
{
if (cur ->st.id ==id )
found = true;
else
p = cur;
cur = cur->link;
}
p->link = cur ->link;
delete p;
cout<<"deleted"<<endl;
}
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
<<"D.) Print out ALL student info" <<endl
<<"E.) View student cgpa 3.0 above "<<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 'd':
case 'D':
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;
}