刪除~
/*Implement an algorithm to delete a node in the middle of a single linked list, given
only access to that node
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e
*/
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(){this->next = 0;}
Node(int a):data(a),next(0){}
};
class LinkList{
public:
LinkList(){ head = new Node(-1);}
LinkList(int ar[],int len){
head = new Node(-1);
Node*p = FindTail();
for(int i = 0; i<len; i++){
p->next = new Node(ar[i]);
p = p->next;
}
}
Node* FindTail();
Node* head;
void insertion(int);
Node* del(int i);
void print()const{
Node* p = head->next;
while(p)
{
cout<<p->data<<" ";
p = p->next;
}
}
};
Node* LinkList::FindTail(){
Node* p = head;
while( p->next!= 0){
p = p->next;
}
return p;
}
void LinkList::insertion(int k){
Node* p = FindTail();
p->next = new Node(k);
}
Node* LinkList::del(int i){
Node* p = head->next;
Node* k = head;
while(p->data != i){
k = p;
p = p->next;
}
k->next = p->next;
delete(p);
return p->next;
}
int main(){
int ar[]={1,3,5,67,8,9,45,78,9,33,12,3,5,4,67,67};
LinkList ll(ar,16);
ll.del(8);
ll.print();
}