View Code of Problem 80

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

typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode, *LinkList;

LinkList CreateList(LinkList L, int n)
{
    LNode *r, *s;
    int i=1;
    L = (LinkList)malloc(sizeof(LNode));
    L->data = 0;
    r = L;
    while(i<=n)
    {
        s = (LNode *)malloc(sizeof(LNode));
        s->data = i;
        r->next = s;
        r = s;
        i++;
    }
    r->next = L;
    return L;
}

void RunTime(LinkList L, int n)
{
    LNode *p = L;
    LNode *q = L->next;
    int i = 1;
    while(n>1)
    {
        if(q->data == 0)
        {
            p = q;
            q = q->next;
        }
        else
        {
            if(i%3 == 0)
            {
                p->next = q->next;
                free(q);
                q = p->next;
                i = 1;
                n = n-1;
            }
            else
            {
                p = q;
                q = q->next;
                i++;
            }
        }
    }
    printf("%d\n", L->next->data);
    free(L->next);
    free(L);
}

void main()
{
    LinkList L;
    int n;
    scanf("%d", &n);
    while(n!=0)
    {
        L = CreateList(L, n);
        RunTime(L, n);

        scanf("%d", &n);
    }
}

Double click to view unformatted code.


Back to problem 80