免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1039 | 回复: 0
打印 上一主题 下一主题

只能编程了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-08 18:11 |只看该作者 |倒序浏览
#include <stdio.h>

typedef struct node
{
        int number;
        struct node* prenode;
        struct node* nextnode;
} Node;

P
int main( int argc , char** argv)
{
        int i;
        Node *first, *cursor, *newnode;

        if( argc < 3 )
        {
                printf("usage: argv[0] total interval\n");
                return 1;
        }

        /*** init list *****/
        if( atoi(argv[1]) ==0 || atoi(argv[2]) ==0)
        {
                return 1;
        }

        first = (Node*) malloc( sizeof( Node ));
        first->number=1;
        first->prenode = first;
        first->nextnode = first;

        cursor = first;
        for( int i=1; i < atoi(argv[1]) ; i++ )
        {
                newnode = (Node*) malloc( sizeof(Node) );
                newnode->number=i+1;
                newnode->prenode = cursor;
                newnode->nextnode = first;

                cursor->nextnode = newnode;
                first->prenode = newnode;

                cursor = newnode;
        }


        cursor = first;
        while( cursor->prenode != cursor )
        {
                newnode = cursor;
                for( i=0 ; i< atoi(argv[2]) ; i++)
                {
                        newnode = newnode->nextnode;
                }

                cursor->prenode->nextnode = cursor->nextnode;
                cursor->nextnode->prenode = cursor->prenode;

                printf( "%d is deleted\n",cursor->number);
                free( cursor);
                cursor = newnode;
        }

        printf("%d is the last number\n",cursor->number);

        return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP