用C语言编译一个程序,根据输入的一系列整数,以0标志结束,用尾插法建立单链表,并输出单链表中各元素值,观察输入的内容与输出的内容是否一致。
时间: 2024-05-09 07:19:06 浏览: 73
尾插法单链表的建立
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
int main() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = NULL;
printf("请输入一系列整数(以0结束):\n");
int num;
scanf("%d", &num);
while (num != 0) {
Node *new_node = (Node *)malloc(sizeof(Node));
new_node->data = num;
new_node->next = NULL;
Node *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
scanf("%d", &num);
}
printf("单链表中各元素值为:");
Node *p = head->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
```
阅读全文