老師布置的作業
是根據提示用戶輸入姓名和聯系電話來找到鏈表中位置進行刪除嗎
void?deleteperson(List?*pList) { Node?node; Person?person; cout<<"你要刪除的聯系人姓名和聯系電話:"; cin>>person.name>>person.phone; node.data?=?person; int?i?=?pList->LocateElem(&node); pList->ListDelete(i,&node);? }
是根據提示用戶輸入姓名和聯系電話來找到鏈表中位置進行刪除嗎
void?deleteperson(List?*pList) { Node?node; Person?person; cout<<"你要刪除的聯系人姓名和聯系電話:"; cin>>person.name>>person.phone; node.data?=?person; int?i?=?pList->LocateElem(&node); pList->ListDelete(i,&node);? }
2016-08-28
舉報
2016-08-29
先找到聯系人位置在刪除,自己寫的,僅供參考
void deletePerson(List<Node> *pl,Node *temp)
{
? ?Node node;
? ?cout << "請輸入姓名:" << endl;
? ?cin >> node.data.name;
? ?cout << "請輸入電話:" << endl;
? ?cin >> node.data.phone;
? ?int locate = pl->LocateElem(&node);//先查找聯系人的位置
? ?if(locate == -1)
? ?{
? ? ? ?cout << "沒找到此聯系人" << endl;
? ? ? ?return;
? ?}
? ?pl->ListDelete(locate,temp);//刪除聯系人
? ?cout << "成功刪除聯系人" << endl;
}
2019-07-28
void deleteperson(List*pList)
{
Node node;
cout << "刪除全部請按 <1> -- 刪除指定姓名請按 <2>--刪除指定號碼請按<3>" <<endl;
int num= 0;
int flag = 0;
cin >> num;
switch (num)
{
case 1:
pList->ClearList(); break;
case 2:
cout << "請輸入姓名:";
cin >> node.data.name;
case 3:
cout << "請輸入電話:";
cin >> node.data.phone;
flag = pList->LocateElem(&node);
if (flag == -1)
{
cout << "通訊錄無此聯系人,請重新輸入!" << endl;
break;
}
else
{
pList->ListDelete(flag, &node);
break;
}
default:
break;
}
}
另外在LocateElem中改變一下
int List::LocateElem(Node *pNode)
{
?Node*currentNode=m_pList;
?int count=0;
?while(currentNode->next!=NULL)
{
?currentNode=currentNode->next;
?if(currentNode->data.name==pNode->data.name||currentNode->data.phone==pNode->data.phone)
?{return count;}
?count++;
}
?return -1;
}
這樣就可以實現不論你輸入電話或者號碼任意一個條件都可以刪除對應的全部信息
2018-01-05
void deletePerson(List *plist)//刪除聯系人 方法2
{
cout<<"是否刪除所有聯系人輸入1"<<endl;
int ?order;
cin>>order;
if (order==1)
{
plist->ClearList();
cout<<"所有聯系人已經刪除"<<endl;
}?
else
{
int sequenceNumber;
cout<<"請輸入序號 : ";
cin>>sequenceNumber;?
sequenceNumber<1||sequenceNumber>plist->ListLength()?cout<<"此聯系人不存在"<<endl:cout<<"此聯系人已經刪除"<<endl;
Node node;
plist->ListDelete(sequenceNumber-1,&node);
}
}
void List::ListTraverse()
{
Node *currentNode=m_pList;
for(int k=0;k<m_iLength;k++)
{
currentNode=currentNode->next;
cout<<"sequence Number ?"<<k+1<<" : ";
currentNode->printNode();
}
}
2017-07-07
修改一下LocateElem函數:
int List::LocateElem(Node *pNode)
? {
? ??Node *currentNode=m_iPlist;
? ??int count=0;
??while(currentNode->next!=NULL)
??{
???currentNode=currentNode->next;
???if(currentNode->data.m_sName==pNode->data.m_sName)
???{
????return count;
?? }
?? count++;
? }
? return -1;
???
? }
通過名字刪除,個人想法,歡迎各位學友交流指正。
2017-02-21
先遍歷,我在遍歷的時候,修改了一下,能夠看到每個聯系人的序號
只需要輸入對應的序號就可以刪除聯系人,在刪除操作上簡化了
同時也衍生出搜索聯系人的問題,對cin>>的運算符進行了重載,靠聯系人的姓名來搜索,不過暫時還沒解決只輸入姓名,年齡,城市,手機號其中的一項來進行搜索的問題,望大神指點迷津
2016-12-06
void deletePerson(List*pList)
{
? ?char str;
? ?Node node;
? ?Person person;
? ?cout<<"請輸入姓名: ";
? ?cin>>person.name;
? ?cout<<"請輸入電話: ";
? ?cin>>person.phone;
? ?node.data=person;
? ?node.data=person;
? ?int count = pList->LocateElem(&node);
? ?if(count==-1)
? ?{
? ? ? ?cout<<"沒有該聯系人"<<endl;
? ? ? ?return;
? ?}
? ?else
? ?{
? ? ? ?cout<<"確定要刪除此人?"<<endl;
? ? ? ?cin>>str;
? ? ? ?switch (str) {
? ? ? ?case 'y':
? ? ? ? ? ?pList->ListDelete(count,&node);
? ? ? ? ? ?cout<<"聯系人已刪除";
? ? ? ? ? ?cout<<"------------"<<endl;
? ? ? ? ? ?pList->ListTraverse();
? ? ? ? ? ?break;
? ? ? ?case 'n':
? ? ? ? ? ?return pList->ListTraverse();
? ? ? ? ? ?break;
? ? ? ?default:
? ? ? ? ? ?break;
? ? ? ?}
? ?}
}
2016-12-06
void deletePerson(List*pList)
{
? ?char str;
? ?Node node;
? ?Person person;
? ?cout<<"請輸入姓名: ";
? ?cin>>person.name;
? ?cout<<"請輸入電話: ";
? ?cin>>person.phone;
? ?node.data=person;
? ?node.data=person;
? ?int count = pList->LocateElem(&node);
? ?if(count==-1)
? ?{
? ? ? ?cout<<"沒有該聯系人"<<endl;
? ? ? ?return;
? ?}
? ?else
? ?{
? ? ? ?cout<<"確定要刪除此人?"<<endl;
? ? ? ?cin>>str;
? ? ? ?switch (str) {
? ? ? ?case 'y':
? ? ? ? ? ?pList->ListDelete(locate,&node);
? ? ? ? ? ?cout<<"聯系人已刪除";
? ? ? ? ? ?cout<<"------------"<<endl;
? ? ? ? ? ?pList->ListTraverse();
? ? ? ? ? ?break;
? ? ? ?default:
? ? ? ? ? ?break;
? ? ? ?}
? ?}
}
2016-09-28
void removePeron(List *plist)
{
Node node;
cout << "刪除全部請按 <1> -- 刪除指定姓名請按 <2>" <<endl;
int num= 0;
int flag = 0;
cin >> num;
switch (num)
{
case 1:
plist->ClearList(); break;
case 2:
cout << "請輸入姓名:";
cin >> node.data.name;
cout << "請輸入電話:";
cin >> node.data.phone;
flag = plist->LocateElem(&node);
if (flag == -1)
{
cout << "通訊錄無此聯系人,請重新輸入!" << endl;
break;
}
else
{
plist->ListDelete(flag, &node);
break;
}
default:
break;
}
}
僅供參考 刪除必須要同時輸入 姓名和 電話 這樣顯得有點多此一舉啊。不知道哪位大神可以改進。