链表的拆分
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{ int n=0,i=0,x=0,y=0;
struct node *head,*head1,*head2,*p;
wtruct node *q,*tail,*tail1,*tail2,*t,*em;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
head1=(struct node*)malloc(sizeof(struct node));
head1->next=NULL;
tail1=head1;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
tail2=head2;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
q=head->next;
while(q!=NULL)
{
t=(struct node*)malloc(sizeof(struct node));
t->next=NULL;
t->data=q->data;
if((t->data)%2==0)
{
tail1->next=t;
tail1=t;
x++;
}
else {
tail2->next=t;
tail2=t;
y++;
}
q=q->next;
}
printf("%d %d\n",x,y);
em=head1;
while(em->next->next!=NULL)
{
printf("%d ",em->next->data);
em=em->next;
}
printf("%d\n",em->next->data);
em=head2;
while(em->next->next!=NULL)
{
printf("%d ",em->next->data);
em=em->next;
}
printf("%d\n",em->next->data);
return 0;
}
链表删除指定字符
#include
#include
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *p,*head,*tail,*t,*em;
int n,key,i=0,k=0;
head = (struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail = head;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
p->next = NULL;
scanf("%d",&p->data);
tail->next = p;
tail=p;
}
scanf("%d",&key);
printf("%d\n",n);
em=head;
while(em->next->next!=NULL)
{
printf("%d ",em->next->data);
em=em->next;
}
printf("%d\n",em->next->data);
em = head;
while(em->next!=NULL)
{
if(em->next->data==key)
{
t= em->next;
em->next=t->next;
free(t);
k++;
}
else
{
em = em->next;
}
}
printf("%d\n",n - k);
em=head;
while(em->next->next!=NULL)
{
printf("%d ",em->next->data);
em=em->next;
}
printf("%d\n",em->next->data);
return 0;
}
链表的顺序和逆序
//顺序
#include
#include
struct xxx
{
int data;
struct xxx *next;
};
int main()
{
struct xxx *head,*now,*tail;
int n;
head=(struct xxx *)malloc(sizeof (struct xxx));
head->next=NULL;
tail = head ;
scanf("%d",&n);
while(n-->0)
{
now=(struct xxx *)malloc(sizeof (struct xxx));
now->next=NULL;
scanf("%d",&now->data);
tail->next=now;
tail=now;
}
while(head->next->next!=NULL)
{
printf("%d ",head->next->data);
head=head->next;
}
printf("%d\n",head->next->data);
return 0;
}
//逆序
#include
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head,*p,*tail;
int n,i;
head=(struct node *)malloc(sizeof (struct node));
head->next=NULL;
tail = head ;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof (struct node));
p->next=NULL;
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
while(head->next->next!=NULL)
{
printf("%d ",head->next->data);
head=head->next;
}
printf("%d\n",head->next->data);
return 0;
}