求教:注释一下DElETE函数

#include
#include
using namespace std;
struct item
{
char name[12];
char telnum[8];
item *next;
};
class TeleBook
{
private:
item *tbook;
int number;
public:
TeleBook()
{
tbook=NULL;
number=0;
}
void Insert();
void Delete();
void Print();
};
void TeleBook::Insert()
{
item *p;
p=new item;
if(!p)
return;
cout<<"Input name and telenumber:";
cin>>p->name>>p->telnum;
p->next=tbook;
tbook=p;//指向下一个位置;
number++;
}
void TeleBook::Delete()
{
char ch[12];
item *p,*q;
p=tbook;
cout<<"Input the name to be delete:";
cin>>ch;
while(p)
{
if(strcmp(p->name,ch)==0)
{
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
cout<<"Not found the name!"< return;
}
if(p==tbook)
{
tbook=p->next;
}
else
{
q->next=p->next;
}
number--;
cout<name<<"is deleted!"< delete p;
return;
}
void TeleBook::Print()
{
item *p;
p=tbook;
cout while(p)
{
coutname<<':'<telnum< p=p->next;
}
return;
}
int main()
{
int ch;
TeleBook myfriend;
ch=1;
cout<<"1.输入,2.删除,3.打印清单,other-退出\n";
cout<<"Please choose!";
cin>>ch;
switch(ch)
{
case 1:
myfriend.Insert();
break;
case 2:
myfriend.Delete();
break;
case 3:
myfriend.Print();
break;
}
return 0;
}

查看全部
znevegiveup1
唐僧爱吃唐僧肉
2017/07/14 01:44
  • c++
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复