纸牌 链表问题(数组能做,我想试试链表 已崩。。。)
有两副扑克牌,牌的顺序都是:大王、小王、黑 桃、红桃、方块、梅花四种花色排列,其中每种花色 又按A,2,3,……J,Q,K的顺序排列,有个人把这两副 扑克牌上下叠放在一起,然后从上到下把单数张的丢 掉,把双数张放在最底层,直到剩下最后一张牌,那 么这张牌是什么?#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int num;
int color;
struct LNode *next;
struct LNode *pre;
}LNode, *LinkList;
//删除
void delete_L(LinkList &L, int i)
{
LinkList p = L;
int j = 0;
for(j=0; j<i; j++)
p = p->next;
p->pre->next = p->next;
p->next->pre = p->pre;
free(p);
}
//利用文件创建链表(也可以使用for循环建立)
void create_L(LinkList &L)
{
FILE *fp = fopen("create.txt","r");
LinkList p = L;
/* LinkList head=(LinkList)malloc(sizeof(LNode));
head->next = head;
head->pre = head;
LinkList p = head;*/
while(!feof(fp))
{
LNode *newbase = (LinkList)malloc(sizeof(LNode));
//memset(newbase, 0, sizeof(newbase));
//fread(newbase, sizeof(LNode), 1, fp);
fscanf(fp, "%d", &newbase->color);
fscanf(fp, "%d", &newbase->num);
if(feof(fp))
{
p->next = NULL;
break;
}
newbase->pre = p->pre;
p->pre->next = newbase;
newbase->next = p;
p->pre = newbase;
}
}
//将双数张放在链尾
void change_L(LinkList &L, int i)
{
/* LinkList p = L->next;
LinkList q = p->next;
L->pre->next = p;
q->pre = p->pre;
p->pre = L->pre;
p->next = L;
*/
L = L->next;
}
//按题目要求排序
void paixu_L(LinkList &L, int &i, int &j)
{
LinkList p=L;
int n = 0;
while(!(p->pre == p->next))
{
if(n=0)
{
delete_L(L, p->next->num);
n=n+1;
}
else
{
change_L(L, p->next->num);
n=n-1;
}
}
i = p->next->num;
j = p->next->color;
}
int main()
{
int i, j = 0;
LinkList L;
printf("i取值1-13代表A-K j取值1-4分别代表黑桃、红桃、方片、梅花\n大王(i,j)=(40,40) 小王(i,j)=(30,30)\n");
printf("请按下列i,j值找出对应纸牌\n");
create_L(L);
paixu_L(L, i, j);
printf("i=%d, j=%d", i, j);
}