日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度

發布時間:2025/3/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遞歸反轉鏈表改變原鏈表嗎

Solution:

解:

Algorithm to find length

查找長度的算法

Input:

輸入:

A singly linked list 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:

偽代碼:

Begintemp=headc=0 //counter to store count of nodesWhile(temp!=NULL) //upto end of linked listbeginc=c+1temp=temp->nextEnd while End

C code:

C代碼:

#include <stdio.h> #include <stdlib.h>//node sructure typedef struct list {int data;struct list *next; }node;int length(node *temp);int main() {node *head=NULL,*temp,*temp1;int choice,count;//building linked listdo{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 functioncount=length(head);printf("\nThe length of the list is : %d",count);return 0; }//function to find length iteratively int length(node *temp) {int c=0;//travarsing to the end of the list and count the number of nodeswhile(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

翻譯自: https://www.includehelp.com/c-programs/find-the-length-of-a-linked-list-without-using-recursion.aspx

遞歸反轉鏈表改變原鏈表嗎

總結

以上是生活随笔為你收集整理的递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。