リンクリストのノードをC言語で指定されたインデックスに出力します
与えられたインデックスでリンクリストのノードのデータを印刷する必要があります。配列のリンクリストとは異なり、通常はインデックスがないため、リンクリスト全体をトラバースして、特定のリストに到達したときにデータを出力する必要があります。
たとえば、リストにノード29、34、43、56、88が含まれ、インデックスの値が1、2、4である場合、出力はこれらのインデックスのノードである34、43、88になります。
例
Linked list: 29->34->43->56->88 Input: 1 2 4 Output: 34 43 88
上記のリンクリストの表現で、黄色で強調表示されているノードは、印刷されるノードまたは特定のインデックスにあるノードです。
ここで使用されるアプローチは、ノードがトラバースされるたびに増分される1に初期化された1つのポインターと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
例
#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.\n");
return;
}
printf("elements of list are : ");
temp = catchead;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
//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\n", 24, index);
else
printf("%d not found in the list.\n", 24);
index=search(55,head);
if (index >= 0)
printf("%d found at position %d\n", 55, index);
else
printf("%d not found in the list.\n", 55);
} 出力
上記のプログラムを実行すると、次の出力が生成されます。
elements of list are : 9 76 13 24 55 109 24 found at position 3 55 found at position 4
-
C++で特定のノードから距離kにあるすべてのノードを出力します
この問題では、二分木、ターゲットノード、整数Kが与えられます。ターゲットノードから距離Kにあるツリーのすべてのノードを印刷する必要があります。 。 二分木 は、各ノードに最大2つのノード(1つまたは2つ/なし)を持つ特別なツリーです。 問題を理解するために例を見てみましょう K =2 ターゲットノード:9 出力 − 5 1 3. 説明 − 距離は、ノードの上位、下位、または同じレベルで取得できます。したがって、それに応じてノードを返します。 この問題を解決するには、ターゲットノードからK距離離れたノードのタイプを理解する必要があります。 上記の試験から、k個の離
-
C ++で再帰を使用して、リンクリストの代替ノードを出力します
リンクリストは、要素を連続していないメモリ位置に格納する線形データ構造です。すべての要素には、リンクリストの次の要素へのポインタが含まれています。 例 − この問題では、リンクリストが与えられ、このリンクリストの要素を印刷する必要がありますが、代替要素のみが印刷されます。問題をよりよく理解するために例を見てみましょう。 Input : 2 -> 4 -> 1 -> 67 -> 48 -> 90 Output : 2 -> 1 -> 48 説明 −リンクリストに代替要素を出力します。したがって、1番目、3番目、5番目の要素が印刷されます。