数据结构中链表元素的删除

来源:互联网 发布:手机淘宝下载安装 编辑:程序博客网 时间:2024/06/05 15:48
#include <iostream>#include <string>struct student{char name[20];int age;student* next;};int main(){using namespace std;char deleteName[20];cout<<"请输入要删除的结点姓名(Kaka,Deco或Terry):"<<endl;cin>>deleteName;student c={"Terry",30,NULL};student b={"Deco",27,&c};student a={"Kaka",23,&b};student* head=&a;student* pointer=head;student* before=NULL;bool isFind=false;while(pointer){if(strcmp(deleteName,(*pointer).name)==0)//寻找删除位置{isFind=true;//已经找到break;}before=pointer;//记录删除位置前一结点的地址pointer=(*pointer).next;}if(isFind){if(before==NULL)  head=(*head).next;//删除链表首记录else{(*before).next=(*pointer).next;//在链表中间或末尾删除}}elsecout<<"未找到你输入的项"<<endl;//未找到输入数据student* coutPointer=head;//输出链表数据cout<<"Head->";while(coutPointer)//while(coutPointer != NULL){cout<<(*coutPointer).name<<"->"<<(*coutPointer).age<<"->";coutPointer=(*coutPointer).next;}cout<<"End"<<endl;return 0;}

0 0
原创粉丝点击