最近在写一个加密的代码,老师要求必须使用链表,该算法中需要大量的输入预设好的数据如26个字母及数字,在创建链表时,通过自己设定的Print函数验证时发现指针没有走动,赋值只是停留在最后一个内容,求大神帮忙看看该怎么改才能把指针走动好让我继续存储数据下去。
Node1 *CreatNode1()
{
Node1 *head,*tail,*p;
head=tail=NULL;
{
p=(Node1*)malloc (sizeof(Node1));
p->alphabet='A';
p->n1=24;
p->n2=24;
p->num=1;
p=(Node1*)malloc (sizeof(Node1));
p->alphabet='B';
p->n1=25;
p->n2=22;
p->next=p;
p->alphabet='C';
p->n1=26;
p->n2=7;
p->next=NULL;
if(NULL == head)
{head =p;
}
else
{tail->next=p;
}
tail=p;
}
return head;
}
完整代码如下
#include
#include
#include
struct Node1
{
char alphabet;
int n1;
int n2;
int num;
struct Node1 *next;
};
typedef struct Node1 Node1;
struct Node2
{
char alphabet;
int n1;
int num;
struct Node2 *next;
};
Node1 *CreatNode1()
{
Node1 *head,*tail,*p;
head=tail=NULL;
{
p=(Node1*)malloc (sizeof(Node1));
p->alphabet='A';
p->n1=24;
p->n2=24;
p->num=1;
// head->next=p;
// head=head->next;
p=(Node1*)malloc (sizeof(Node1));
p->alphabet='B';
p->n1=25;
p->n2=22;
// head->next=p;
// head=head->next;
p->alphabet='C';
p->n1=26;
p->n2=7;
p->next=NULL;
if(NULL == head)
{head =p;
}
else
{tail->next=p;
}
tail=p;
}
return head;
}
void Print(Node1 *head)
{
Node1 *p;
p=head;
if (NULL ==head)
{
printf("链表为空");
}
else
{
printf("链表如下\n");
while(p != NULL)
{
printf("%c %d %d",p->alphabet,p->n1,p->n2);
p=p->next;
}
printf("\n");
}
}
void ReleaseNode1(Node1 *head)
{
Node1 *p1,*p2;
p1=head;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
free(p2);
}
}
/*
void encrypt(char ch)
{
while(head1!==NULL)
{
if(ch == head1->alphabet)
*/
int main()
{
Node1 *head1;
head1=CreatNode1();
//Node2 *head2;
//head2=CreatNode2();
Print(head1);
ReleaseNode1(head1);
return 0;
}