Diberi n nod, tugasnya adalah untuk mencetak nod ke-n di hujung senarai terpaut. Program ini tidak boleh mengubah susunan nod dalam senarai, tetapi hanya boleh mencetak nod ke-n dari nod terakhir senarai terpaut.
Input -: 10 20 30 40 50 60 N=3 Output -: 40
Dalam contoh di atas, bermula dari nod pertama, traverse ke count-n nod, iaitu, 10,20 30,40, 50,60, jadi nod ketiga hingga terakhir ialah 40. .
Tetapkan pembilang kepada bilangan nod dalam senarai
Alihkan suhu ke suhu → seterusnya sehingga kiraan-n tunjukkan suhu → dataStart 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("</p><p>%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("</p><p>linked list is : "); display(); //calling function for finding nth element in a list from last last(n); return 0; }
Output Atas ialah kandungan terperinci Dalam program C, terjemah kandungan berikut ke dalam bahasa Cina: Program untuk mencari nod ke-n dari bahagian bawah senarai terpaut. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!linked list is : 1 2 3 4 5 6
3rd node from the end of linked list is : 4