链表的基本操作-创建、排序、删除、插入

 之前的代码,最近要清理一下电脑的文件,先把代码上传上来,有时间再写。

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* next;
};
typedef struct node link;
typedef struct node* linkp;
linkp create()
{
    linkp head=NULL,p,q;
    p=(linkp)malloc(sizeof(link));
    scanf("%d",&p->data);
    q=p;
    while(p->data)
    {
        if(head==NULL)
            head=p;
        else
            q->next=p;
        q=p;
       p=(linkp)malloc(sizeof(link));
       scanf("%d",&p->data);
    }
    q->next=NULL;
    return head;
}

void p1(linkp h)
{
    linkp p=h;
    while(p)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
void p2(linkp h)
{
    linkp p=h,q;
    
    while(p->next)
    {
	    q=p;p=p->next;
    	printf("%d ",q->data);
       	printf("%d ",p->data);
        
    }
    printf("\n");
}
linkp sort1(linkp h)
{
    linkp h1,h2,p,q,t;
    h1=h;
    h2=h;
    h1=h1->next;
    h2->next=NULL;
    while(h1)
    {
        t=h1;
        for(p=q=h2;q&&(t->data>q->data);p=q,q=q->next);
        h1=h1->next;
        if(q==h2)
        {
            h2=t;
            t->next=q;
        }

        else
        {
            t->next=q;
            p->next=t;
        }

    }
    return h2;
}
void insert(linkp *h,int x)
{
    linkp p,q,s;
    p=q=*h;

    while(q&&(x>q->data))
    {
        p=q;
        q=q->next;
    }
    s=(linkp)malloc(sizeof(link));
    s->data=x;
    if(q==*h)
    {
        s->next=q;
        *h=s;
    }
    else
    {
      s->next=q;
      p->next=s;
    }

}
void del(linkp *h,int x)
{
    linkp p,q;
    p=q=*h;

    while(q&&(x!=q->data))
    {
        p=q;
        q=q->next;
    }
    if(q==NULL)
    {
        printf("不存在此数据!");
        return ;
    }
    if(q==*h)
    {
        *h=(*h)->next;
        free(q);
    }
    else
    {
        p->next=q->next;
        free(q);
    }

}
main()
{
    linkp head;
    head=create();
    p1(head);
    head=sort1(head);
    p1(head);
    insert(&head,222);
    p1(head);
    del(&head,32);
    p1(head);
    p2(head);
}
/*
12 3 54 6 21 7 18 10 32 8 1 0
*/
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二琳爱吃肉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值