首頁  >  文章  >  後端開發  >  在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,所以倒數第三個節點是40。

在C程式中,將以下內容翻譯為中文:找出鍊錶倒數第n個節點的程式

而不是如此有效率地遍歷整個列表可以遵循的方法-

  • 取得一個臨時指針,比如說,節點類型的temp
  • 將此暫存指標設定為指向的第一個節點頭指標
  • 將計數器設定為清單中的節點數
  • 將temp 移到temp → 下一個直到count -n
  • 顯示temp → 資料

如果我們使用這個方法,計數將為5,程式將迭代循環直到5-3,即2,因此從0th 位置上的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刪除