c++
#include?<iostream>using?namespace?std;
struct?L?{
int?data;
L?*next;
};
int?main()?{
L?*a=new?L;
a->next=new?L;
L?*b=a;
cout<<"創建單鏈表,請輸入單鏈表總結點(N>0)"<<endl;
int?N;
cin>>N;
for(int?i=0;?i<N;?i++)?{
printf("第%d個數字:",i+1);
b->next=new?L;
b=b->next;
cin>>b->data;
}
b->next=NULL;
/**到這裏存儲完畢**/
/*這裏開始查找並且插入*/
cout<<endl<<"輸入尋找的值x"<<endl;
int?x;
cin>>x;
b=a;
while(b->next)?{
b=b->next;
if(b->data==x)
break;
}
cout<<"輸入插入個數m"<<endl;
int?m;
cin>>m;
for(int?i=0;?i<m;?i++)?{
printf("第%d個數字:",i+1);
L?*temp=new?L;
cin>>temp->data;
temp->next=b->next;
b->next=temp;
b=b->next;
}
/**輸出**/
L?*p=a;
cout<<"該鏈表中所含元素:"<<endl;
while?(p->next)?{
p=p->next;
cout<<p->data<<"?";
}
return?0;
}
運行結果:
創建單鏈表,請輸入單鏈表總結點(N>0)
5
第1個數字:100
第2個數字:99
第3個數字:80
第4個數字:45
第5個數字:8
輸入尋找的值x
80
輸入插入個數m
3
第1個數字:1
第2個數字:2
第3個數字:3
該鏈表中所含元素:
100 99 80 1 2 3 45 8
如果滿意的話 請采納 大晚上的雖然閑著沒事,但看在我純手打的份上,麻煩采納壹下