链表交替节点的乘积


给定n个节点,任务是打印链表中交替节点的乘积。程序必须只打印交替节点的乘积,而无需实际更改节点的位置。

示例

Input -: 10 20 30 40 50 60
Output -: 15000

在上例中,从第一个节点10开始,交替节点为10、30、50,它们的乘积为10*30*50 = 15000。

在上图中,蓝色节点是交替节点(从第一个节点开始),红色节点是不考虑的节点。

下面使用的方案如下

  • 取一个临时指针,假设为temp,类型为节点

  • 将此temp指针设置为head指针指向的第一个节点

  • 当(temp->next!=NULL && temp!=NULL && temp->next->next!=NULL) 条件成立时,移动temp到temp->next->next

  • 设置product=product*(temp->data)

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

算法

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 alternate nodes
   void alternate()
      declare int product
      Set temp=head
      Set product=head->data
      Loop While(temp->next!=NULL && temp!=NULL && temp->next-
         >next!=NULL)
         Set temp=temp->next->next
         Set product=product * (temp->data)
      End
      Print product
Step 5 -> in main()
   Create nodes using struct node* head = NULL;
   Call function insert(10) to insert a node
   Call display() to display the list
   Call alternate() to find alternate nodes product
Stop

代码

 在线演示

#include<stdio.h>
#include<stdlib.h>
//structure of a node
struct node {
   int data;
   struct node *next;
}*head,*temp;
//function for inserting nodes into a list
void insert(int val) {
   struct node* newnode = (struct node*)malloc(sizeof(struct node));
   newnode->data = val;
   if(head == NULL) {
      head = newnode;
      head->next = NULL;
      } else {
      temp=head;
      while(temp->next!=NULL) {
         temp=temp->next;
      }
      newnode->next=NULL;
      temp->next=newnode;
   }
}
//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 alternate elements
void alternate() {
   int product;
   temp=head;
   product=head->data;
   while(temp->next!=NULL && temp!=NULL && temp->next->next!=NULL) {
      temp=temp->next->next;
      product=product * (temp->data);
   }
   printf("
product of alternate nodes is %d : " ,product); } int main() {    //creating list    struct node* head = NULL;    //inserting elements into a list    insert(10);    insert(20);    insert(30);    insert(40);    insert(50);    insert(60);    //displaying the list    printf("linked list is : ");    display();    //calling alternate function for finding product    alternate();    return 0; }

输出

linked list is : 10 20 30 40 50 60
product of alternate nodes is : 15000

更新于: 2019年9月20日

131 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告