- 论坛徽章:
- 0
|
#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;
} |
|