
#include<iostream.h>
struct node
{
int data;
node *next;
};
void creat(node *head,int);
void remove(node *head,int);
void print(node *head);
void main()
{
int count; //数字总数
int num;//要删除的数字
node *head=new node;
do
{
cout<<"依次输入个数n(1<=n<=200000),n个元素(用空格分隔),待删除的元素。"<<endl;
cin>>count;
}
while(count>200000||count<1);
creat(head,count);
cin>>num;
remove(head,num);
cout<<"结果如下:"<<endl;
print(head);
}
void creat(node *head,int i)
{
node *temp;
head->next=NULL;
while(i--)
{
temp=new node;
cin>>temp->data;
temp->next=head->next;
head->next=temp;
head=temp;
}
}
void remove(node *head,int num)
{
node *p=head;
head=head->next;
while(head!=NULL)
{
if(head->data==num)
{
p->next=head->next;
head=p->next;
}
else
{
p=p->next;
head=head->next;
}
}
}
void print(node *head)
{
head=head->next;
while(head!=NULL)
{
cout<<head->data<<' ';
head=head->next;
}
cout<<endl;
}
运行结果如下:
不好意思,我不会c++……可以简单点的,有数组转化为链表或者把数组输入链表就可以了
广告 您可能关注的内容 |