1. 问题描述:
将数组的所有元素使用尾插法插入到一个链表中,最终的序列与插入的序列是一致的,具体节点的插入如下图:
2. 下面是具体的C语言代码:
#include<stdio.h>
#include<malloc.h>
typedef struct node{
int data;
node *next;
}LNode;
LNode *C;
//返回一个指向链表C的指针
LNode* createListR(LNode *&C, int a[], int n){
LNode *s, *r;
int i;
C = (LNode*)malloc(sizeof(LNode));
C->next = NULL;
r = C;
for(int i = 0; i < n; ++i){
s = (LNode*)malloc(sizeof(LNode));
s->data = a[i];
r->next = s;
r = r->next;
}
r->next = NULL;
return C;
}
int main(void){
int a[10] = {1, 56, 23, 100, 45, 78, 10, 29, 61, 10};
LNode *p = createListR(C, a, 10);
while(p->next != NULL){
printf("%d ", p->next->data);
p = p->next;
}
return 0;
}