#include <stdio.h>
#include <string.h>
#define ERROR 0
#define OK 1
#define ElemType char
#define N 26
typedef int status;
typedef struct Node
{
ElemType data;
struct Node* perior;
struct Node* next;
}Node;
typedef struct Node* Linklist;
/*遍历双向链表元素并打印*/
status VisitList(Linklist L)
{
Linklist p;
int j=0;
p=(Linklist)malloc(sizeof(Node));
p=L;
while( p->next != L)
{
printf("%c",p->data);
p=p->next;
j++;
}
printf("%c\n",p->data);
j++;
printf("链表长度为:%d\n",j);
return OK;
}
/*使用尾插法创建26个字母的双向链表*/
Linklist CreateListTail(Linklist *L)
{
Linklist r,p,head;
int j;
*L=(Linklist)malloc(sizeof(Node));
head=(Linklist)malloc(sizeof(Node));
r=(Linklist)malloc(sizeof(Node));
head=r=*L;
srand(time(0));
for(j=0;j<26;j++)
{
p=(Linklist)malloc(sizeof(Node));
p->data='A'+j;
r->next=p;
p->perior=r;
r=p;
}
r->next=head->next;
head->next->perior=r;
free(head);
return r->next;
}
/*得到明文字母在链表中的位置*/
Linklist GetElem(Linklist L,ElemType e)
{
Linklist p;
p=L;
int j=0;
while( p->data != e)
{
p=p->next;
j++;
}
return p;
}
/*根据随机生成数字进行移位*/
Linklist MoveList(Linklist *L,int n)
{
Linklist head;
int j;
head=(Linklist)malloc(sizeof(Node));
head=*L;
if(n==0)
return *L;
if(n>0)
{
for(j=0;j<n;j++)
head=head->next;
}
if(n<0)
{
for(j=0;j<-n;j++)
head=head->perior;
}
return head;
}
int main()
{
int x,n;
ElemType e;
Linklist L,temp;
char s[1000];
int a[1000];
srand(time(0));
FILE *fp;
fp=fopen("D:\\test.txt","w");
L=CreateListTail(&L);
printf("输入明文:");
scanf("%s",s);
for(x=0;x<strlen(s);x++)
{ temp=L;
a[x]=rand()%100;
temp=GetElem(L,s[x]);
temp=MoveList(&temp,a[x]%26);
printf("%c ",temp->data);
fprintf(fp,"%c ",temp->data);
}
printf("\n");
for(x=0;x<strlen(s);x++)
{
printf("%d ",a[x]);
}
fprintf(fp,"\n");
x=0;
n=strlen(s);
while(n--)
{fprintf(fp,"%d ",a[x]);
x++;
};
fclose(fp);
return 0;
}
【数据结构与算法】双向链表-维吉尼亚加密
最新推荐文章于 2020-07-30 15:37:09 发布