程序代码:
#include <stdio.h>
#include <stdlib.h>
#define N 7
#define M 20
int password[N] = {3, 1, 7, 2, 4, 8, 4};
typedef struct node_struct
{
int index;
int password;
struct node_struct *next;
}node_t;
node_t *create_list(node_t **_head)
{
int i = 0;
node_t *tmp;
if( NULL==((*_head)=(node_t *)malloc(sizeof(node_t))) ) return NULL;
tmp = *_head;
while (1)
{
tmp->index = i+1;
tmp->password = password[i++];
if (N==i)
{
tmp->next = *_head;
break;
}
if( NULL==((tmp->next)=(node_t *)malloc(sizeof(node_t))) ) return NULL;
tmp = tmp->next;
}
return *_head;
}
void print_list(node_t *head)
{
int i = 0;
printf("your list looks like as follows: \n");
while (i<N)
{
printf("index = %d, password = %d\n",head->index, head->password);
head = head->next;
i++;
}
}
void print_solution(node_t *head, int m)
{
int i = 0;
node_t *prev = head;
while (prev->next!=head)
{
prev = prev->next;
}
printf("the order is: \n");
while (i<N)
{
while (m!=1)
{
prev = head;
head = head->next;
m--;
}
printf("get out: %d\n",head->index);
m = head->password;
prev->next = head->next;
free(head);
head = prev->next;
i++;
}
}
void main()
{
node_t *head;
head = create_list(&head);
print_list(head);
print_solution(head,M);
}
[local]1[/local]
希望不是作业。