将数组的内容,用前插法建立一个带有头节点的单向链表。 #include "stdlib.h" #include "stdio.h" #define N 10 typedef struct list { int data; struct list *next; }NODE; NODE *creat_list(int *a,int n) { } print(NODE *h) //打印链表内容 { NODE *p; p=h->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } printf("\n"); } int main() { int i,j,a[N]={1,13,25,37,9,911,133,415,147,19}; NODE *head; head=creat_list(a,10); print(head); }
时间: 2023-06-19 22:06:09 浏览: 42
可以按照以下方式实现:
```C
NODE *creat_list(int *a,int n)
{
NODE *head, *p;
head = (NODE *)malloc(sizeof(NODE)); // 创建头节点
head->next = NULL; // 头节点不存放数据,指向空
for(int i=0; i<n; i++)
{
p = (NODE *)malloc(sizeof(NODE)); // 创建新节点
p->data = a[i]; // 将数组元素赋值给节点的数据域
p->next = head->next; // 将新节点插入到头节点后面
head->next = p;
}
return head; // 返回头节点指针
}
```
在主函数中调用:
```C
int main()
{
int i,j,a[N]={1,13,25,37,9,911,133,415,147,19};
NODE *head;
head=creat_list(a,10);
print(head);
return 0;
}
```
完整代码如下:
相关问题
将数组的内容,用前插法建立一个带有头节点的单向链表。 #include "stdlib.h" #include "stdio.h" #define N 10 typedef struct list { int data; struct list *next; }NODE; NODE *creat_list(int *a,int n) { } print(NODE *h) //打印链表内容 { NODE *p; p=h->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } printf("\n"); } int main() { int i,j,a[N]={1,13,25,37,9,911,133,415,147,19}; NODE *head; head=creat_list(a,10); print(head); } 使用c语言编写
好的,我知道了。以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct list {
int data;
struct list *next;
} NODE;
NODE *creat_list(int *a, int n) {
NODE *head, *p, *q;
head = (NODE *) malloc(sizeof(NODE));
head->next = NULL;
q = head;
for (int i = 0; i < n; ++i) {
p = (NODE *) malloc(sizeof(NODE));
p->data = a[i];
p->next = q->next;
q->next = p;
q = p;
}
return head;
}
void print(NODE *h) {
NODE *p;
p = h->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
int i, j, a[N] = {1, 13, 25, 37, 9, 911, 133, 415, 147, 19};
NODE *head;
head = creat_list(a, 10);
print(head);
return 0;
}
```
这个程序使用前插法将数组的内容建立为一个带有头节点的单向链表,然后打印链表的内容。在 `creat_list` 函数中,我们首先创建一个头节点,然后遍历数组中的元素,每次创建一个新节点并将其插入到链表的头部。最后返回头节点。在 `print` 函数中,我们从链表的第二个节点开始遍历,并打印每个节点的数据。
//(链表)将数组的内容,用前插法建立一个带有头节点的单向链表。 #include "stdlib.h" #include "stdio.h" #define N 10 typedef struct list { int data; struct list *next; }NODE; NODE *creat_list(int *a,int n) { } print(NODE *h) //打印链表内容 { NODE *p; p=h->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } printf("\n"); } int main() { int i,j,a[N]={1,13,25,37,9,911,133,415,147,19}; NODE *head; head=creat_list(a,10); print(head); }
使用前插法,我们可以从数组的最后一个元素开始,依次将每个元素插入到头节点之后,直到插入完整个数组,建立一个带有头节点的单向链表。在插入时,需要使用一个指针指向链表的头节点,并将新元素插入到该指针指向的节点之后,然后将指针指向新插入的节点。最终建立的链表,头节点不存储数据,是用来方便链表操作的一个虚拟节点。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)