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;
    }
}
相关推荐
重生之我是Java开发战士几秒前
【递归、搜索与回溯】记忆化搜索:斐波那契数列,不同路径,最长递增子序列,猜数字游戏II,矩阵中最长递增路径
算法·leetcode·深度优先
yy我不解释5 分钟前
关于comfyui的mmaudio音频生成插件时时间不一致问题(四)(video upload)(解决方法)
开发语言·python·ai作画·音视频·comfyui
干啥啥不行,秃头第一名6 分钟前
C++与机器学习框架
开发语言·c++·算法
hongtianzai7 分钟前
Laravel7.x十大核心特性解析
java·c语言·开发语言·golang·php
爱吃涮毛肚的肥肥(暂时吃不了版)9 分钟前
Leetcode——181.超过经理收入的员工
算法·leetcode·职场和发展
永远睡不够的入10 分钟前
C++庖丁解牛:深入理解多态:从虚函数表到底层实现
开发语言·c++
姚青&12 分钟前
Pytest fixture 参数化(params 参数)
开发语言·python·pytest
Charlie_lll12 分钟前
力扣解题-接雨水
算法·leetcode
仰泳的熊猫14 分钟前
题目2580:蓝桥杯2020年第十一届省赛真题-分类计数
数据结构·c++·算法·蓝桥杯
qyzm14 分钟前
牛客周赛 Round 136
数据结构·python·算法