1 先定义一个链表的结构体 (1)包括数据 int data (2)下一个节点 Node*Next 2 定义一个链表类 #include<iostream> using namespace std; /*单向链表head为头节点,头结点不包含在链表中,所以在计数时不包含头结点*/ struct Node { Node*Next; int data; }; class NodeManager { private: Node*head; Node*tail; public: NodeManager() { this->head=NULL; this->tail=NULL; } ~NodeManager() { while(head) { Node*pTemp; pTemp=head; head=head->Next; delete pTemp; } } void AddNode(int data) //添加数 { if((head==NULL)&&(tail==NULL)) { head=tail=new Node(); Node*pNode=new Node(); head->Next=pNode; tail->Next=pNode; pNode->data=data; tail=pNode; tail->Next=NULL; } else { Node*pNode=new Node(); tail->Next=pNode; pNode->data=data; tail=pNode; tail->Next=NULL; } } void DeleteNode(int dtnum) //删除节点 { Node*node=head;//要删除的节点 Node*pre=head; //删除的节点的前一个节点 while(node!=NULL) { if(head==tail)//空链表 { return; } if(node->data!=dtnum) { pre=node; node=node->Next; } else { if(tail==node) { tail=pre; pre->Next=NULL; } else { pre->Next=node->Next; node=pre->Next;//执行下一步 } } } } void PrintNode() //打印节点 { if(head==NULL) { cout<<"此链表为空!"<<endl; return; } Node*node=head->Next; while(node!=NULL) { cout<<node->data<<endl; node=node->Next; } } int GetCount()//获取节点的数目 { Node*pCurrent=head->Next;//头结点不计 int count=0; while(pCurrent!=NULL) { count++; pCurrent=pCurrent->Next; } return count; } bool RemoveAt(int index) //删除特定位置的数据 { if(index<0||index>getCount()) { cout<<"the wrong position!"<<endl; return false; } Node*pre,*cur; pre=head; cur=head->Next; while(index-1) { pre=pre->Next; cur=cur->Next; --index; } if(tail==cur) { tail=pre; } pre->Next=cur->Next; if(cur!=NULL) { return true; } else { return false; } } bool InsertAt(int index,int value)//在索引前插入新节点 { if(index<=0||index>this->getCount()) { cout<<"the wrong position"<<endl; return false; } Node*current=head; while(index-1) { current=current->Next; --index; } Node*add=new Node(); add->data=value; add->Next=current->Next; current->Next=add; if(current->Next!=NULL) return true; else return false; } void ClearList() //清空链表 { Node*pCurrent1=head; Node*pCurrent2=head; while(pCurrent1->Next) { pCurrent2=pCurrent1->Next; delete pCurrent1; pCurrent1=pCurrent2; } head=NULL; } }; void main() { NodeManager*nm=new NodeManager(); int addnum=0; int dtnum=0; int index=0; int value=0; while(1) { char choice; cin>>choice; switch(choice) { case '0': return; case '1': cout<<"请输入要添加的数据"<<endl; cin>>addnum; nm->addNode(addnum); cout<<"****************"<<endl; break; case '2': cout<<"显示链表中的数据"<<endl; nm->printNode(); cout<<"****************"<<endl; break; case '3': cout<<"请输入要删除的链表中的数据"<<endl; cin>>dtnum; nm->deleteNode(dtnum); cout<<"*****************"<<endl; break; case '4': cout<<"获取链表的长度"<<endl; cout<<nm->getCount()<<endl; cout<<"***************"<<endl; break; case '5': cout<<"按索引删除数据,请输入索引值"<<endl; cin>>index; if(nm->RemoveAt(index)) { cout<<"删除数据成功"<<endl; } else { cout<<"删除数据失败"<<endl; } cout<<"****************"<<endl; break; case '6': cout<<"按索引插入值,请输入索引值及要插入的数据"<<endl; cin>>index; cin>>value; if(nm->InsertAt(index,value)) { cout<<"插入数据成功"<<endl; } else { cout<<"插入数据失败"<<endl; } cout<<"*******************"<<endl; break; case '7': cout<<"删除链表中的所有数据"<<endl; nm->ClearList(); cout<<"*********************"<<endl; break; } } }