ホームページ  >  記事  >  バックエンド開発  >  C プログラムで、次の内容を中国語に翻訳します。 リンクされたリストの下から n 番目のノードを検索するプログラム

C プログラムで、次の内容を中国語に翻訳します。 リンクされたリストの下から n 番目のノードを検索するプログラム

WBOY
WBOY転載
2023-09-13 15:13:01934ブラウズ

n 個のノードがある場合、タスクはリンク リストの最後に n 番目のノードを出力することです。プログラムはリスト内のノードの順序を変更してはなりませんが、リンクされたリストの最後のノードから n 番目のノードのみを出力する必要があります。

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

上記の例では、最初のノードから開始して、count-n 個のノード、つまり 10,20 30,40, 50,60 まで移動します。つまり、最後の 3 個はノード数は40です。

C プログラムで、次の内容を中国語に翻訳します。 リンクされたリストの下から n 番目のノードを検索するプログラム

リスト全体を効率的に走査する代わりに、次のようにすることもできます -

  • たとえば、ノード タイプの temp# への一時ポインタを取得します。
  • ##この一時ポインタを、指す最初のノード ヘッド ポインタに設定します。
  • カウンタをリスト内のノードの数に設定します。
  • Move temp to temp → next until count -n
  • Display temp → data
このメソッドを使用すると、カウントは 5 になり、プログラムは 5-3 (つまり 2) までループを繰り返しますので、0 から始まります。

th 10 位から始まり、20 位までが 1 位、30 位が 2 位となります。したがって、このアプローチを使用すると、リスト全体を最後まで繰り返す必要がなく、スペースとメモリが節約されます。

アルゴリズム

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("</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;
}

出力

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

以上がC プログラムで、次の内容を中国語に翻訳します。 リンクされたリストの下から n 番目のノードを検索するプログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。