0
点赞
收藏
分享

微信扫一扫

C语言|链表-不敢死队问题

janedaring 2022-03-18 阅读 58

 

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n,m,i,k=0,j;
    struct node *head,*tail,*p,*q;
    while(~scanf("%d",&m)&&m!=0)
    {
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    head->data=1;
    tail=head;
    for(i=2;i<=m;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->data=i;
        tail->next=p;
        tail=p;
    }
    tail->next=head;
    p=head;
    k=0;
    j=0;
    q=tail->next;
    while(p->next!=NULL)
    {
        j++;
        if(j==4)
        {
            q=p->next;
            if(q->data==1)
                break;
            else {
                p->next=p->next->next;
                free(q);
                j=0;
                k++;
            }
        }
        p=p->next;
    }
        printf("%d\n",k+1);
}
}
举报

相关推荐

0 条评论