写了一个魔术师发牌问题的C语言代码。大家帮我看一下这个问题
#include <stdlib.h>
#define CardNumber 13
typedef struct node {
int data;
struct node *next;
}lnode, *linklist;
linklist createLinkList()
{
linklist head = NULL;
linklist s, r;
int i;
r = head;
for ( i = 1; i <= CardNumber; i++ )
{
s = (linklist) malloc( sizeof(lnode) );
s->data = 0; /* 将所有节点初始化为0 */
if ( head == NULL )
head = s;
else
r->next = s;
r = s;
}
r->next = head;
return(head);
}
int main(){
linklist head=NULL, p;
head = createLinkList();
p = head;
p->data = 1;
int i;
for(i=0; i<CardNumber; i++){
p = p->next;
p->data=i+1;
}
printf("%d",head->data);
return 0;
}
---------------------------------------------------------
请问 在int main里面我将p指向head, 但是为什么最后输出head的时候是13而不是1呢 展开
#include <stdio.h>
#include <stdlib.h>
#define CardNumber 13
typedef struct node {
int data;
struct node *next;
}lnode, *linklist;
linklist createLinkList()
{
linklist head = NULL;
linklist s, r;
int i;
r = head;
for ( i = 1; i <= CardNumber; i++ )
{
s = (linklist) malloc( sizeof(lnode) );
s->data = i; /* 将所有节点初始化为相应数字 */
if ( head == NULL )
head = s;
else
r->next = s;
r = s;
}
r->next = head;
return(head);
}
int main(){
linklist head=NULL, p;
head = createLinkList();
p = head;
int i;
for(i=0; i<CardNumber; i++){ //输出链表所有结点值
printf("%d ",p->data);
p = p->next;
}
return 0;
}
你运行一下我的代码 我想问的是 为什么我输出的head->data是13而不是1呢