本题要求实现,建立一个链表,按输入数据的逆序输出。 函数接口定义: struct ListNode *create(); 函数create利用scanf从输入中获取一系列正整数,当读到-1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。 链表节点结构定义如下: struct ListNode { int data; struct ListNode *next; }; 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *create(); int main() { struct ListNode *p, *head = NULL; head = create(); p = head; while(p != NULL ) { printf("%d ", p->data); p = p->next; } printf("\n"); return 0; } /* 你的代码将被嵌在这里 */
时间: 2024-01-11 08:05:29 浏览: 49
```
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *create() {
struct ListNode *head = NULL;
struct ListNode *temp;
int num;
scanf("%d", &num);
while (num != -1) {
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->data = num;
temp->next = head;
head = temp;
scanf("%d", &num);
}
return head;
}
int main() {
struct ListNode *p, *head = NULL;
head = create();
p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
```
相关问题
本题要求实现一个函数,按输入数据的逆序建立一个链表。 函数接口定义: struct ListNode *createlist(); 函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下: struct ListNode { int data; struct ListNode *next; };
以下是实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode* next;
};
struct ListNode* createlist() {
int num;
struct ListNode* head = NULL; // 头节点指针
scanf("%d", &num);
while (num != -1) { // 输入以-1结尾
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建节点
newNode->data = num;
newNode->next = head; // 新节点指向头节点
head = newNode; // 头节点指向新节点
scanf("%d", &num);
}
return head;
}
```
函数首先定义一个头节点指针 `head`,表示链表的开头。然后通过循环 `scanf` 输入一系列正整数,每读入一个数就新建一个节点,将该节点的 `data` 设置为读入的数值,将该节点的 `next` 指向当前的头节点,将 `head` 指向新建的节点。最终返回头节点指针 `head`,即为逆序建立的链表的开头。
逆序建立链表 分数 10 作者 zy 单位 安阳工学院 本题要求实现,建立一个链表,按输入数据的逆序输出。 函数接口定义: struct ListNode *create(); 函数create利用scanf从输入中获取一系列正整数,当读到-1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。 链表节点结构定义如下: struct ListNode { int data; struct ListNode *next; }; 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *create(); int main() { struct ListNode *p, *head = NULL; head = create(); p = head; while(p != NULL ) { printf("%d ", p->data); p = p->next; } printf("\n"); return 0; } /* 你的代码将被嵌在这里 */ 输入样例: 在这里给出一组输入。例如: 1 2 3 4 5 6 7 -1
根据题目要求,我们需要按输入数据的逆序建立一个链表,并返回链表头指针。具体实现过程如下:
1. 定义一个头结点和一个当前节点,初始时它们都指向 NULL。
2. 从输入中读取一个节点的值,创建一个新节点,将新节点的 next 指向当前节点,将当前节点指向新节点。
3. 重复步骤 2 直到读取完所有节点。
4. 将头结点的 next 指向当前节点的 next,即为链表的头节点。
具体的代码如下:
```C
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *create() {
struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = NULL;
struct ListNode *cur = NULL;
while (true) {
int val;
scanf("%d", &val);
if (val == -1) break;
struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->data = val;
node->next = cur;
cur = node;
}
head->next = cur;
return head->next;
}
int main() {
struct ListNode *p, *head = NULL;
head = create();
p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
```
在这个代码中,我们定义了一个结构体 ListNode 来表示链表中的节点,其中 data 表示节点的值,next 表示指向下一个节点的指针。create 函数用于逆序建立链表,其返回头节点。主函数中我们先使用 create 函数建立链表,然后遍历链表输出每个节点的值。