在单链表中删除指定的结点。这里单链表是用尾插法建立的,因为尾插法输出的顺序与输入的顺序是相同的。
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}no;
int main()
{
no *head,*tail,*p,*r,*q;
head=new no;
head->next=NULL;
tail=head;
int n,k;
printf("一共要输入的数: ");
scanf("%d\n",&n);
//尾插法建立单链表
for(int i=0;i<n;i++)
{
cin>>k;
p=new no;
p->data=k;
p->next=NULL;
tail->next=p;
tail=p;
}
//接下来是删除操作
int m;
printf("输入要删除的数: ");
scanf("%d",&m);
p=head;//让p指针从头结点开始遍历,要注意的是,头结点是没有数值的哦!
while(p->data!=m&&p->next!=NULL)//循环查找要删除的结点
{
r=p;
p=p->next;//把p的下一个结点给p,所以p就不是原来的p了,原来的p变成了r
if(p-