链表的应用

顺序建立链表,代码如下:

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *create(int n)
{
    int i;
    struct node *head,*tail,*p;
    head = (struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(i=0;i<n; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next = p;
        tail = p;
    }
    return head;
}
int main()
{
    int n;
    scanf("%d",&n);
    struct node *q,*head;
    head=create(n);
    q=head;
    
    while(q->next!=NULL)
    {
        //为了保证输出的最后一个数后面没有空格
        if(q->next->next==NULL)
        {
            printf("%d",q->next->data);
        }
         else
         {
            printf("%d ",q->next->data);
          }
         q=q->next;
    }
    return 0;
}

运用链表对数据进行从小到大排序,代码如下:

#include <stdio.h>
#include <stdlib.h>
struct student
{
    int num;
    float score;
    struct student *next;
};
typedef struct student STU;
STU *delete_(STU *head,float Score)
{
    STU *p;
    while(head!=NULL)
    {
        if( head->score <=Score)
        {
            head = head->next;
        }
        else
        {
            break;
        }
    }
    p = head;
    if(p!=NULL)
    {
        while( p->next !=NULL)
        {
            if( p->next->score <=Score)
            {
                p->next = p->next->next;
            }
            else
            {
                p = p->next;
            }
        }

    }
    return head;
}
STU *sort_( STU *head )
{
    STU *newhead = NULL,*end_of_new;
    STU *p;
    STU *findhead;
    int firstfind = 1;
    float minscore;
    while( head !=NULL) //旧的链表不为空
    {
        p = head;
        findhead = head;
        minscore = p->score;
        while( p!=NULL)
        {
            if(p->score < minscore) // 找到最小节点
            {
                minscore = p->score;
                findhead = p;
            }
            p = p->next;
        }

        if(findhead == head)  //第1次搜索最小,最为新链表头
        {
            head = findhead->next;
        }
        else  //否则,搜索节点链接到新链表后
        {
            p = head;
            while(p->next!=NULL)
            {
                if(p->next->score == minscore)
                {
                    p->next = p->next->next;
                    break;  //无需继续搜索,避免重复删除
                }
                p = p->next;
            }
        }

        //构建新链表
        if(firstfind == 1) //第一次构建,构建新链表头
        {
            newhead = findhead;
            end_of_new = newhead;
            end_of_new->next = NULL;
            firstfind = 0;
        }
        else
        {
            end_of_new->next = findhead;
            end_of_new = findhead;
            end_of_new->next = NULL;
        }
    }
    return newhead;
}
int main()
{
    STU st1,st2,st3;
    STU *head,*p,*t;
    float sst;
    st1.num = 101;st1.score=92;
    st2.num = 102;st2.score=82;
    st3.num = 103;st3.score=72;
    head = &st1;
    st1.next = &st2;
    st2.next = &st3;
    st3.next = NULL;

    t = (STU *)malloc(sizeof(STU));
    if(t!=NULL)
    {
        t->num = 104;
        t->score = 62;
        t->next = NULL;

    }
    else
    {
        exit(0);
    }
    st3.next = t;
    p = head;
    while(p != NULL)
    {
        printf("\n%d\t%.2f",p->num,p->score);
        p = p->next;

    }

    /


    printf(" \n 排序结果:\n");
    p =  sort_(head);
    if(p == NULL)
    {
        printf("\n空链表");
    }
    else
    {
        while(p != NULL)
        {
            printf("\n%d\t%.2f",p->num,p->score);
            p = p->next;

        }
    }



    return 0;
}

删除比一个特定数值小的链表节点,代码如下:

#include <stdio.h>
#include <stdlib.h>
struct student
{
    int num;
    float score;
    struct student *next;
};
typedef struct student STU;
STU *delete_(STU *head,float Score)
{
    STU *p;
    while(head!=NULL)
    {
        if( head->score <=Score)
        {
            head = head->next;
        }
        else
        {
            break;
        }
    }
    p = head;
    if(p!=NULL)
    {
        while( p->next !=NULL)
        {
            if( p->next->score <=Score)
            {
                p->next = p->next->next;
            }
            else
            {
                p = p->next;
            }
        }

    }
    return head;
}
int main()
{
    STU st1,st2,st3;
    STU *head,*p,*t;
    float sst;
    st1.num = 101;st1.score=92;
    st2.num = 102;st2.score=91;
    st3.num = 103;st3.score=82;
    head = &st1;
    st1.next = &st2;
    st2.next = &st3;
    st3.next = NULL;

    t = (STU *)malloc(sizeof(STU));
    if(t!=NULL)
    {
        t->num = 104;
        t->score = 72;
        t->next = NULL;

    }
    else
    {
        exit(0);
    }
    st3.next = t;
    p = head;
    while(p != NULL)
    {
        printf("\n%d\t%.2f",p->num,p->score);
        p = p->next;

    }

    /
    printf(" \n 请输入限定值:\n");
    scanf("%f",&sst);

    printf(" \n delete:\n");
    p =  delete_(head,sst);
    if(p == NULL)
    {
        printf("\n空链表");
    }
    else
    {
        while(p != NULL)
        {
            printf("\n%d\t%.2f",p->num,p->score);
            p = p->next;

        }
    }



    return 0;
}

在尾部插入一个数值,代码如下:

#include <stdio.h>
#include <stdlib.h>

struct student
{
    int num;
    float score;
    struct student *next;
};
typedef struct student STU;
int main()
{
    STU st1,st2,st3;
    STU *head,*p,*temp;


    st1.num = 1001;
    st1.score = 98;

    st2.num = 1002;
    st2.score = 100;

    st3.num = 1003;
    st3.score = 87.6;
    head = &st1;
    st1.next = &st2;
    st2.next = &st3;
    st3.next = NULL;


    temp =( STU *)malloc(sizeof(STU));
    if( temp != NULL)
    {
        temp->num = 1004;
        temp->score = 86.9;
        temp->next = NULL;
        st3.next = temp;
    }


    p = head;
    while(p!=NULL)
    {
         printf("%d\t%.2f\n",p->num,p->score);
         p = p->next;
    }


   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值