>  기사  >  백엔드 개발  >  C 프로그램에서 다음 내용을 중국어로 번역합니다. 연결 리스트의 맨 아래에서 n번째 노드를 찾는 프로그램

C 프로그램에서 다음 내용을 중국어로 번역합니다. 연결 리스트의 맨 아래에서 n번째 노드를 찾는 프로그램

WBOY
WBOY앞으로
2023-09-13 15:13:01934검색

n개의 노드가 주어지면 작업은 연결된 목록의 끝에 n번째 노드를 인쇄하는 것입니다. 프로그램은 리스트의 노드 순서를 변경해서는 안 되며, 연결된 리스트의 마지막 노드에서 n번째 노드만 인쇄해야 합니다.

Example

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

위의 예에서는 첫 번째 노드부터 n번째 노드, 즉 10,20 30,40, 50,60까지 순회하므로 세 번째에서 마지막 노드는 40입니다.

C 프로그램에서 다음 내용을 중국어로 번역합니다. 연결 리스트의 맨 아래에서 n번째 노드를 찾는 프로그램

전체 목록을 효율적으로 탐색하는 대신 다음과 같은 접근 방식을 사용할 수 있습니다. -

  • 임시 포인터를 노드 유형의 임시 포인터로 가져옵니다.
  • 이 임시 포인터를 가리키는 첫 번째 노드 헤드 포인터로 설정합니다.
  • 카운터를 목록의 노드 수로 설정
  • temp를 temp로 이동 → count-n이 될 때까지 다음
  • show temp → data

이 방법을 사용하면 개수는 5가 되고 프로그램은 다음을 반복합니다. 5-3, 즉 2까지 반복하므로 0번째 위치에서 10부터 시작하여 20까지 결과가 1번째 위치가 되고 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("</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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제