Home  >  Article  >  Backend Development  >  In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list

In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list

WBOY
WBOYforward
2023-09-13 15:13:01932browse

Given n nodes, the task is to print the nth node at the end of the linked list. The program must not change the order of the nodes in the list, but should only print the nth node from the last node of the linked list.

Example

Input -: 10 20 30 40 50 60
   N=3
Output -: 40

In the above example, starting from the first node, traverse to count-n nodes, that is, 10,20 30,40, 50,60, so the last Three nodes are 40.

In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list

Instead of traversing the entire list so efficiently one can follow -

  • Get a temporary pointer to, say, a temp# of node type
  • ##Set this temporary pointer to the first node head pointer pointed to
  • Set the counter to the number of nodes in the list
  • Move temp to temp → next until count -n
  • Display temp → data
If we use this method, the count will be 5 and the program will iterate through the loop until 5-3, which is 2, so from 0

th Starting at position 10, the result up to 20 is in the 1st position, and the 30th position is in the second position. So with this approach, there is no need to iterate through the entire list until the end, which will save space and memory.

Algorithm

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

Example

Live demonstration

#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

linked list is : 1 2 3 4 5 6
3rd node from the end of linked list is : 4

The above is the detailed content of In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete