C语言 关于链表赋值的提问 求各路大神

最近在写一个加密的代码,老师要求必须使用链表,该算法中需要大量的输入预设好的数据如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;
}

2个回答

p->next=p;
这个不对
应该是
head->next=p;
head=head->next;

qq_37630212
qq_37630212 我按照你说的试了 可是这下什么的显示不出来了?这该怎么办??
大约 3 年之前 回复

#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;
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问