复制代码
1
2
3
4
5
6
7题目描述 输入一个正整数序列,遇负数时停止,建立一个线性链表存储读入的数据,将各个元素按顺序输出。 样例输入 Copy 1 2 3 4 5 -1 样例输出 Copy 1 2 3 4 5
复制代码
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#include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; typedef struct Lnode{ int data; struct Lnode*next; }Lnode,*linklist; linklist tailinsert(linklist L){ int x; L=(Lnode*)malloc(sizeof(Lnode));//头节点,此处返回头指针给L; Lnode *s,*r=L; L->next=NULL; scanf("%d",&x); while(x!=-1){ s=(Lnode*)malloc(sizeof(Lnode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return L; } void printlinklist(linklist L){ linklist p; p=L->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } printf("n"); } int main() { linklist L; L=tailinsert(L); printlinklist(L); return 0; }
最后
以上就是开心玫瑰最近收集整理的关于尾插法建立单链表并输出的全部内容,更多相关尾插法建立单链表并输出内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复