Linux下学C语言——第十九节 数据结构|链表的应用

1.数学合并同类项算法:

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

typedef struct node_st
{
    int coef;
    int exp;
    struct node_st *next;
}node;

node *poly_creat(int a[][2],int n)
{
    node *me;
    node *new,*cur;
    int i;

    me = malloc(sizeof(*me));
    if(me == NULL)
        return NULL;

    cur = me;

    for(i=0;i<n;i++)
    {
        new = malloc(sizeof(*new));
        if(new == NULL)
            return NULL;
        new ->coef = a[i][0];
        new ->exp = a[i][1];
        new ->next = NULL;

        cur->next = new;
        cur = new;

    }
    return me;

}
void poly_show(node *me)
{
    node *cur;

    for(cur = me ->next;cur != NULL;cur = cur ->next)
    {
        printf("(%d,%d)",cur->coef,cur -> exp);
    }
    printf("\n");
}
void poly_union(node * p1,node *p2)

{
    node *p,*q;
    node *r;
    p = p1 ->next;
    q = p2 ->next;
    r = p1;
    
    while(p&&q)
    {
        if(p->exp < q->exp)
        {
            r -> next = p;
            r = p;
            p = p->next;
        }
        else if(p->exp > q ->exp)
        {
            r->next = q;
            r =q;
            q = q->next;
        }
        else//p->exp == q->exp
        {
            p->coef += q->coef;
            if(p->coef)
            {
                r->next =p;
                r = p;
            }
            p = p->next;
            q = q->next;
        }
    }



}


int main()
{
    int a[][2] = {{5,0},{2,1},{8,8},{3,16}};
    int b[][2] = {{6,1},{16,6},{-8,8}};
    node *p1, *p2;

    p1 = poly_creat(a,4);
    if(p1 == NULL)
        exit(1);
    p2 = poly_creat(b,3);
    if(p2 ==NULL)
        exit(1);

        

    poly_show(p1);
    poly_show(p2);

    poly_union(p1,p2);
    poly_show(p1);
    exit(0);
}

执行:

[tom@CentOS7 polynomial]$ ./poly 
(5,0)(2,1)(8,8)(3,16)
(6,1)(16,6)(-8,8)
(5,0)(8,1)(16,6)(-8,8)

2.报数出局小游戏

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

#define JOSE_NR 8

typedef struct node_st
{
        int data;
        struct node_st *next;
}node;

node *jose_creat(int n)
{
    node *me;
    node *new,*cur;
    int i = 1;

    me = malloc (sizeof(*me));
    if(me == NULL)
        return NULL;
    me ->data = i;
    me ->next = me;
    i++;
    
    cur = me;

    for(;i<= n;i++)
    {
        
        new=malloc (sizeof(*new));
        if(new == NULL)
        {
            return NULL;
        }
            printf("%d\n",i);
        new ->data = i;
        new -> next = me;

        cur -> next = new;
        cur = new;
    }
    return me;
}

void jose_show(node *me)
{
    node *list;
    for(list = me;list -> next != me;list = list->next)
    {

        printf("%d ",list ->data);
    }
    printf("%d\n",list->data);
}
void jose_kill(node **me,int n)
{
    node *cur =*me,*node;
    int i =1;
    
    while (cur != cur ->next)
    {
        while(i<n)
        {
            node = cur;
            cur = cur ->next ;
            i++;
        }
        printf("%d",cur ->data);
        node->next = cur ->next;
        free(cur);

        cur = node ->next;
        i= 1;
    }
    *me = cur;
    printf("\n");
    
}
int main()
{   
    node *list;
    list = jose_creat(JOSE_NR);
    if(list == NULL)
        exit(1);
    jose_show(list);
    jose_kill(&list,3);
    jose_show(list);
    exit(0);
}

执行:隔三出局一个人,最后剩7号

1 2 3 4 5 6 7 8

7

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值