0

0

单链表的节点乘积

WBOY

WBOY

发布时间:2023-08-28 09:13:08

|

656人浏览过

|

来源于tutorialspoint

转载

给定 n 个节点,任务是打印单链表中所有节点的乘积。程序必须从初始节点开始遍历单向链表的所有节点,直到找不到 null。

示例

Input -: 1 2 3 4 5
Output -: 120

在上面的例子中,从第一个节点开始遍历所有节点,即 1, 2 3, 4, 5, 6,它们的乘积为 1*2*3*4*5*6 = 120

云网OA
云网OA

采用JSP开发的办公自动化产品、基于B/S结构,运行环境:JDK v1.5、Tomcat v5.5、MySQL v4.1,三者均为以上版本其他相关内容:可视化流程设计: 流程支持串签、会签和分支流程,可以设置流程节点的修改、删除权限,并可指定流程中各个用户在表单中可以填写的域。智能表单所见即所得设计: 智能设计,自动在数据库中生成表格,方便优化程序 公共交流: 集论坛、博客、聊天室于一体文件柜:C

下载
单链表的节点乘积

下面使用的方法如下

  • 获取一个临时指针,比如节点类型的 temp
  • 将此临时指针设置为头指针指向的第一个节点
  • 当 temp 不为 NULL 时,将 temp 移动到 temp ->next。
  • 设置 Product=product*(temp->data)

算法

  • H2>
    Start
    Step 1 -> create structure of a node and temp, next and head as pointer to a structure node
       struct node
          int data
          struct node *next, *head, *temp
       End
    Step 2 -> declare function to insert a node in a list
       void insert(int val)
          struct node* newnode = (struct node*)malloc(sizeof(struct node))
          newnode->data = val
          IF head= NULL
             set head = newnode
             set head->next = NULL
          End
          Else
             Set temp=head
             Loop While temp->next!=NULL
             Set temp=temp->next
          End
          Set newnode->next=NULL
          Set temp->next=newnode
       End
    Step 3 -> Declare a function to display list
       void display()
          IF head=NULL
             Print no node
          End
          Else
             Set temp=head
             Loop While temp!=NULL
                Print temp->data
                Set temp=temp->next
             End
          End
    Step 4 -> declare a function to find alternate nodes
       void product_nodes()
          declare int product=1
          Set temp=head
       Loop While temp!=NULL
          Set product=product * (temp->data)
          Set temp=temp->next
       End
       Print product
    Step 5 -> in main()
       Create nodes using struct node* head = NULL;
       Call function insert(10) to insert a node
       Call display() to display the list
       Call product_nodes() to find alternate nodes product
    Stop

    示例

    #include
    #include
    //structure of a node
    struct node{
       int data;
       struct node *next;
    }*head,*temp;
    //function for inserting nodes into a list
    void insert(int val){
       struct node* newnode = (struct node*)malloc(sizeof(struct node));
       newnode->data = val;
       newnode->next = NULL;
       if(head == NULL){
          head = newnode;
          temp = head;
       } else {
          temp->next=newnode;
          temp=temp->next;
       }
    }
    //function for displaying a list
    void display(){
       if(head==NULL)
          printf("no node ");
       else{
          temp=head;
          while(temp!=NULL){
             printf("%d ",temp->data);
             temp=temp->next;
          }
       }
    }
    //function for finding product
    void product_nodes(){
       int product=1;
       temp=head;
       while(temp!=NULL){
          product=product * (temp->data);
          temp=temp->next;
       }
       printf("

    product of nodes is : %d" ,product); } int main(){ //creating list struct node* head = NULL; //inserting elements into a list insert(1); insert(2); insert(3); insert(4); insert(5); insert(6); //displaying the list printf("linked list is : "); display(); //calling function for finding prodouct Product_nodes(); return 0; }

    输出

    linked list is : 1 2 3 4 5 6
    product of nodes is : 720
  • 相关专题

    更多
    c语言中null和NULL的区别
    c语言中null和NULL的区别

    c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

    229

    2023.09.22

    java中null的用法
    java中null的用法

    在Java中,null表示一个引用类型的变量不指向任何对象。可以将null赋值给任何引用类型的变量,包括类、接口、数组、字符串等。想了解更多null的相关内容,可以阅读本专题下面的文章。

    434

    2024.03.01

    页面置换算法
    页面置换算法

    页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

    387

    2023.08.14

    php源码安装教程大全
    php源码安装教程大全

    本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

    7

    2025.12.31

    php网站源码教程大全
    php网站源码教程大全

    本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

    4

    2025.12.31

    视频文件格式
    视频文件格式

    本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

    7

    2025.12.31

    不受国内限制的浏览器大全
    不受国内限制的浏览器大全

    想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

    7

    2025.12.31

    出现404解决方法大全
    出现404解决方法大全

    本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

    41

    2025.12.31

    html5怎么播放视频
    html5怎么播放视频

    想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

    3

    2025.12.31

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    Sass 教程
    Sass 教程

    共14课时 | 0.7万人学习

    PHP课程
    PHP课程

    共137课时 | 8.1万人学习

    麻省理工大佬Python课程
    麻省理工大佬Python课程

    共34课时 | 5万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号