请用C语言创建一个不带头结点的单链表
时间: 2024-05-12 09:17:47 浏览: 136
好的,以下是创建一个不带头结点的单链表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = NULL, *tail = NULL, *temp = NULL;
int num;
printf("请输入整数,输入 -1 结束:\n");
scanf("%d", &num);
while (num != -1) {
temp = (struct Node *) malloc(sizeof(struct Node));
temp -> data = num;
temp -> next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
} else {
tail -> next = temp;
tail = temp;
}
scanf("%d", &num);
}
printf("链表的值为:\n");
temp = head;
while (temp != NULL) {
printf("%d ", temp -> data);
temp = temp -> next;
}
return 0;
}
```
希望这能够帮到您!
阅读全文