这个是双向链表的创建与其问题
问题是:
创建一个链ABCDEFGHIJKLMNOPQRSTUVWXYZ ,输入3 得到答案为D->E->F->G->H->I->J->K->L->M->N->O->P->Q->R->S->T->U->V->W->X->Y->Z->A->B->C
输入-3答案为X->Y->Z->A->B->C->D->E->F->G->H->I->J->K->L->M->N->O->P->Q->R->S->T->U->V->W
#include <stdio.h>
#include <stdlib.h>
typedef struct SX
{
char a;
struct SX * prior;
struct SX * next;
} SX;
SX * creat(int n) //建立个双向链表
{
SX * p , * q , * head ;
head = (SX *)malloc(sizeof(SX));
head->prior = head->next = NULL;
p = head;
int i = 1;
while( i <= n )
{
q = (SX *)malloc(sizeof(SX));
head->prior = q;
q->prior = p;
q->a = 'A' + ((i++) - 1);
q->next = head->next;
p->next = q;
p = q;
}
return p->next;
}
void show(SX * p, int n) //正向输出
{
int i = 1;
while( i ++ < n )
{
printf("%c->" , p->a);
p = p->next;
}
printf("%c" , p->a);
}
void wohs(SX * p, int n) //反向输出
{
p = p->prior;
int i = 1;
while( i ++ < n )
{
p = p->prior;
printf("%c->" , p->a);
}
printf("%c" , p->prior->a);
}
void Caesar(SX * p, int n, int i) //移位函数
{
if( i > 0 )
{
do
{
p = p->next;
}while( --i );
show(p,n);
}
if( i < 0 )
{
do
{
p = p->prior;
}while( ++i );
show(p->prior,n);
}
}
int main() //主函数测试
{
int n;
cin>>n;
SX * p ;
p = creat(26);
show(p , 26);
printf("\n");
Caesar(p,26,n);
}