6-3 逆序数据建立链表(20 分)_头插法建链表
2017-10-14 13:15
716 查看
6-3 逆序数据建立链表(20 分)
本题要求实现一个函数,按输入数据的逆序建立一个链表。
函数接口定义:
struct ListNode *createlist();
函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下:
struct ListNode {
int data;
struct ListNode *next;
};
裁判测试程序样例:
/* 你的代码将被嵌在这里 */
输入样例:
1 2 3 4 5 6 7 -11
输出样例:
7 6 5 4 3 2 1
本题要求实现一个函数,按输入数据的逆序建立一个链表。
函数接口定义:
struct ListNode *createlist();
函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下:
struct ListNode {
int data;
struct ListNode *next;
};
裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); int main() { struct ListNode *p, *head = NULL; head = createlist(); for ( p = head; p != NULL; p = p->next ) printf("%d ", p->data); printf("\n"); return 0; }
/* 你的代码将被嵌在这里 */
输入样例:
1 2 3 4 5 6 7 -11
输出样例:
7 6 5 4 3 2 1
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist() { struct ListNode *head = NULL, *now = NULL; int a; while (1) { scanf("%d", &a); if (a == -1)return head; now = (struct ListNode*)malloc(sizeof(struct ListNode)); now->data = a; now->next = head; head = now; } } int main() { struct ListNode *p, *head = NULL; head = createlist(); for (p = head; p != NULL; p = p->next) printf("%d ", p->data); printf("\n"); return 0; } /* 你的代码将被嵌在这里 */
相关文章推荐
- SDUT OJ 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 2117数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 【2117】数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构——链表之逆序建立链表
- SDUT 2117数据结构实验之链表二:逆序建立链表
- C语言 链表 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 【2117】数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构——20 单链表逆序
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表