📄 答案.txt
字号:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{int number, cipher;
}elemtype;
typedef struct SLNode
{elemtype data;
struct SLNode *next;
}SLNodetype;
int InitiateSL(SLNodetype **h)
{if(!(*h=(SLNodetype*)malloc(sizeof(SLNodetype))))
return 0;
(*h)->next=*h;
return 1;
}
int InsertSL(SLNodetype *h, int i, elemtype x)
{SLNodetype *p, *s;
int j;
if(!h||i<=0) return 0;
p=h, j=0;
while((j==0||p!=h)&&j<i-1) p=p->next, j++;
if(j!=i-1||(i>1&&p==h))
{printf("Invalid position!\n"); return 0;
}
if(!(s=(SLNodetype*)malloc(sizeof(SLNodetype))))
return 0;
s->data=x, s->next=p->next, p->next=s;
return 1;
}
int DeleteSL(SLNodetype *h, int i, elemtype *x)
{SLNodetype *p, *s;
int j;
if(!h||i<=0) return 0;
p=h, j=0;
while(p->next!=h) p=p->next;
p->next=h->next;
while(j<i-1) p=p->next, j++;
s=p->next;
if(p->next!=p) h->next=s->next, p->next=h;
else h->next=h;
*x=s->data, free(s);
return 1;
}
SLNodetype *GetSL(SLNodetype *h, int i)
{SLNodetype *p;
int j;
if(!h||i<=0) return NULL;
p=h, j=0;
while((j==0||p!=h)&&j<i) p=p->next, j++;
if(p!=h&&j==i) return p;
else return NULL;
}
void readelem(elemtype *x)
{scanf("%d", &x->cipher);
}
int CreateSL(SLNodetype **h, int n)
{int i;
elemtype x;
if(!InitiateSL(h)) return 0;
printf("Input %d ciphers: \n", n);
for(i=1; i<=n; i++)
{readelem(&x), x.number=i;
if(!InsertSL(*h, i, x)) return 0;
}
return 1;
}
void writeelem(elemtype x)
{printf("number=%d, cipher=%d\n", x.number, x.cipher);
}
void DisplaySL(SLNodetype *h)
{int i;
SLNodetype *x;
printf("Displaying list:\n");
for(i=1; 1; i++)
if((x=GetSL(h, i))) writeelem(x->data);
else
{printf("%d elements displayed.\n", i-1); break;
}
}
void main(void)
{SLNodetype *ha;
int n, m;
elemtype x;
printf("Inpute n and m: ");
scanf("%d %d", &n, &m);
CreateSL(&ha, n);
printf("ha created: ");
DisplaySL(ha);
printf("Deleting order:\n");
while(ha->next!=ha)
{DeleteSL(ha, m, &x);
m=x.cipher;
writeelem(x);
}
}
/*
Inpute n and m: 7 20
Input 7 ciphers: 3 1 7 2 4 8 4
*/
/*
ha created: Displaying list:
number=1, cipher=3
number=2, cipher=1
number=3, cipher=7
number=4, cipher=2
number=5, cipher=4
number=6, cipher=8
number=7, cipher=4
7 elements displayed.
Deleting order:
number=6, cipher=8
number=1, cipher=3
number=4, cipher=2
number=7, cipher=4
number=2, cipher=1
number=3, cipher=7
number=5, cipher=4
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -