題目
- 要求實作使用者輸入一個數使得26個字母排列發生變化,如使用者輸入3則輸出結果:DEFGHIJKLMNOPQRSTUVWXYZABC;使用者輸入-3則輸出結果:XYZABCDEFGHIJKLMNOPQRSTUVW
//============================================================================
// Name : InsertSort.cpp
// Author : YL
// Version :
//============================================================================
#include <iostream>
#include <string>
#define Error 0
#define OK 1
using namespace std;
typedef char Elemtype;
typedef int Status;
typedef struct DualNode
{
Elemtype data;
struct DualNode *prior;
struct DualNode *next;
}DualNode, *DualLinkList;
Status Init(DualLinkList *L)
{
DualNode *p, *q;
int i;
*L = (DualNode *)malloc(sizeof(DualNode));
if(!(*L)) return Error;
(*L)->prior = (*L)->next = NULL;
p = *L;
for (i =; i<; i++)
{
q = (DualNode *)malloc(sizeof(DualNode));
if(!(q)) return Error;
q->data = 'A'+i;
//注意用(p),不要用(*L)
q->next = p->next;
q->prior = p;
p->next = q;
p = q;
}
p->next = (*L)->next;
(*L)->next->prior = p;
return OK;
}
void Caeser(DualLinkList *L, int i)
{
if(i>)
{
do
{
(*L) = (*L)->next;
}while(--i);
}
if(i<)
{
do
{
(*L) = (*L)->prior;
}while(++i);
}
}
int main()
{
DualLinkList L;
int n;
Init(&L);
printf("初始化字段:\n");
for (int i = ; i<;i++)
{
L = L->next;
printf("%c",L->data);
}
printf("\n");
cin >>n ;
Caeser(&L,n);
printf("操作後字段:\n");
for (int i = ; i<;i++)
{
L = L->next;
printf("%c",L->data);
}
return ;
}
//輸出結果
//
//初始化字段:
//ABCDEFGHIJKLMNOPQRSTUVWXYZ
//3
//操作後字段:
//DEFGHIJKLMNOPQRSTUVWXYZABC
//
//初始化字段:
//ABCDEFGHIJKLMNOPQRSTUVWXYZ
//-5
//操作後字段:
//VWXYZABCDEFGHIJKLMNOPQRSTU
核心思路解析(圖檔)