#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node*creatlist(struct node*head,int n)//顺序
{
struct node*tail,*p;
tail=head;
head->next=NULL;
while(n--)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
struct node*creatlist(struct node*head,int n)//逆序
{
struct node*p;
head->next=NULL;
while(n--)
{
p=(struct node)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
return head;
}
void reserve(struct node*head)//逆置
{
struct node*head,*tail,*p;
tail=head->next;
head->next=NULL;//打散
p=tail->next;
while(tail!=NULL)
{
tail->next=head->next;
head->next=tail;
tail=p;
if(tail!=NULL)
{
p=p->next;
}
}
}
void add(struct node*head,struct node*head1)//归并
{
struct node*tail,*tail1,*tail2;
tail=head->next;
tail1=head1->next;
free(head1);
tail2=head//head和head1不存,所以释放head1
while(tail&&tail1)
{
if(tail->data<tail1->data)
{
tail2->next=tail1;
tail2=tail1;
tail1=tail1->next;
}
else
{
tail2->next=tail;
tail2=tail;
tail=tail->next;
}
if(tail)
tail2->next=tail;
else
tail2->next=tail1;
}
}
void kick(struct node*head,struct node*head1,struct node*head2)//针对奇数偶数的拆分
{
struct node*p,*tail,*q,*tail1;
p=head1
q=head2;
tail=head->next;
free(head);
tail1=tail->next;
while(tail!=NULL)
{
tail->next=NULL;//剥离操作
if(tail->data%2==0)
{
p->next=tail;
p=p->next;
}
else
{
q->next=tail;
q=q->next;
}
tail=tail1;
if(tail!=NULL)
{
tail1=tail1->next;
}
}
}
struct node*arrange(struct node*head,int n)//建立有序链表(比较大小)
{
struct node*p,*q;
int m,i;
for(i=0;i<=n-1;i++)
{
p=head->next;
q=p->next;
while(p->next!=NULL)//两个一起跑,保证前面的不变
{
if(p->data>q->data)
{
m=p->data;
p->data=q->data;
q->data=m;
}
p=p->next;
q=q->next;
}
}
return head;
}
void searchthesameelement(struct node*head,int n)//删除相同操作
{
struct node*q,*x,*z;
x=head->next;//head的下一个才存东西
while(x)
{
q=x;//q从x所在的数据往下走和后面的所有数据一一比较
z=q->next;
while(z)
{
if(x->data==z->data)
{
q->next=z->next;//head的下一个被下一个的下一个覆盖
z=q->next;
n--;
}
else
{
q=q->next;
z=z->next;
}
}
x=x->next;
}
}
void print(struct node*head)//输出
{
struct node*tail;
for(tail=head->next; tail!=NULL; tail=tail->next)
{
printf("%d",tail->data);
if(tail!=NULL) printf(" ");
else
printf("\n");
}
}