|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void InitList(struct Node **, struct Node **, int);
void CreateList(struct Node *, struct Node *, struct Node **, struct Node **, int);
void ListTraverse(struct Node *, struct Node *);
void main()
{
struct Node *head, *rear, *r, *s;
int n;
printf("请输入黑桃牌的张数: ");
scanf("%d", &n);
putchar('\n');
InitList(&head, &rear, n);
CreateList(head, rear, &r, &s, n);
ListTraverse(r, s);
}
void InitList(struct Node **head, struct Node **rear, int n)
{
struct Node *p, *q;
int i, j;
*head = (struct Node *)malloc(sizeof(struct Node));
p = *head;
for(i=1;i<=n;i++)
{
j = 1;
q = (struct Node *)malloc(sizeof(struct Node));
p->next = q;
p = q;
q->data = 0;
}
p->next = (*head)->next;
*rear = p;
}
void CreateList(struct Node *head, struct Node *rear, struct Node **r, struct Node **s, int n)
{
struct Node *p=head->next, *q=rear, *t;
int i=1, j;
InitList(r, s, n);
t = (*r)->next;
while(p!=p->next)
{
j= 1;
while(j%i)
{
p = p->next;
while(t->next->data!=0)
{
t = t->next;
}
t = t->next;
q = q->next;
j++;
}
printf("黑桃%2d ", i);
t->data = p->data = i++;
q->next = p->next;
free(p);
p = q->next;
while(t->next->data!=0)
{
t = t->next;
}
t = t->next;
}
t->data = i;
printf("黑桃%2d \n\n", i);
}
void ListTraverse(struct Node *r, struct Node *s)
{
struct Node *p = r;
printf("**********扑克牌应该按如下顺序排列:**********\n\n");
while(p!=s)
{
p = p->next;
printf("黑桃%2d ", p->data);
}
printf("\n\n");
} |
|