从无头单链表中删除节点

#include<iostream>
using namespace std;

struct Node{
      int data;
   Node* next;
};

void deleteNode(Node* p)
{
 p->data=p->next->data;   //将p后的节点的数值赋给p的data域
 p->next=p->next->next;   //将p后的节点删除,即完成了删除节点p的效果。
}
void display(Node* r)
{
     while(r!=NULL)
  {
      cout<<r->data<<" ";
   r=r->next;
  }
  cout<<endl;
}
int main()
{
    Node* t;
    Node* p=new Node();
 p->data=1;
 p->next=NULL;
 t=p;
 Node* q=new Node();
 q->data=2;
 q->next=NULL;
 p->next=q;
 Node* r=new Node();
 r->data=3;
 r->next=NULL;
 q->next=r;
 Node* s=new Node();
 s->data=4;
 s->next=NULL;
 r->next=s;
 Node* h=new Node();
 h->data=5;
 h->next=NULL;
 s->next=h;
 display(t);
 deleteNode(r);
 display(t);
    system("pause");
 return 0;
}

  

原文地址:https://www.cnblogs.com/yanglf/p/2773800.html