Traversing a singly linked list with a dummy header

In this problem you will Implement a function to traverse a singly linked list with a dummy header and output the data field of each node.

Structure description:

The node structure is shown below:

复制代码
typedef struct ListNode {
    int data;
    struct ListNode *next;
} node;

typedef node* position;
typedef position List;

Function definition:

复制代码
void traversal(List L);

The parameter L is a pointer to the dummy header. The function outputs the data field of each node as it traverses the singly linked list.

Test program example:

cs 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode {
    int data;
    struct ListNode *next;
}node;

typedef node* position;
typedef position List;

void traversal(List L);

// The questioner has implemented the createList function.
// This function reads a series of positive integers separated by spaces
// and inserts them into a linked list using the head insertion method.
// Entering -1 indicates the end of input.
// creatgeList函数由题目提供,不需要在本题的答案中实现
List createList();

int main(void)
{
    List L = createList();
    traversal(L);
    return 0;
}

/* Please write the answer here */

Input Specification:

A series of positive integers separated by spaces. Entering -1 indicates the end of input.

Output Specification:

Output the data field of each node in the singly linked list. After each number, output a space.(每个整数后输出1个空格)

Sample Input :

复制代码
9 5 7 2 -1

Sample Output :

复制代码
2 7 5 9 

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

C程序如下:

cs 复制代码
void traversal(List L){ //遍历链表的函数,参数L是指向链表头节点的指针  
    if(L->next ==NULL){//检查该链表是否为空
        return;//如果为空则返回
    }
    L = L->next;//L指向该链表的第一个数据域
    while(L!=NULL){//遍历整个单链表
        printf("%d ", L->data);
        L= L->next;
    }
}
相关推荐
clever1018 分钟前
在QtCreator 4.10.2中调试qt程序qDebug()输出中文为乱码问题的解决
开发语言·qt
YuTaoShao14 分钟前
【LeetCode 每日一题】3010. 将数组分成最小总代价的子数组 I——(解法二)排序
算法·leetcode·排序算法
测试开发Kevin30 分钟前
小tip:换行符CRLF 和 LF 的区别以及二者在实际项目中的影响
java·开发语言·python
Abona35 分钟前
C语言嵌入式全栈Demo
linux·c语言·面试
松☆1 小时前
Dart 核心语法精讲:从空安全到流程控制(3)
android·java·开发语言
编码者卢布1 小时前
【App Service】Java应用上传文件功能部署在App Service Windows上报错 413 Payload Too Large
java·开发语言·windows
kaikaile19951 小时前
结构风荷载理论与Matlab计算
开发语言·matlab
切糕师学AI2 小时前
ARM 汇编器中的伪指令(Assembler Directives)
开发语言·arm开发·c#
吴维炜2 小时前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
No0d1es2 小时前
电子学会青少年软件编程(C语言)等级考试试卷(三级)2025年12月
c语言·c++·青少年编程·电子学会·三级