Cプログラムのリンクリストの最後からn番目のノードのプログラム
n個のノードがある場合、タスクはリンクリストの最後からn番目のノードを印刷することです。プログラムは、リスト内のノードの順序を変更してはなりません。代わりに、リンクリストの最後からn番目のノードのみを出力する必要があります。
例
Input -: 10 20 30 40 50 60 N=3 Output -: 40
上記の例では、最初のノードからカウントnノードまでのノードがトラバースされます(10、20、30、40、50、60)。したがって、最後から3番目のノードは40です。
リスト全体をトラバースする代わりに、この効率的なアプローチに従うことができます-
- 一時的なポインタ、たとえばノードタイプの温度を取得します
- この一時ポインタを、ヘッドポインタが指す最初のノードに設定します
- カウンターをリスト内のノード数に設定します
- tempをtempに移動→count-nまで次へ
- 表示温度→データ
このアプローチを使用する場合、カウントは5になり、プログラムは5〜3、つまり2までループを繰り返すため、0 th の10から開始します。 1 st の20よりも場所 場所と2 nd の30 結果である場所。したがって、このアプローチでは、リスト全体を最後までトラバースする必要がなく、スペースとメモリを節約できます。
アルゴリズム
Start Step 1 -> create structure of a node and temp, next and head as pointer to a structure node struct node int data struct node *next, *head, *temp End Step 2 -> declare function to insert a node in a list void insert(int val) struct node* newnode = (struct node*)malloc(sizeof(struct node)) newnode->data = val IF head= NULL set head = newnode set head->next = NULL End Else Set temp=head Loop While temp->next!=NULL Set temp=temp->next End Set newnode->next=NULL Set temp->next=newnode End Step 3 -> Declare a function to display list void display() IF head=NULL Print no node End Else Set temp=head Loop While temp!=NULL Print temp->data Set temp=temp->next End End Step 4 -> declare a function to find nth node from last of a linked list void last(int n) declare int product=1, i Set temp=head Loop For i=0 and i<count-n and i++ Set temp=temp->next End Print temp->data Step 5 -> in main() Create nodes using struct node* head = NULL Declare variable n as nth to 3 Call function insert(10) to insert a node Call display() to display the list Call last(n) to find nth node from last of a list Stop
例
#include<stdio.h>
#include<stdlib.h>
//structure of a node
struct node{
int data;
struct node *next;
}*head,*temp;
int count=0;
//function for inserting nodes into a list
void insert(int val){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = NULL;
if(head == NULL){
head = newnode;
temp = head;
count++;
} else {
temp->next=newnode;
temp=temp->next;
count++;
}
}
//function for displaying a list
void display(){
if(head==NULL)
printf("no node ");
else {
temp=head;
while(temp!=NULL) {
printf("%d ",temp->data);
temp=temp->next;
}
}
}
//function for finding 3rd node from the last of a linked list
void last(int n){
int i;
temp=head;
for(i=0;i<count-n;i++){
temp=temp->next;
}
printf("\n%drd node from the end of linked list is : %d" ,n,temp->data);
}
int main(){
//creating list
struct node* head = NULL;
int n=3;
//inserting elements into a list
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
//displaying the list
printf("\nlinked list is : ");
display();
//calling function for finding nth element in a list from last
last(n);
return 0;
} 出力
linked list is : 1 2 3 4 5 6 3rd node from the end of linked list is : 4
-
リンクリストの概念をC言語で説明する
リンクリストを理解する前に、Cプログラミング言語での配列の欠点とポインタの利点について学びましょう。 アレイのデメリット 静的メモリ割り当てが含まれます アレイではメモリの浪費が発生する可能性があります。 メモリの不足は、アレイの重要な欠点の1つです。 ポインタの利点 動的メモリ割り当てが含まれます。 メモリの効果的な使用はポインタで体験できます。 リンクリスト リンクリストは動的メモリ割り当てを使用します。つまり、それに応じて拡大および縮小します。それらはノードのコレクションとして定義されます。ここで、ノードにはデータとリンクの2つの部分があります。データ、リンク
-
単一リンクリストのノードの積
n個のノードがある場合、タスクは、単一リンクリストのすべてのノードの積を出力することです。プログラムは、最初のノードからNULLが見つからなくなるまで、単一リンクリストのすべてのノードをトラバースする必要があります。 例 Input -: 1 2 3 4 5 Output -: 120 上記の例では、最初のノードから開始して、すべてのノードがトラバースされます。つまり、1、2、3、4、5、6であり、それらの積は1 * 2 * 3 * 4 * 5 * 6 =120 以下で使用するアプローチは次のとおりです 一時的なポインタ、たとえばノードタイプの温度を取得します この一時ポインタを、