C语言从键盘输入数据,以$结束,建立链表
时间: 2023-05-17 12:03:05 浏览: 102
非常感谢您的提问。以下是建立链表的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
int num;
while (scanf("%d", &num) != EOF) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = num;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
return 0;
}
```
这段代码可以从键盘输入数据,以$结束,然后将输入的数据存储在链表中,并输出链表中的所有数据。希望能够帮助到您。
阅读全文