我是靠谱客的博主 自然鲜花,这篇文章主要介绍单链表基本操作题目DescriptionInputOutputSample Input 1Sample Output 1Sample Input 2Sample Output 2题解,现在分享给大家,希望可以做个参考。

Description

输入3 4 5 6 7 9999一串整数,9999代表结束,通过头插法新建链表,并输出,通过尾插法新建链表并输出。

Input

3 4 5 6 7 9999,第二行也是3 4 5 6 7 9999,数据需要输入两次

Output

如果输入是3 4 5 6 7 9999,那么输出是7 6 5 4 3,数之间空格隔开,尾插法的输出是3 4 5 6 7

Sample Input 1

3 4 5 6 7 9999
3 4 5 6 7 9999

Sample Output 1

7 6 5 4 3
3 4 5 6 7

Sample Input 2

1 3 5 7 9 9999
1 3 5 7 9 9999

Sample Output 2

9 7 5 3 1
1 3 5 7 9

题解

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h> #include<stdlib.h> typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; void head_insert(LinkList &L){ L = (LinkList)malloc(sizeof(LNode)); L->next = NULL; int x; LinkList s; scanf("%d",&x); while(x!=9999){ s = (LinkList)malloc(sizeof(LNode)); s->data = x; s->next = L->next; L->next = s; scanf("%d",&x); } } void tail_insert(LinkList &L){ L = (LinkList)malloc(sizeof(LNode)); L->next = NULL; LinkList s,tail = L; int x; scanf("%d",&x); while(x!=9999){ s = (LinkList)malloc(sizeof(LNode)); s->data = x; tail->next = s; tail = s; scanf("%d",&x); } tail->next = NULL; } void PrintList(LinkList L){ L=L->next; while(L!=NULL) { printf("%d",L->data); L=L->next; if(L!=NULL) { printf(" "); } } printf("n"); } int main(){ LinkList head,tail; head_insert(head); PrintList(head); tail_insert(tail); PrintList(tail); return 0; }

最后

以上就是自然鲜花最近收集整理的关于单链表基本操作题目DescriptionInputOutputSample Input 1Sample Output 1Sample Input 2Sample Output 2题解的全部内容,更多相关单链表基本操作题目DescriptionInputOutputSample内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部