问题描述:对双向链表对26个字母进行移位
代码如下:
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 typedef char ElemType; typedef int Status; typedef struct DualNode { ElemType data; struct DualNode *prior; struct DualNode *next; }DualNode, *DuLinkList; Status InitList(DuLinkList *L){ DualNode *p, *q; int i; *L = (DuLinkList)malloc(sizeof(DualNode)); if( !(*L) ) { return ERROR; } (*L)->prior = (*L)->next = NULL; p = (*L); // 26个字母生成 for( i=0; i<26; i++ ) { q = (DualNode *) malloc( sizeof(DualNode) ); if( !q ){ return ERROR; } q->data = 'A' + i; q->prior = p; q->next = p->next; p->next = q; p = q; } // 循环 p->next = (*L)->next; (*L)->next->prior = p; return OK; } void Caesar(DuLinkList *L, int i) { if ( i>0 ) { do { (*L) = (*L)->next; }while(--i); } if( i<0 ) { (*L) = (*L)->next->prior; do { (*L) = (*L)->prior; }while( ++i ); } } int main() { DuLinkList L; int i,n; InitList(&L); printf("请输入一个整数: "); scanf("%d",&n); Caesar(&L, n); for( i=0; i<26; i++ ) { L = L->next; printf("%c",L->data); } return 0; }
效果截图:
输入正数3:
输入负数 -3: