#include <iostream>using namespacestd;#defineNUM 100/*linked list data structure*/typedefstructlnode{intdata; structLnode *Next;} Lnode,*linklist;/*the initialization of a linked list*/voidInitlist (Linklist &L) {L=NewLnode; L->next=NULL;}/*insertion of linked list data*/voidInsertlist (linklist &l)//set up a circular link list{Lnode*head,*temp; Head=m; for(intI=0; i<num;i++) {Temp=NewLnode; Temp->data=rand ()%2000000000-100000000; Temp->next=NULL; Head->next=temp; Head=head->Next; }}voidoutput (linklist L) { for(intI=0; i<num;i++) {cout<<L->next->data<<" "; L=l->Next; }}intMainintargcChar*argv[]) { intmax,sum,flag=0;//Sums are the sum of the sub-arrays, and Max is the sum of the largest sub-arrays intordern=0, orderx=0; Linklist L; Lnode*temp1,*Temp2; Initlist (L); Insertlist (L); //inserting data into a linked list by the userTemp2=l->Next; Max=l->next->data;//Max Initial value is the first number in a linked list for(intj=0;j<num;j++,temp2=temp2->next) { for(intk=j;k<num;k++) {sum=0; Temp1=Temp2; for(intH=j;h<=k;h++,temp1=temp1->next) {Sum=sum+temp1->data; } if(max<sum)//assigns the maximum value to Max, and saves the current ordinal number{Max=sum; Ordern=J; Orderx=K; } }} Temp1=l->Next; cout<<"array:"<<Endl; Output (L); cout<<endl<<"the maximum number of sub-arrays is:"; for(intI=0; i<ordern;i++)//find the first number of sub-arrays when the maximum value is obtained{Temp1=temp1->Next; } for(intj=0;j< (orderx-ordern+1); j++,temp1=temp1->next)//will get maximum and sub-array element output{cout<<temp1->data<<" "; } cout<<endl<<"the number of the largest sub-arrays is:"<<max<<Endl;; return 0;}
One-dimensional array summation 2