不敢死队

本文介绍了一个使用C语言实现的链表结构及其操作方法,包括创建链表和从链表中删除特定节点的过程。此外,还展示了如何通过具体实例演示算法流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2056&cid=1156

View Code
#include<stdio.h>  
#include<stdlib.h>  
struct node  
{  
    int num ;  
    struct node *next ;  
};  
struct node *creat(int n)  
{  
    int i ;  
    struct node *head, *p, *tail ;  
    p = (struct node*)malloc(sizeof(struct node)) ;  
    p->num = 1 ;  
    p->next = NULL ;  
    head = p ;  
    tail = p ;  
    for(i=2; i<=n; i++)  
    {  
        p = (struct node*)malloc(sizeof(struct node)) ;  
        p->num = i ;  
        tail->next = p ;  
        tail = p ;  
        tail->next = NULL ;  
    }  
    tail->next = head ;  
    return head ;  
}  
void del(struct node *head, int n)  
{  
    int count = 0, i = 0 ;  
    struct node *p, *q ;  
    q = head ;  
    while(q->next!=head)  
    q = q->next ;  
    while(count<n-1)  
    {  
        p = q->next ;  
        i++ ;  
        if(i%5==0)  
        {  
            q->next = p->next ;  
            if(p->num==1)  
            break ;  
            free(p) ;  
            count++ ;  
        }  
        else  
        q = p ;  
    }  
    if(n==5)  
    printf("1\n") ;  
    else  
    printf("%d\n", count+1) ;  
}  
int main()  
{  
    int n ;  
    struct node *head ;  
    while(scanf("%d", &n), n!=0)  
    {  
        head = creat(n) ;  
        del(head, n) ;  
    }  
    return 0 ;  
}  
  

 

转载于:https://www.cnblogs.com/yelan/archive/2013/04/19/3031536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值