链表的简单操作

#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");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值