首頁 > 後端開發 > C++ > 主體

在C語言中,印出給定索引處的鍊錶節點

WBOY
發布: 2023-08-26 21:21:04
轉載
1087 人瀏覽過

我們必須列印給定索引處鍊錶節點的資料。與數組鍊錶不同,通常沒有索引,因此我們必須遍歷整個鍊錶並在到達特定位置時打印數據。

比方說,列表包含節點 29、34、43、56 和88 並且索引值為1 、2 和4,則輸出將是這些索引處的節點,即34、43 和88。

在C語言中,印出給定索引處的鍊錶節點

範例

Linked list: 29->34->43->56->88
Input: 1 2 4
Output: 34 43 88
登入後複製

在上面的鍊錶表示中,黃色反白的節點是要列印的節點或位於特定索引上的節點。

這裡使用的方法涉及將一個指標和一個計數器變數初始化為1 每當遍歷該節點時,該值就會遞增。計數器與鍵值匹配。當鍵與計數器值相符時,指向節點結構的指標將列印節點的資料並遞增到下一個節點,依此類推,為我們提供特定鍵處的節點。

下面的程式碼顯示了c實作

##演算法

START
   Step 1 -> create node variable of type structure
      Declare int data
      Declare pointer of type node using *next
   Step 2 -> create struct node* intoList(int data)
      Create newnode using malloc
      Set newnode->data = data
      newnode->next = NULL
      return newnode
   step 3 -> Declare function void displayList(struct node *catchead)
      create struct node *temp
      IF catchead = NULL
         Print list is empty
         return
      End
      Set temp = catchead
      Loop While (temp != NULL)
         print temp->data
         set temp = temp->next
      End
   Step 4 -> Declare Function int search(int key,struct node *head)
      Set int index
      Create struct node *newnode
      Set index = 0 and newnode = head
      Loop While (newnode != NULL & newnode->data != key)
         Set index++
         Set newnode = newnode->next
      End
      return (newnode != NULL) ? index : -1
   step 5 -> In Main()
      create node using struct node* head = intoList(9)
      call displayList(head)
      set index = search(24,head)
      IF (index >= 0)
         Print index
      Else
         Print not found in the list
      EndIF
STOP
登入後複製

Example

#include <stdio.h>
#include <stdlib.h>
//structure of a node
struct node {
   int data;
   struct node *next;
};
struct node* intoList(int data) {
   struct node* newnode = (struct node*)malloc(sizeof(struct node));
   newnode->data = data;
   newnode->next = NULL;
   return newnode;
}
//funtion to display list
void displayList(struct node *catchead) {
   struct node *temp;
   if (catchead == NULL) {
      printf("List is empty.</p><p>");
      return;
   }
   printf("elements of list are : ");
   temp = catchead;
   while (temp != NULL) {
      printf("%d ", temp->data);
      temp = temp->next;
   }
   printf("</p><p>");
}
//function to search element
int search(int key,struct node *head) {
   int index;
   struct node *newnode;
   index = 0;
   newnode = head;
   while (newnode != NULL && newnode->data != key) {
      index++;
      newnode = newnode->next;
   }
   return (newnode != NULL) ? index : -1;
}
int main() {
   int index;
   struct node* head = intoList(9); //inserting elements into a list
   head->next = intoList(76);
   head->next->next = intoList(13);
   head->next->next->next = intoList(24);
   head->next->next->next->next = intoList(55);
   head->next->next->next->next->next = intoList(109);
   displayList(head);
   index = search(24,head);
   if (index >= 0)
      printf("%d found at position %d</p><p>", 24, index);
   else
      printf("%d not found in the list.</p><p>", 24);
   index=search(55,head);
   if (index >= 0)
      printf("%d found at position %d</p><p>", 55, index);
   else
   printf("%d not found in the list.</p><p>", 55);
}
登入後複製

輸出

如果我們執行上面的程序,將會產生以下輸出。

elements of list are : 9 76 13 24 55 109
24 found at position 3
55 found at position 4
登入後複製

以上是在C語言中,印出給定索引處的鍊錶節點的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!