博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度
阅读量:2531 次
发布时间:2019-05-11

本文共 2263 字,大约阅读时间需要 7 分钟。

递归反转链表改变原链表吗

Solution:

解:

Algorithm to find length

查找长度的算法

Input:

输入:

A whose address of the first node is stored in a pointer, say head.

一个 ,其第一个节点的地址存储在指针(例如head)中。

Output:

输出:

The no of nodes in the list, c

列表中的节点数c

Data structure used:

使用的数据结构:

Singly linked list where each node contains a data element say data, and the address of the immediate next node say next, with head holding the address of the first node.

单链列表,其中每个节点包含一个数据元素,例如data ,直接下一个节点的地址说next ,头保持第一个节点的地址。

Pseudo code:

伪代码:

Begin    temp=head    c=0 //counter to store count of nodes    While(temp!=NULL) //upto end of linked list	    begin	    c=c+1	    temp=temp->next    End whileEnd

C code:

C代码:

#include 
#include
//node sructuretypedef struct list {
int data; struct list *next;}node;int length(node *temp);int main(){
node *head=NULL,*temp,*temp1; int choice,count; //building linked list do {
temp=(node *)malloc(sizeof(node)); if(temp!=NULL) {
printf("\nEnter the element in the list : "); scanf("%d",&temp->data); temp->next=NULL; if(head==NULL) {
head=temp; } else {
temp1=head; while(temp1->next!=NULL) {
temp1=temp1->next; } temp1->next=temp; } } else {
printf("\nMemory not avilable...node allocation is not possible"); } printf("\nIf you wish to add more data on the list enter 1 : "); scanf("%d",&choice); }while(choice==1); //Now counting the length of the list using a user defined function count=length(head); printf("\nThe length of the list is : %d",count); return 0;}//function to find length iterativelyint length(node *temp) {
int c=0; //travarsing to the end of the list and count the number of nodes while(temp!=NULL) {
c=c+1; temp=temp->next; } return c;}

Output

输出量

Enter the element in the list : 1If you wish to add more data on the list enter 1 : 1Enter the element in the list : 2If you wish to add more data on the list enter 1 : 1Enter the element in the list : 3If you wish to add more data on the list enter 1 : 1Enter the element in the list : 4If you wish to add more data on the list enter 1 : 1Enter the element in the list : 5If you wish to add more data on the list enter 1 : 0The length of the list is : 5

翻译自:

递归反转链表改变原链表吗

转载地址:http://uzxzd.baihongyu.com/

你可能感兴趣的文章
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>