#include
#include
struct node
{
int data;
node* next;
}
node* create(int Array[])
{
node p,*pre,*head;
head = new node;
head->next=NULL;
pre=head;
for(int i=0;i p=new node;
p->data=Array[i];
p->next=NULL;
pre->next=p;
pre=p;
}
return head;
}
int search(node head,int x)
{
int cout=0
node* p=head->next;
while(p!=NULL){
if(p->data==x) count++;
p=p->next;
}
return count;
}
void insert(node* head,int pos,int x)
{
node* p=head;
for(int i=0;i p=p->next;
}
node* q=new node;
q->data=x;
q->next=p->next;
p->next=q;
}
void del(node* head,int x)
{
node* pre =head;
while(p!=NULL){
if(p->data==x){
pre->next=p->next;
delete(p);
p=pre->next;
}else{
pre=p;
p=p->next;
}
}
}
int main()
{
int Array[9]={9,8,7,6,5,4,3,2,1};
node* L=create(Array);
node* q=L;
L=L->next;
printf("输出所建链表的元素:");
while(L!=NULL){
printf("%d",L->data);
L=L->next;
}
printf("\n查找元素为5的个数:%d\n",search(q,5));
del(q,5);
printf("删除元素为5的结点之后的链表;");
L=q->next;
while(L!=NULL){
printf("%d",L->data);
L=L->next;
}
insert(q,8,10);
printf("\n在第8个位置插入元素为10的结点之后的链表;");
L=q->next;
while(L!=NULL){
printf("%d",L->data);
L=L->next;
}
return 0;
}