目錄
鍊錶的類型
程式
輸出
首頁 後端開發 C++ C程式以找到鍊錶的長度

C程式以找到鍊錶的長度

Sep 07, 2023 pm 07:33 PM
c程式 找到 鍊錶長度

連結清單使用動態記憶體分配,即它們相應地增長和收縮。它們被定義為節點的集合。這裡,節點有兩個部分,即資料和鏈路。資料、連結和鍊錶的表示如下-

C程式以找到鍊錶的長度

鍊錶的類型

鍊錶有四種類型,如下: -

  • 單鍊錶/ 單鍊錶
  • 雙/雙向鍊錶
  • 循環單鍊錶
  • 循環雙鍊錶

我們用遞歸方法求鍊錶長度的邏輯是-

int length(node *temp){
   if(temp==NULL)
      return l;
   else{
      l=l+1;
      length(temp->next);
   }
}
登入後複製

程式

以下是求鍊錶長度的C程式-

 現場示範

#include 
#include 
typedef struct linklist{
   int data;
   struct linklist *next;
}node;
int l=0;
int main(){
   node *head=NULL,*temp,*temp1;
   int len,choice,count=0,key;
   do{
      temp=(node *)malloc(sizeof(node));
      if(temp!=NULL){
         printf("

enter the elements in a 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("

Memory is full"); } printf("

press 1 to enter data into list: "); scanf("%d",&choice); }while(choice==1); len=length(head); printf("The list has %d no of nodes",l); return 0; } //recursive function to find length int length(node *temp){ if(temp==NULL) return l; else{ l=l+1; length(temp->next); } }

登入後複製

輸出

當執行上述程序時,會產生以下結果-

Run 1:
enter the elements in a list: 3
press 1 to enter data into list: 1
enter the elements in a list: 56
press 1 to enter data into list: 1
enter the elements in a list: 56
press 1 to enter data into list: 0
The list has 3 no of nodes
Run 2:
enter the elements in a list: 12
press 1 to enter data into list: 1
enter the elements in a list: 45
press 1 to enter data into list: 0
The list has 2 no of nodes
登入後複製

以上是C程式以找到鍊錶的長度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

將以下內容翻譯為中文:C程式將羅馬數字轉換為十進位數字 將以下內容翻譯為中文:C程式將羅馬數字轉換為十進位數字 Sep 05, 2023 pm 09:53 PM

將以下內容翻譯為中文:C程式將羅馬數字轉換為十進位數字

C++程式比較兩個字串的字典序 C++程式比較兩個字串的字典序 Sep 04, 2023 pm 05:13 PM

C++程式比較兩個字串的字典序

C程式以找到鍊錶的長度 C程式以找到鍊錶的長度 Sep 07, 2023 pm 07:33 PM

C程式以找到鍊錶的長度

C++程式以給定值為參數,找出雙曲正弦反函數的值 C++程式以給定值為參數,找出雙曲正弦反函數的值 Sep 17, 2023 am 10:49 AM

C++程式以給定值為參數,找出雙曲正弦反函數的值

C++程式列印字典 C++程式列印字典 Sep 11, 2023 am 10:33 AM

C++程式列印字典

C程式使用rename()函數更改檔名 C程式使用rename()函數更改檔名 Sep 21, 2023 pm 10:01 PM

C程式使用rename()函數更改檔名

C程式實作歐幾裡得演算法 C程式實作歐幾裡得演算法 Sep 17, 2023 pm 12:41 PM

C程式實作歐幾裡得演算法

複製的東西在哪裡可以找到 複製的東西在哪裡可以找到 Feb 09, 2024 pm 10:18 PM

複製的東西在哪裡可以找到

See all articles