S
Sakula
#define MAX 20
/* includes */
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct Student
{
int number;
int age;
int male;
char name[20];
Student * next;
};
Student * Create();
void ShowList(Student * g_head);
Student * Delete(Student * g_head, int age);
Student * g_head;
Student * Create()
{
Student * pS;
Student * pEnd;
pS=new Student;
cout<<"please input number,age ,male and name"<<endl;
cin>>pS->number>>pS->age>>pS->male;
gets(pS->name);
g_head=NULL;
pEnd=pS;
while(pS->number!=0)
{
if(g_head==NULL)
g_head = pS;
else
pEnd->next=pS;
pEnd=pS;
pS=new Student;
cout<<"please input number,age , male and name"<<endl;
cin>>pS->number>>pS->age>>pS->male;
gets(pS->name);
}
pEnd->next=NULL;
delete pS;
return (g_head);
}
void ShowList(Student * g_head)
{
cout<<"now the items of list are \n";
while(g_head)
{
cout<<g_head->number<<" "<<g_head->age<<"
"<<g_head->male<<"
"<<g_head->name<<endl;
g_head=g_head->next;
}
}
Student * Delete(Student * g_head, int age)
{
Student *p=NULL;
p=new Student;
if(!g_head)
{
cout<<"\nList null!\n";
return NULL;
}
if(g_head->age==age)
{
p=g_head;
g_head=g_head->next;
delete p;
cout<<age<<"the g_head of list has been deleted\n";
return g_head;
}
for(Student * pGuard=g_head; pGuard->next;pGuard=pGuard->next)
{
if(pGuard->next->age==age)
{
p=pGuard->next;
pGuard->next=p->next;
delete p;
cout<<age<<"has been deleted\n";
return g_head;
}
}
cout<<age<<"is not found! \n";
}
void main()
{
int age=0;
g_head=Create();
cout<<"now the list are \n";
ShowList(g_head);
cout<<"please input the age you want to delete : \n";
cin>>age;
Delete(g_head,age);
ShowList(g_head);
}
when i delete the head of the linked list,it will be crupt!
I debug this program and find the gloable variable g_head doesn't be
changed by Delete( ). I don't know why this happen.
/* includes */
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct Student
{
int number;
int age;
int male;
char name[20];
Student * next;
};
Student * Create();
void ShowList(Student * g_head);
Student * Delete(Student * g_head, int age);
Student * g_head;
Student * Create()
{
Student * pS;
Student * pEnd;
pS=new Student;
cout<<"please input number,age ,male and name"<<endl;
cin>>pS->number>>pS->age>>pS->male;
gets(pS->name);
g_head=NULL;
pEnd=pS;
while(pS->number!=0)
{
if(g_head==NULL)
g_head = pS;
else
pEnd->next=pS;
pEnd=pS;
pS=new Student;
cout<<"please input number,age , male and name"<<endl;
cin>>pS->number>>pS->age>>pS->male;
gets(pS->name);
}
pEnd->next=NULL;
delete pS;
return (g_head);
}
void ShowList(Student * g_head)
{
cout<<"now the items of list are \n";
while(g_head)
{
cout<<g_head->number<<" "<<g_head->age<<"
"<<g_head->male<<"
"<<g_head->name<<endl;
g_head=g_head->next;
}
}
Student * Delete(Student * g_head, int age)
{
Student *p=NULL;
p=new Student;
if(!g_head)
{
cout<<"\nList null!\n";
return NULL;
}
if(g_head->age==age)
{
p=g_head;
g_head=g_head->next;
delete p;
cout<<age<<"the g_head of list has been deleted\n";
return g_head;
}
for(Student * pGuard=g_head; pGuard->next;pGuard=pGuard->next)
{
if(pGuard->next->age==age)
{
p=pGuard->next;
pGuard->next=p->next;
delete p;
cout<<age<<"has been deleted\n";
return g_head;
}
}
cout<<age<<"is not found! \n";
}
void main()
{
int age=0;
g_head=Create();
cout<<"now the list are \n";
ShowList(g_head);
cout<<"please input the age you want to delete : \n";
cin>>age;
Delete(g_head,age);
ShowList(g_head);
}
when i delete the head of the linked list,it will be crupt!
I debug this program and find the gloable variable g_head doesn't be
changed by Delete( ). I don't know why this happen.